CRM 4.0 – JavaScript – Stop Form from Saving

A member of the CRM team was working on requirement which involved checking two values on a form.  We then validate the form by checking to see if two fields have values in, if the two fields are not set then we pop up an alert telling the user to fill in the values of the two fields and stop the form from saving.

To stop a form from saving this is the Javascript you should use.

event.returnValue = false;
return false;

in context :-

// If the user selected no for both Sales Approved and Sales Agreed, alert and stop save

salesapproved =crmForm.all.new_salesapproved.DataValue
salesagreed =crmForm.all.new_salesagreed.DataValue

if (salesapproved==false && salesagreed==false)
{
alert(“Sales Approved and Sales agreed are set to no, please rectify”)
// Cancel the save operation.
event.returnValue = false;
return false;
};

3 thoughts on “CRM 4.0 – JavaScript – Stop Form from Saving

  1. neel July 27, 2012 / 3:13 pm

    Hi,
    I am using same javascript code to stop saving a form, but it is still saving form with the restricted value on the form.
    I am using it on on-change event of field, I know that I can use same code on on-save event, then it will work, But I do not have to put this code on on-save event because of some condition. Can anyone help me with this.

    Thanks in advance.
    Neel

    below is my code:

    if((crmForm.all.new_ption.DataValue == ‘3’) || (crmForm.all.new_option.DataValue == ‘4’))
    {
    alert(‘You cannot select Special(abc) or Special(xyz)’);
    crmForm.all.new_option.SetFocus();
    event.returnValue = false;
    return false;
    }
    else
    {
    return true;
    }

    Like

    • Hosk July 27, 2012 / 3:16 pm

      have you tried pressing F12 and using the debugger to step through the code to see what values are being passed and what route it is taking

      Like

      • Neel July 30, 2012 / 7:47 am

        yes I tried, using debugger i can see the code i checked there is also same code nothing changed.
        In the form, only a specific role of user can change a specific option from picklist field.
        User with other roles cannot select that specific option from picklist, but they can select other options from the same picklist field, they can change other field as well and can save the form even if the picklist special options are already selected. That is why i wrote that code in on-change event of picklist.
        here is my complete code:

        //The following function returns all roles of a specific user by creating a SOAP message request to the CRM Service:

        //*********************
        function GetCurrentUserRoles()
        {
        var xml = “” +
        “” +
        “” +
        GenerateAuthenticationHeader() +
        ” ” +
        ” ” +
        ” ” +
        ” role” +
        ” ” +
        ” ” +
        ” name” +
        ” ” +
        ” ” +
        ” false” +
        ” ” +
        ” ” +
        ” roleid” +
        ” role” +
        ” systemuserroles” +
        ” roleid” +
        ” Inner” +
        ” ” +
        ” ” +
        ” systemuserid” +
        ” systemuserroles” +
        ” systemuser” +
        ” systemuserid” +
        ” Inner” +
        ” ” +
        ” And” +
        ” ” +
        ” ” +
        ” systemuserid” +
        ” EqualUserId” +
        ” ” +
        ” ” +
        ” ” +
        ” ” +
        ” ” +
        ” ” +
        ” ” +
        ” ” +
        ” ” +
        ” ” +
        “” +
        “”;

        var xmlHttpRequest = new ActiveXObject(“Msxml2.XMLHTTP”);
        xmlHttpRequest.Open(“POST”, “/mscrmservices/2007/CrmService.asmx”, false);
        xmlHttpRequest.setRequestHeader(“SOAPAction”,” http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple“);
        xmlHttpRequest.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8”);
        xmlHttpRequest.setRequestHeader(“Content-Length”, xml.length);
        xmlHttpRequest.send(xml);

        var resultXml = xmlHttpRequest.responseXML;
        return(resultXml);
        }

        //*********************
        //The following function checks if the specified user has a specified user role:
        //*********************
        function UserHasRole(roleName) {
        //get Current User Roles, oXml is an object
        var oXml = GetCurrentUserRoles();
        if (oXml != null) {
        //select the node text
        var roles = oXml.selectNodes(“//BusinessEntity/q1:name”);
        if (roles != null) {
        for (i = 0; i < roles.length; i++) {
        if (roles[i].text == roleName) {
        //return true if user has this role
        return true;
        }
        }
        }
        }
        //otherwise return false
        return false;
        }

        //The following code block uses the above function to do some logic based on user’s security roles:

        //*********check role***********

        if(!(UserHasRole("Operations Director")) && !(UserHasRole("Finance Director")) && !(UserHasRole("Systeemaanpasser")) && !(UserHasRole("Systeembeheerder")))
        {
        if((crmForm.all.new_standaardofspecial.DataValue == '3') ||(crmForm.all.new_standaardofspecial.DataValue == '4'))
        {
        alert('You cannot select Special(Goedgekeurd door MCXess) or Special(Afgekeurd door MCXess)');
        crmForm.all.new_standaardofspecial.SetFocus();
        event.returnValue = false;
        return false;
        }
        else
        {
        return true;
        }
        }
        else
        {
        return true;
        }

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.