locked
MS Dynamics CRM 2016 Online Javascript Error RRS feed

  • Question

  • Hi,

    I have applied below javascript code on change of "Account Name" field of contact entity but I ma facing below error message on change of Account Name field.

    One of the scripts for this record has caused an error. For more details, download the log file.
    ReferenceError: OnAttributeChangeParentCustomer is not defined at eval (eval at RunHandlerInternal (organisation), <anonymous>:1:1)

    Javascript code is as below :

    function OnAttributeChangeParentCustomer(){
    var parentCustomerLookup = Xrm.Page.getAttribute("parentcustomerid").getValue();

    if (parentCustomerLookup == null) {
    var deleteAddress = confirm("You have deleted the Parent Customer from this contact record. Do you want to delete the contact's address as well?");
    if (deleteAddress == true){
    this.UserConfirmsToDeleteAddress();
    }
    }

    if (parentCustomerLookup != null && parentCustomerLookup[0].entityType == "account") {
    var accountId = parentCustomerLookup[0].id;
    var accountName = parentCustomerLookup[0].name;
    var copyAddress = confirm("If parent account \"" + accountName + "\" has address information, do you want to overwrite this contact address with account address?");
    if (copyAddress == true) {
    this.UserConfirmsToCopyAddress(accountId);
    }
    }

    },

    ----------------------------User confirms to delete address---------------------------------

    function UserConfirmsToDeleteAddress(){

    //Delete contact address information
    Xrm.Page.getAttribute("address1_city").setValue(null);
    Xrm.Page.getAttribute("address1_country").setValue(null);
    Xrm.Page.getAttribute("address1_line1").setValue(null);
    Xrm.Page.getAttribute("address1_line2").setValue(null);
    Xrm.Page.getAttribute("address1_line3").setValue(null);
    Xrm.Page.getAttribute("address1_stateorprovince").setValue(null);
    Xrm.Page.getAttribute("address1_postalcode").setValue(null);

    //Saves the record synchronously
    Xrm.Page.data.entity.save();
    },

    ------------------User confirms to copy address-------------------------------

    function GetAccount(accountId, successCallback, errorCallback) {

    //OData URI to get address information from parent account record
    var oDataURI = Xrm.Page.context.getClientUrl()
    + "/XRMServices/2011/OrganizationData.svc/"
    + "AccountSet(guid'" + accountId + "')"
    + "?$select="
    + "Address1_City,"
    + "Address1_Country,"
    + "Address1_Line1,"
    + "Address1_Line2,"
    + "Address1_Line3,"
    + "Address1_StateOrProvince,"
    + "Address1_PostalCode";

    //Asynchronous XMLHttpRequest to retrieve account record
    var req = new XMLHttpRequest();
    req.open("GET", encodeURI(oDataURI), true);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.onreadystatechange = function () {
    //debugger;
    if (this.readyState == 4 /* complete */) {
    req.onreadystatechange = null; //avoids memory leaks
    if (this.status == 200) {
    //parse the response string as a JSON object into the successCallback method.
    successCallback(JSON.parse(this.responseText).d);
    }
    else {
    errorCallback(accountId);
    }
    }
    };
    req.send();
    },


    function CopyAccountAddressToContactAddress(account) {

    //Set contact address information
    Xrm.Page.getAttribute("address1_city").setValue(account.Address1_City);
    Xrm.Page.getAttribute("address1_country").setValue(account.Address1_Country);
    Xrm.Page.getAttribute("address1_line1").setValue(account.Address1_Line1);
    Xrm.Page.getAttribute("address1_line2").setValue(account.Address1_Line2);
    Xrm.Page.getAttribute("address1_line3").setValue(account.Address1_Line3);
    Xrm.Page.getAttribute("address1_stateorprovince").setValue(account.Address1_StateOrProvince);
    Xrm.Page.getAttribute("address1_postalcode").setValue(account.Address1_PostalCode);

    //Saves the contact record asynchronously with callback methods.
    Xrm.Page.data.save().then(
    function () {
    var msg2 = "Successfully copied and saved contact record.";
    //display a non-blocking alert dialog
    Xrm.Utility.alertDialog(msg2, function () { });
    },
    function () {
    var msg3 = "PT.Contact_Main.CopyAccountAddressToContactAddress Error: Cannot save contact form with new parent account informatin. Please check contact and account records.";
    //display a non-blocking alert dialog
    Xrm.Utility.alertDialog(msg3, function () { });
    }
    );
    },


    function GetAccountErrorCallback(id){

    var msg1 = "PT.Contact_Main.GetAccount() Error: unable to retrieve account record with accountId = " + id + ".";
    //display a non-blocking alert dialog
    Xrm.Utility.alertDialog(msg1, function(){});

    },

    Saturday, November 19, 2016 4:50 PM