locked
Error message with Javacode on CRM 2013 RRS feed

  • Question

  • Hi Everybody!!

    After the upgrade from CRM 2011 to 2013, I am running into many Error Messages. Here are two of them. I tried to correct it by myself but there are still error messages on the onsave, onload event. Please your help would be appreciated.

    -----------------------------------------------------------------------------------------------------------------------------------------------

    //+------When VAR Contact is selected, auto populate the VAR from the selected Contact's Parent Account
    function setVAR_Account() {
     var CRM_FORM_TYPE = Xrm.Page.ui.getFormType();
     if ((CRM_FORM_TYPE != 1) && (CRM_FORM_TYPE != 2)) { return; }
       if(Xrm.Page.getAttribute("new_varcontact").getValue()!=null) {
      if(Xrm.Page.getAttribute("new_varcontact").getValue()[0]. entityType =="contact") {
      //alert ("This opportunity is bound to a contact. The relationship will automatically change for the contact's parent account.");
    var idCustomer = Xrm.Page.getAttribute("new_varcontact").getValue()[0].id;
      var retrieveRecordsReq = new XMLHttpRequest();
      var ODataPath = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc";
          retrieveRecordsReq.open('GET', ODataPath+"/ContactSet(guid'" + idCustomer + "')",false);
      retrieveRecordsReq.setRequestHeader("Accept", "application/json");
      retrieveRecordsReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
      retrieveRecordsReq.send();

      var records = this.parent.JSON.parse(retrieveRecordsReq.responseText).d;

      var lookupValue = new Array();
      lookupValue[0] = new Object();
      lookupValue[0].id = records.ParentCustomerId.Id;
      lookupValue[0].name = records.ParentCustomerId.Name;
      lookupValue[0].entityType = "account";
      Xrm.Page.getAttribute("new_varifapplicable").setValue(lookupValue);  //used to be 'customerid' 
      
      var contact_TextFieldValue = Xrm.Page.getAttribute("new_varifapplicable").getValue()[0].name;
                                               Xrm.Page.getAttribute("new_varifapplicable_label").setValue(contact_TextFieldValue);
      
         var account_TextFieldValue = Xrm.Page.getAttribute("new_varcontact").getValue()[0].name;
      Xrm.Page.getAttribute("new_varcontact_label").setValue(account_TextFieldValue);
      }
     }
    }

    //+------When End-User Contact is selected, auto populate the End-User from the selected End-User's Parent Account
    function setEndUser_Account() {
     
     var CRM_FORM_TYPE = Xrm.Page.ui.getFormType();
     if ((CRM_FORM_TYPE != 1) && (CRM_FORM_TYPE != 2)) { return; }
     
     if(Xrm.Page.getAttribute("new_endusercontact").getValue()!=null) {
      if(Xrm.Page.getAttribute("new_endusercontact").getValue()[0].entityType =="contact") {
      //alert ("This opportunity is bound to a contact. The relationship will automatically change for the contact's parent account.");

      var idCustomer = Xrm.Page.getAttribute("new_endusercontact").getValue()[0].id;
      var retrieveRecordsReq = new XMLHttpRequest();
      var ODataPath = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc";
       
      retrieveRecordsReq.open('GET', ODataPath+"/ContactSet(guid'" + idCustomer + "')",false);
      retrieveRecordsReq.setRequestHeader("Accept", "application/json");
      retrieveRecordsReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
      retrieveRecordsReq.send();

      var records = this.parent.JSON.parse(retrieveRecordsReq.responseText).d;

      var lookupValue = new Array();
      lookupValue[0] = new Object();
      lookupValue[0].id = records.ParentCustomerId.Id;
      lookupValue[0].name = records.ParentCustomerId.Name;
      lookupValue[0].entityType = "account";
      Xrm.Page.getAttribute("customerid").setValue(lookupValue);  //used to be 'new_endusercontact'
      
      var contact_TextFieldValue = Xrm.Page.getAttribute("new_endusercontact").getValue()[0].name;
      Xrm.Page.getAttribute("new_endusercontact_label").setValue(contact_TextFieldValue);
      
         var account_TextFieldValue = Xrm.Page.getAttribute("customerid").getValue()[0].name;
      Xrm.Page.getAttribute("new_customerid_label").setValue(account_TextFieldValue);
      }
     }
    }

    function setTextFieldLabel_VAR() {
     textFieldValue = Xrm.Page.getAttribute("new_varifapplicable").getValue()[0].name;
     Xrm.Page.getAttribute("new_varifapplicable_label").setValue(textFieldValue);
    }

    function setTextFieldLabel_EndUser() {
     textFieldValue = Xrm.Page.getAttribute("customerid").getValue()[0].name;
     Xrm.Page.getAttribute("new_customerid_label").setValue(textFieldValue);
    }

    Tuesday, July 15, 2014 5:58 PM

All replies

  • Hi

    Try to debug the code, you will easily get the line which is causing the error. Get that line, so that we can suggest you change.

    --

    Regards,

    Gopinath.

    Tuesday, July 15, 2014 6:14 PM
  • How to debug?
    Tuesday, July 15, 2014 6:18 PM
  • Try the following link..

    http://social.technet.microsoft.com/wiki/contents/articles/3256.how-to-debug-jscript-in-microsoft-dynamics-crm-2011.aspx

    You can just enable the debugging on the browser and debug it.

    --

    Regards,

    Gopinath.

    Tuesday, July 15, 2014 6:21 PM
  • It looks good but on CRM 2013 it triggers erors
    Tuesday, July 15, 2014 6:52 PM
  • Did you find the line causing the error in the javascript? If yes, please post that along with the exception message.

    Tuesday, July 15, 2014 6:56 PM
  • I followed the process and everything was good but on CRM 2013 there are error messages. according to the process followed no line is causing the error in the javascript

    Tuesday, July 15, 2014 6:59 PM
  • Can you just post the error message?
    Tuesday, July 15, 2014 7:00 PM
  • there are three different types:

    "There was an Error on the customized field

    Field: Crmform

    Event:onsave

    Error: Undefined"

    The second:

     

    "There was an Error on the customized field

    Field: new_endusercontact

    Event: onchange

    Error: Undefined"

    The third:

     

    "There was an Error on the customized field

    Field: window

    Event: onload

    Error: Undefined"

    Thanks for asking

    Tuesday, July 15, 2014 7:16 PM
  • try with this code:

    //+------When VAR Contact is selected, auto populate the VAR from the selected Contact's Parent Account
    function setVAR_Account() {
        var CRM_FORM_TYPE = Xrm.Page.ui.getFormType();
        if (CRM_FORM_TYPE != 1 && CRM_FORM_TYPE != 2) { return; }
        
        var varContact = Xrm.Page.getAttribute("new_varcontact").getValue();
        if(varContact != null && varContact[0].entityType == "contact") {
            //alert ("This opportunity is bound to a contact. The relationship will automatically change for the contact's parent account.");
            var idCustomer = varContact[0].id;
            var contactRequest = new XMLHttpRequest();
            var ODataPath = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc";
            contactRequest.open('GET', ODataPath+"/ContactSet(guid'" + idCustomer + "')", false);
            contactRequest.setRequestHeader("Accept", "application/json");
            contactRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            contactRequest.send();
            if (contactRequest.status == 200) {
                
                var contact = JSON.parse(contactRequest.responseText).d;
                var lookupValue = new Array(); 
                lookupValue[0] = new Object();
                lookupValue[0].id = contact.ParentCustomerId.Id;
                lookupValue[0].name = contact.ParentCustomerId.Name; 
                lookupValue[0].entityType = contact.ParentCustomerId.LogicalName;
                
                Xrm.Page.getAttribute("new_varifapplicable").setValue(lookupValue);
                Xrm.Page.getAttribute("new_varifapplicable_label").setValue(lookupValue[0].name);
                Xrm.Page.getAttribute("new_varcontact_label").setValue(varContact[0].name);
            } else {
                alert("Error with XMLHttpRequest");
            }
        }
    }
    
    //+------When End-User Contact is selected, auto populate the End-User from the selected End-User's Parent Account
    function setEndUser_Account() {
        
        var CRM_FORM_TYPE = Xrm.Page.ui.getFormType();
        if (CRM_FORM_TYPE != 1 && CRM_FORM_TYPE != 2) { return; }
        var endUser = Xrm.Page.getAttribute("new_endusercontact").getValue();
        if(endUser !=null && endUser[0].entityType == "contact") {
            //alert ("This opportunity is bound to a contact. The relationship will automatically change for the contact's parent account.");
            var idCustomer = endUser[0].id;
            var contactRequest = new XMLHttpRequest();
            var ODataPath = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc";
            
            contactRequest.open('GET', ODataPath+"/ContactSet(guid'" + idCustomer + "')", false);
            contactRequest.setRequestHeader("Accept", "application/json");
            contactRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            contactRequest.send();
            if (contactRequest.status == 200) {
                
                var contact = JSON.parse(contactRequest.responseText).d;
                var lookupValue = new Array(); 
                lookupValue[0] = new Object();
                lookupValue[0].id = contact.ParentCustomerId.Id;
                lookupValue[0].name = contact.ParentCustomerId.Name; 
                lookupValue[0].entityType = contact.ParentCustomerId.LogicalName;
                
                Xrm.Page.getAttribute("customerid").setValue(lookupValue);  //used to be 'new_endusercontact'
                Xrm.Page.getAttribute("new_endusercontact_label").setValue(endUser[0].name);
                Xrm.Page.getAttribute("new_customerid_label").setValue(lookupValue[0].name);
            } else {
                alert("Error with XMLHttpRequest");
            }
        }
    }
    
    function setTextFieldLabel_VAR() {
        var applicable = Xrm.Page.getAttribute("new_varifapplicable").getValue();
        if (applicable != null) {
            Xrm.Page.getAttribute("new_varifapplicable_label").setValue(applicable[0].name);
        }
    }
    
    function setTextFieldLabel_EndUser() {
        var customer = Xrm.Page.getAttribute("customerid").getValue();
        if (customer != null) {
            Xrm.Page.getAttribute("new_customerid_label").setValue(customer[0].name); 
        }
    }


    My blog: www.crmanswers.net - Rockstar 365 Profile

    Tuesday, July 15, 2014 7:24 PM
  • You are Jesus!!
    Tuesday, July 15, 2014 8:43 PM