Wednesday 3 October 2018

Retrieve Option set value from web API in Dynamic CRM

Get optionset text from OData query in CRM web api

Description:

In this example, we explain that how to get the Option Set Value and Text in jQuery Ajax call success method result in Dynamic CRM.or how to retrieve Option set value from web api in Dynamic CRM.or retrieve the option set values for collection attribute in Dynamic CRM.

So here we explain that how to get option set text and value from OData query in Dynamic CRM web api.so below is the OData query that are useful to get the Option set value in Dynamic CRM.
Code:

function GetUnitsBasedonAccount(investmentId, opportunityaccountId) {
         
           var oDataUri = Xrm.Page.context.getClientUrl() + "/xrmservices/2011/OrganizationData.svc/new_investmenttransactionSet?$filter=new_ParentInvestment/Id eq Guid'" + investmentId.replace("{", "").replace("}", "") + "' and new_OpportunityAccount/Id eq Guid'" + opportunityaccountId.replace("{", "").replace("}", "") + "'  &$select=new_Units,new_TransactionType,new_TransactedAmount,new_UnitPrice,new_Status";
         
           $.ajax({
               type: "GET",
               async: false,
               contentType: "application/json; charset=utf-8",
               datatype: "json",
               url: oDataUri,
               beforeSend: function (XMLHttpRequest) {
                   XMLHttpRequest.setRequestHeader("Accept", "application/json");
               },
               success: function (data, textStatus, XmlHttpRequest) {
                   var Unit = 0;

                   var totalUnitEngagedAccountwise = Xrm.Page.getAttribute("new_unitengaged").getValue();
                   if (data.d.results.length > 0) {
                       for (var i = 0; i < data.d.results.length; i++) {

                           if (data.d.results[i].new_TransactionType.Value == '100000002' && data.d.results[i].new_Status.Value == '100000001') // get values of Optionset and check it
                           {
                               Unit = Unit + Number(data.d.results[i].new_Units);

                           }
                       }
                   }
                   var FinalEngagedUnitAccountwise = totalUnitEngagedAccountwise - Unit;
                   Xrm.Page.getAttribute("new_units").setValue(FinalEngagedUnitAccountwise);

               },
               error: function (XMLHttpRequest, textStatus, errorThrown) { }
           });
       }



0 comments:

Post a Comment