Wednesday, July 6, 2011

Retrieving Customer Address from parent Account on Contact Form using ODATA

Hi All, Here I am going to retrieve Parent Account Address on contact form using Odata in crm 2011

// Populate Customer Address from Parent Account
function PopulateCustomerAddress(ParentAccount) {

    if (ParentAccount != null) {
        var AccountId = ParentAccount.getValue()[0].id;

        // Pass odataQuery
        var odataQuery = "AccountSet?$select=Address1_Fax,Address1_Line1,Address1_Line2,Address1_StateOrProvince,Address1_Telephone1,Address1_Telephone2,new_regionid&$filter=AccountId eq guid'" + AccountId + "'";

        retrieveRecord(odataQuery, retrieveCustomerAddressCompleted, null);
    }
 
}


// Retrieve Record Details based on the odataQuery
function retrieveRecord(odataQuery, successCallback, errorCallback) {
    var context = Xrm.Page.context;
    //Retrieve the server url,
    serverUrl = context.getServerUrl();
    //The XRM OData end-point
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";

    //odataQuery is required, i.e. "AccountSet/guid('')"
    if (!odataQuery) {
        alert("odataQuery is required.");
        return;
    }

    //Asynchronous AJAX function to Retrieve a CRM record using OData
    $.ajax({
        type: "GET",
        async: false,      
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: serverUrl + ODATA_ENDPOINT + "/" + odataQuery,
        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) {
            if (successCallback) {
                successCallback(data.d, textStatus, XmlHttpRequest);
            }
        },
        error: function (XmlHttpRequest, textStatus, errorThrown) {
            if (errorCallback)
                errorCallback(XmlHttpRequest, textStatus, errorThrown);
            else
                errorHandler(XmlHttpRequest, textStatus, errorThrown);
        }
    });
}

//Called upon successful Retrieval of Customer Address.
function retrieveCustomerAddressCompleted(data, textStatus, XmlHttpRequest) {

    //Get back the Account JSON object
    var Account = data.results[0];
    if (Account != null) {
        Xrm.Page.getAttribute("address1_fax").setValue(Account.Address1_Fax);
        Xrm.Page.getAttribute("address1_line1").setValue(Account.Address1_Line1);
        Xrm.Page.getAttribute("address1_line2").setValue(Account.Address1_Line2);
        Xrm.Page.getAttribute("address1_stateorprovince").setValue(Account.Address1_StateOrProvince);
        Xrm.Page.getAttribute("address1_telephone1").setValue(Account.Address1_Telephone1);
        Xrm.Page.getAttribute("address1_telephone2").setValue(Account.Address1_Telephone2);
        
             // setting lookup field
               if (Account.new_regionid != null && Account.new_regionid .Id != null)
            SetLookupValue("new_region", Account.new_regionid .Id, Account.new_regionid .Name, Account.new_regionid .LogicalName);
    }
    
}

// Set lookup value to a field
function SetLookupValue(fieldName, id, name, entityType) {
    if (fieldName != null) {
        var lookupValue = new Array();
        lookupValue[0] = new Object();
        lookupValue[0].id = id;
        lookupValue[0].name = name;
        lookupValue[0].entityType = entityType;

        Xrm.Page.getAttribute(fieldName).setValue(lookupValue);
    }
}

Dont forget to add JSON and JQuery libraries to the form. you can find it in my previous posts click here

1 comment:

  1. Hi Guru,



    I am new to CRM and I have a question on how to auto populate text from lookup value.

    I went through many forum, and got solution for 2 entites, but i am looking for 4 entities mentioned below:



    In our CRM instance, there are 4 entities mentioned below:

    Case

    Location

    Finance Region Location

    Finance Region

    Case has N:1 Relationship with Location and Finance Region.

    Location has 1:N relationship with Finance Region Location.

    Finance Region Location has N:1 relationship with Finance Region.

    When user select one value in Location lookup in case form, Finance Region should be auto populate

    for example: if user enter asia as location, in finance region field(Read only text box) APAC should be autopopulate.



    Could you please let me know help to write javascript code to autopopulate Finance Region text box Please help me out
    Thanks in advance

    ReplyDelete