Thursday, 5 March 2015

21:18 - No comments

Adding a lookup field to select a record and display record details of the same record in the same form by using JavaScript (OData). | MS CRM

Adding a lookup field to select a record and display record details of the same record in the same form by using JavaScript (OData).

Step 1:
Create a lookup field in the form in which the details should be displayed.

Step 2:
Create necessary fields to get and display record details.

Step 3:
Add the JavaScript code given below to the form properties and set the function on change of the lookup field

Eg: Here I am fetching information from lead entity



function getdetail()
{
debugger;
    var lookUpObjectValue = Xrm.Page.getAttribute("lookup field name ").getValue();
    if ((lookUpObjectValue != null))
    {
        var lookuptextvalue = lookUpObjectValue[0].name;
         var lookupid = lookUpObjectValue[0].id;
         var serverUrl = Xrm.Page.context.getClientUrl();
    //The XRM OData end-point
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
    var odataSetName = "leadSet"; //which entity looking for (eg: leadSet or contactSet)
    var odataSelect = serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "(guid'" + lookupid + "')";

    $.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) {

            var result= data.d;
                                //setting the to the current fields
                        Xrm.Page.getAttribute("current field name ").setValue(result.LeadFieldname1);                                             
          Xrm.Page.getAttribute("current field name ").setValue(result.LeadFieldname2)
        },
        error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + odataSelect); }
    });

    }

   }

0 comments:

Post a Comment