Dynamics 365





Saturday, December 14, 2013

CRM 2011 - Auto populate values on lookup value selected - json ODATA

On Lookup up change get Account Entity values get auto populated using ODATA JSON script

function retrieveRecord() {

    var odataSetName = "AccountSet";
    debugger;
    var accountLookup = Xrm.Page.getAttribute("pmax_companyname1").getValue();
    if (accountLookup != null) {
        var AccountID = accountLookup[0].id;
    }
    // Get Server URL

    var serverUrl = Xrm.Page.context.getServerUrl();

    var serverUrl = "http://117.218.28.36/pmax-crm";

    //The OData end-point

    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";

    //Asynchronous AJAX function to Retrieve a CRM record using OData
    debugger;
    $.ajax({

        type: "GET",

        contentType: "application/json; charset=utf-8", datatype: "json",

        url: serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "(guid'" + AccountID + "')",

        beforeSend: function (XMLHttpRequest) {

            //Specifying this header ensures that the results will be returned as JSON.

            XMLHttpRequest.setRequestHeader("Accept", "application/json");

        },

        success: function (data, textStatus, XmlHttpRequest) {

            readRecord(data, textStatus, XmlHttpRequest)

        },

        error: function (XmlHttpRequest, textStatus, errorThrown) {

            alert("Error – " + errorThrown)

        }

    });

}

    function readRecord(data, textStatus, XmlHttpRequest) {

    alert("Record read successfully!!");

    var account = data;

    alert("Name – " + account.d.Telephone1);
    alert("Name – " + account.d.WebSiteURL);

    Xrm.Page.getAttribute("pmax_website").setValue(account.d.WebSiteURL);
    Xrm.Page.getAttribute("pmax_website").setSubmitMode("always");

    Xrm.Page.getAttribute("pmax_businessphone").setValue(account.d.Telephone1);
    Xrm.Page.getAttribute("pmax_businessphone").setSubmitMode("always");
  

    }

No comments:

Post a Comment