locked
CRM 2011 onsave javascript error RRS feed

  • Question

  • I get the below error when saving the form:

    There was an error with this field's customized event.

    Field:crmForm

    Event:onsave

    Error:Unable to get value of the property'setSubmitMode':object is null or undefined

    JS code here:

    function Form_onload() {
        //PRICE LIST DEFAULT
        var pricelevel = Xrm.Page.data.entity.attributes.get("pricelevelid").getValue();
        if (pricelevel == null || pricelevel == "" || pricelevel == "undefined") {
            var lookupData = new Array();
            var lookupItem = new Object();
            if (ORG_UNIQUE_NAME == "CRMTEST") {
                lookupItem.id = '{E27C78A6-070C-E211-9F9E-0050568C3D2C}';
            }
            else {
                lookupItem.id = '{4ECEE2C4-E0F7-DA11-A791-000BCD4F5459}';
            }
            lookupItem.entityType = 'pricelevel';
            lookupItem.name = 'LIST';
            lookupData[0] = lookupItem;
            Xrm.Page.getAttribute("pricelevelid").setValue(lookupData);
            Xrm.Page.getAttribute("pricelevelid").setSubmitMode("always");
        }
        //SET REQUIREMENT LEVEL
        Xrm.Page.getAttribute("gtri_contractnameid").setRequiredLevel("required");
        Xrm.Page.getAttribute("gtri_contactid").setRequiredLevel("none");
    
        //DISABLE FIELDS
        Xrm.Page.ui.controls.get("gtri_reasonforloss").setDisabled(true);
        Xrm.Page.ui.controls.get("estimatedvalue").setDisabled(true);
        Xrm.Page.ui.controls.get("gtri_50pctdate").setDisabled(true);
        Xrm.Page.ui.controls.get("gtri_75pctdate").setDisabled(true);
        Xrm.Page.ui.controls.get("gtri_75pcthwswrevenue").setDisabled(true);
        Xrm.Page.ui.controls.get("gtri_75pcthwswgp").setDisabled(true);
        Xrm.Page.ui.controls.get("gtri_75pctservicesrevenue").setDisabled(true);
        Xrm.Page.ui.controls.get("gtri_75pctservicesgp").setDisabled(true);
        Xrm.Page.ui.controls.get("gtri_90pctdate").setDisabled(true);
        Xrm.Page.ui.controls.get("gtri_100pctdate").setDisabled(true);
        Xrm.Page.ui.controls.get("actualclosedate").setDisabled(true);
    
        //LOST -- REQUIRE REASON FOR LOSS
        var prob = Xrm.Page.data.entity.attributes.get("gtri_probabilitytoclose").getValue();
        if (prob == 7) {
            Xrm.Page.ui.controls.get("gtri_reasonforloss").setDisabled(false);
            Xrm.Page.getAttribute("gtri_reasonforloss").setRequiredLevel("required");
            Xrm.Page.ui.controls.get("gtri_reasonforloss").setVisible(true);
        }
        else {
            Xrm.Page.ui.controls.get("gtri_reasonforloss").setDisabled(true);
            Xrm.Page.getAttribute("gtri_reasonforloss").setRequiredLevel("none");
            Xrm.Page.ui.controls.get("gtri_reasonforloss").setVisible(false);
        }
    
        //SET OPP TYPE SETTINGS
        gtri_opportunitytype_onchange();
    
        //SHAREPOINT IFRAME
        var spfolderpath = Xrm.Page.getAttribute("gtri_spfolderpath").getValue();
        if (spfolderpath == null || spfolderpath == "" || spfolderpath == "undefined") {
            Xrm.Page.getControl("IFRAME_Documents").setSrc("about:blank");
        }
        else {
            Xrm.Page.getControl("IFRAME_Documents").setSrc(spfolderpath);
        }
    }
    function Form_onsave() {
        Xrm.Page.data.entity.attributes.get("estimatedvalue").setSubmitMode("always");
        Xrm.Page.data.entity.attributes.get("gtri_totalgp").setSubmitMode("always");
        Xrm.Page.data.entity.attributes.get("gtri_probabilitytoclose").setSubmitMode("always");
        Xrm.Page.data.entity.attributes.get("gtri_expectedprojectstart").setSubmitMode("always");
        Xrm.Page.data.entity.attributes.get("gtri_rptuid").setSubmitMode("always");
    
        //SET CRM PHASE FIELD
        var phaseAttribute = Xrm.Page.getAttribute("stepname");
        var probalitityText = Xrm.Page.getAttribute("gtri_probabilitytoclose").getSelectedOption().text;
        phaseAttribute.setValue(probalitityText);
    
        //REPLACE INVALID CHARACTERS
        var name = Xrm.Page.getAttribute("name").getValue();
        name = name.replace(' & ', ' and ');
        name = name.replace('&', ' and ');
        name = name.replace('&', '');
        name = name.replace('+', '');
        name = name.replace('~', '');
        name = name.replace('#', '');
        name = name.replace('%', '');
        name = name.replace('*', '');
        name = name.replace('{', '');
        name = name.replace('}', '');
        name = name.replace('\\', '');
        name = name.replace(':', '');
        name = name.replace('<', '');
        name = name.replace('>', '');
        name = name.replace('?', '');
        name = name.replace('/', '');
        name = name.replace('"', '');
        name = name.replace('|', '');
        name = name.replace('"', '');
        name = name.replace('.', '');
        Xrm.Page.getAttribute("name").setValue(name);
    
        var accountId = Xrm.Page.getAttribute("customerid").getValue()[0].id;
        if (Xrm.Page.context.getServerUrl() == "http://srvdev250/GTRI") {
            var clientURL = "http://srvdev250/GTRI";
        }
        else {
            var clientURL = "https://gtri.gtrint.com";
        }
        var clientURL = Xrm.Page.context.getServerUrl();
        var odataSelect = clientURL + "/xrmservices/2011/OrganizationData.svc/AccountSet?$select=gtri_gtri_class_account/GTRI_name&$expand=gtri_gtri_class_account&$filter=AccountId eq guid'" + accountId + "'";
    
        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: odataSelect,
            beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
            success: function (data, textStatus, XmlHttpRequest) {
                // Use only one of these two methods
    
                // Use for a selection that may return multiple entities
                //ProcessReturnedEntities(data.d.results);
    
                // Use for a single selected entity
                ProcessReturnedEntity(data.d);
    
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + odataSelect); }
        });
    
        function ProcessReturnedEntity(SingleEntity) {
            var singleEntity = SingleEntity;
            var AccountClass = singleEntity.GTRI_name;
    
            if (Xrm.Page.context.getOrgUniqueName() == "CRMTEST") {
                classDoD = '{1A75742C-070C-E211-9F9E-0050568C3D2C}';
            }
            else {
                classDoD = '{FD907D0B-C461-DF11-BD51-0050569F4744}';
            }
            if (AccountClass == classDoD) {
                // Set the crm checkbox field value
                Xrm.Page.getAttribute("gtri_rptuid").setValue(true);
            }
            else {
                Xrm.Page.getAttribute("gtri_rptuid").setValue(false);
            }
    
        }
    }
    function gtri_opportunitytype_onchange() {
        //REQUIRE REASON FOR LOSS FIELD
        var probtoclose = Xrm.Page.data.entity.attributes.get("gtri_probabilitytoclose").getValue();
        if (probtoclose == 7) {
            Xrm.Page.ui.controls.get("gtri_reasonforloss").setDisabled(false);
            Xrm.Page.getAttribute("gtri_reasonforloss").setRequiredLevel("required");
        }
    }
    function gtri_probabilitytoclose_onchange() {
        // Create variables to capture the "Probability To Close" selected value
        var selectedOptionSet = Xrm.Page.getAttribute("gtri_probabilitytoclose");
        var optionValue = selectedOptionSet.getSelectedOption().value;
        // Function checks to see if the field passed in contains a value and returns true if it does, false if not
        function checkForValue(fieldName) {
            var fieldValue = Xrm.Page.getAttribute(fieldName).getValue();
            //alert(fieldValue);
            if (fieldValue == "" || fieldValue == "undefined" || fieldValue == null) {
                return false;
            }
            else {
                return true;
            }
        }
        // Functions that will be used to set the 50/75/90/100% Date Fields
        function setPercentDate(fieldName) {
            // Create variable to hold today's Date
            var _today = new Date();
            // Set control's value to current date
            Xrm.Page.ui.controls.get(fieldName).setDisabled(false);
            Xrm.Page.getAttribute(fieldName).setValue(_today);
            Xrm.Page.ui.controls.get(fieldName).setDisabled(true);
            Xrm.Page.getAttribute(fieldName).setSubmitMode("always");
        }
        function set75PercentFields() {
            //ENABLE FIELDS
            Xrm.Page.ui.controls.get("gtri_75pcthwswrevenue").setDisabled(false);
            Xrm.Page.ui.controls.get("gtri_75pcthwswgp").setDisabled(false);
            Xrm.Page.ui.controls.get("gtri_75pctservicesrevenue").setDisabled(false);
            Xrm.Page.ui.controls.get("gtri_75pctservicesgp").setDisabled(false);
            //SET VALUES
            Xrm.Page.getAttribute("gtri_75pcthwswrevenue").setValue(Xrm.Page.getAttribute("gtri_hwswmaintrevenue").getValue());
            Xrm.Page.getAttribute("gtri_75pcthwswgp").setValue(Xrm.Page.getAttribute("gtri_hwswmaintgp").getValue());
            Xrm.Page.getAttribute("gtri_75pctservicesrevenue").setValue(Xrm.Page.getAttribute("gtri_servicesrevenue").getValue());
            Xrm.Page.getAttribute("gtri_75pctservicesgp").setValue(Xrm.Page.getAttribute("gtri_servicesgp").getValue());
            //DISABLE FIELDS
            Xrm.Page.ui.controls.get("gtri_75pcthwswrevenue").setDisabled(true);
            Xrm.Page.ui.controls.get("gtri_75pcthwswgp").setDisabled(true);
            Xrm.Page.ui.controls.get("gtri_75pctservicesrevenue").setDisabled(true);
            Xrm.Page.ui.controls.get("gtri_75pctservicesgp").setDisabled(true);
            //FORCESUBMIT FIELDS
            Xrm.Page.getAttribute("gtri_75pcthwswrevenue").setSubmitMode("always");
            Xrm.Page.getAttribute("gtri_75pcthwswgp").setSubmitMode("always");
            Xrm.Page.getAttribute("gtri_75pctservicesrevenue").setSubmitMode("always");
            Xrm.Page.getAttribute("gtri_75pctservicesgp").setSubmitMode("always");
        }
        //LOST -- REQUIRE REASON FOR LOSS
        if (optionValue == 7) {
            Xrm.Page.ui.controls.get("gtri_reasonforloss").setDisabled(false);
            Xrm.Page.getAttribute("gtri_reasonforloss").setRequiredLevel("required");
            Xrm.Page.getControl("gtri_reasonforloss").setVisible(true);
        }
        else {
            // Check if this is a Project Opportunity and set appropriate values based on the "Probability To Close" dropdownlist selection
            if (Xrm.Page.getAttribute("gtri_opportunitytype").getText() == 2) {
                // User selected 50%
                if (optionValue == 3) {
                    // Set value for the "50% Date" field
                    setPercentDate("gtri_50pctdate");
                }
                    // User selected 75%
                else if (optionValue == 4) {
                    // Check if the "50% Date" field has a value
                    if (Xrm.Page.getAttribute("gtri_50pctdate").getValue() == "" || Xrm.Page.getAttribute("gtri_50pctdate").getValue() == "undefined" || Xrm.Page.getAttribute("gtri_50pctdate").getValue() == null) {
                        // Set value for the "50% Date" field
                        setPercentDate("gtri_50pctdate");
                    }
                    // Set values for the "75%" fields
                    setPercentDate("gtri_75pctdate");
                    set75PercentFields();
                }
                    // User selected 90%
                else if (optionValue == 5) {
                    // Check if we have a 75% date, and if not set the ProbToClose dropdownlist to null and alert the user
                    if (checkForValue("gtri_75pctdate") == false) {
                        // Empty the value in the "Probability To Close" dropdownlist
                        Xrm.Page.getAttribute("gtri_probabilitytoclose").setValue(null);
                        // Give user popup alert that this is not allowed
                        alert("You must set the Opportunity to 75% and save the Opportunity before moving forward.");
                    }
                    else {
                        // Set value for the "90% Date" field
                        setPercentDate("gtri_90pctdate");
                    }
                }
                    // User selected 100%
                else if (optionValue == 6) {
                    // Check if we have a 75% date, and if not set the ProbToClose dropdownlist to null and alert the user
                    if (checkForValue("gtri_75pctdate") == false) {
                        // Empty the value in the "Probability To Close" dropdownlist
                        Xrm.Page.getAttribute("gtri_probabilitytoclose").setValue(null);
                        // Give user popup alert that this is not allowed
                        alert("You must set the Opportunity to 75% and save the Opportunity before moving forward.");
                    }
                    else {
                        // Check if we have a 90% date
                        if (checkForValue("gtri_90pctdate") == false) {
                            // Set value for the "90% Date" field
                            setPercentDate("gtri_90pctdate");
                        }
                        // Set value for the "100% Date" field
                        setPercentDate("gtri_100pctdate");
                    }
                }
            }
        }
    }

    Please help! Thank you

    newjeep

    Tuesday, April 23, 2013 3:58 PM

Answers

  • Hmm.... Can you add at the very start of the on save function the following line

    debugger;

    Also, in your internet explorer settings (internet options on the advanced tab). Go into the browsing section and make sure "disable script debugging" is unchecked.

    From here when you click save you should get prompted to open a script editing tool(visual studio, etc). should be able to step through using f10 and see which field exactly it is breaking on.

    Let me know if you have any questions regarding this process! :)

    • Marked as answer by newjeep Tuesday, April 23, 2013 6:57 PM
    Tuesday, April 23, 2013 6:38 PM

All replies

  • Hi Newjeep,

    It looks like one of the values you are setting  with SetSubmitMode is either not on the form or mistyped. Just a thought. Let me know when you get a second!

    So one of the following

    • pricelevelid
    • estimatedvalue
    • gtri_totalgp
    • gtri_probabilitytoclose
    • gtri_expectedprojectstart
    • gtri_rptuid
    • gtri_50pctdate (used in the function =setPercentDate)
    • gtri_75pctdate (used in the function =setPercentDate)
    • gtri_90pctdate (used in the function =setPercentDate)
    • gtri_100pctdate (used in the function =setPercentDate)
    Tuesday, April 23, 2013 5:23 PM
  • I have a second.....there are fields that are not on the form. They are listed below:

    estimatedvalue

    gtri_rptuid


    newjeep

    Tuesday, April 23, 2013 5:55 PM
  • Well 2 options for you then. 

    1) remove the pieces of code making changes to these fields

    or

    2) add these fields to the form.

    Reason your seeing the error is because the JavaScript isn't seeing these fields on the form when they are referenced in the code. :)

    Tuesday, April 23, 2013 6:08 PM
  • I did comment out the two fields/values in the js onSave function and I still got the same error

    newjeep

    Tuesday, April 23, 2013 6:18 PM
  • Hmm.... Can you add at the very start of the on save function the following line

    debugger;

    Also, in your internet explorer settings (internet options on the advanced tab). Go into the browsing section and make sure "disable script debugging" is unchecked.

    From here when you click save you should get prompted to open a script editing tool(visual studio, etc). should be able to step through using f10 and see which field exactly it is breaking on.

    Let me know if you have any questions regarding this process! :)

    • Marked as answer by newjeep Tuesday, April 23, 2013 6:57 PM
    Tuesday, April 23, 2013 6:38 PM
  • Thanks for your help. I did miss the field/value and so I commeted it out and now the onSave function works!

    newjeep

    Tuesday, April 23, 2013 6:58 PM
  • Glad to hear this is running for ya. Figured it had to be something small :)
    Tuesday, April 23, 2013 7:01 PM