Thursday, September 13, 2012

Using Odata, Json in CRM 2011

I had this requirement from client who wanted to pull value from a field in one entity A to another field in another  entity B.
Used Odata services with JQuery and Json to bring about the result.

function SelectFee() {
    var context = Xrm.Page.context;
    var serverUrl = context.getServerUrl();
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
    var CRMObject = new Object();
    /////////////////////////////////////////////////////////////
    // Specify the ODATA entity collection
       //TODO get the GUID of the fee selected
    var speakerFeeLookup = Xrm.Page.getAttribute("wsb_speakerfee").getValue();
    if (speakerFeeLookup == null) return;
    var speakerFeeId = speakerFeeLookup[0].id;
    var ODATA_EntityCollection = "/wsb_feeSet?$select=wsb_TotalFee&$filter=wsb_feeId eq guid'" + speakerFeeId + "'";
    /////////////////////////////////////////////////////////////
    // Define attribute values for the CRM object you want created
    //Asynchronous AJAX function to Create a CRM record using OData
    $.ajax({ type: "GET",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection,
        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) {
           
            var defaultFee = data["d"].results;
           defaultfee1 = parseInt(defaultFee[0].wsb_TotalFee);
           

            Xrm.Page.getAttribute('wsb_deffee').setValue(defaultfee1);
            Xrm.Page.getAttribute('wsb_proposedprice').setValue(defaultfee1);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("failure: " + errorThrown);
        }
    });
}

No comments:

Post a Comment