Monday, May 7, 2012

CRM2011 oData: Retrieve Lookup and String field values using JS and REST Endpoints


CRM2011 oData: Retrieve Lookup, String field values using JS and REST Endpoints 

// JScript source code
function lookupvalidation() {
    var TechExpert = new Array();
    TechExpert = Xrm.Page.getAttribute("msft_technicalexpertiseid").getValue();

    if (TechExpert != null) {
        var name = TechExpert[0].name;
        var uniqueid = TechExpert[0].id;
        var entType = TechExpert[0].entityType;

        //Required ODataQuery
        var context = parent.Xrm.Page.context;
        var serverUrl = context.getServerUrl();
        var odataSelect = serverUrl + "/xrmservices/2011/OrganizationData.svc/msft_technologySet?$select=msft_CategoryId,msft_PandL&$filter=msft_technologyId eq guid'"
+ uniqueid + "'";
        //  alert(odataSelect);

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

                // Multiple Entities retrieval
                ProcessReturnedEntities(data.d.results);

            },
            error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + odataSelect); }
        });

    }

}

function ProcessReturnedEntities(ManyEntities) {
    // var AwardCategory = new Array();
    //AwardCategory = ManyEntities[0].msft_CategoryId;  

    var id = ManyEntities[0].msft_CategoryId.Id;
    var name = ManyEntities[0].msft_CategoryId.Name;
    var entityType = ManyEntities[0].msft_CategoryId.LogicalName;

    var AwardCategory = new Array();
    AwardCategory[0] = new Object();
    AwardCategory[0].id = id;
    AwardCategory[0].name = name;
    AwardCategory[0].entityType = entityType;

    var PL = ManyEntities[0].msft_PandL;

    //Assign AwardCategory based on Technical Experitise
     if (AwardCategory != null)
     Xrm.Page.getAttribute("msft_awardcategoryid").setValue(AwardCategory);


    //Assign Product&Language based on Technical Expertise
    if (PL != null)
        Xrm.Page.getAttribute("msft_pl").setValue(PL);

}

No comments:

Post a Comment