Pages

Tuesday 11 February 2014

On Selecting one lookup field another lookup field sets automatically

Hi Folks,
Client had a requirement in which if we select one lookup field then the another lookup field must get set automatically. for doing this we have three entities. Lets call A, B and C as three different entities. A entity has a lookup of B entity, so A entity is mapped to B entity. Now C entity has a lookup of A and lookup of B, so here the requirement is that if we select a record in A lookup then the B lookup must set automatically(Since A entity and B entity are mapped by a lookup).

For doing these we can use OData Query. You can download the Odata Query Designer from above link

http://crm2011odatatool.codeplex.com/

The above snippet code can be used for doing these operations
------------------->
function customentity_onchange() {

debugger;
    var lookupObject = Xrm.Page.getAttribute("A_type lookup fied schema name").getValue();
    var z=lookupObject[0].id; // Guid of lookup
    if (z != null) {
        var entityA="A_entitynameSet";            //Entity name of A and "Set" is used in Odataquery           
        var field = "B-type lookup schema name in A entity";        //Schema name of a lookup name in A entity
        var a = GetAdata(z,entityA,field);               //calling function to get the details
        if(a!=null)
        {
        alert(a[0]["B-type lookup schema name in A entity].Name);
        var lookupData = new Array();
        var lookupItem = new Object();
        lookupItem.id=a[0]["B-type lookup schema name in A entity"].Id;
        alert(lookupItem.id);
        lookupItem.name = a[0]["B-type lookup schema name in A entity"].Name;
        alert(lookupItem.name);
        lookupItem.entityType ="B-Entity Schema name";
        alert(lookupItem.entityType);
        lookupData[0] = lookupItem;
        Xrm.Page.getAttribute("B type lookup field schema name in C entity").setValue(lookupData);
             }
        }
    }
function GetAdata(z,entityA,field) {
    var serverUrl = Xrm.Page.context.getServerUrl();
    var oDataUri = serverUrl + "/XRMServices/2011/OrganizationData.svc/"+entityA+"(guid'" + z + "')?$select="+field;                  //retrieving B type lookup field details of a particular record using GUID
    var jSonArray = new Array();
    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: oDataUri,
        async: false,
        beforeSend: function (XMLHttpRequest) {
            //Specifying this header ensures that the results will be returned as JSON.           
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
            //alert(hai);
        },
        success: function (data, textStatus, XmlHttpRequest) {
            if (data && data.d != null) {
                jSonArray.push(data.d);
            }
        },
        error: function (XmlHttpRequest, textStatus, errorThrown) {
        alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown);
            //alert("Error :  has occured during retrieval of the A details");
    }
    });

    return jSonArray;
}
--------------->

No comments:

Post a Comment