Thursday, 19 May 2016

Query Data using the Web API

In the below function RetrieveParentAccount() i want to  show how to retrieve string, option Set, lookup and  to Two Option fields
I have test the   RetrieveParentAccount() function on form OnLoad of contact entity.


  function RetrieveParentAccount() {
   
    var accountLookup = Xrm.Page.getAttribute("parentcustomerid");
    if (accountLookup != null) {
        var accountLookupValue = accountLookup.getValue();
        var accountGuid = accountLookupValue[0].id;
        accountGuid = accountGuid.replace("{", "");
        accountGuid = accountGuid.replace("}", "");

        //name : is single line of text field
        //primarycontactid : is Lookup field
        //preferredcontactmethodcode: is optionset field
        //donotemail : is two option field

        //filter by lookup field
        // var filter = "&$filter=_primarycontactid_value eq 0F7AFC02-891C-E611-80E1-5065F38B0371";

        //filter byOptionset field
        //var filter = "&$filter=preferredcontactmethodcode eq 2";

        //filter TwoOption field
        var filter = "&$filter=donotemail eq false";
        var orderBy = "&$orderby=name asc";
        var expand = "&$expand=incident_customer_accounts($select=title)";

        //var query = "accounts?$select=name,_primarycontactid_value,preferredcontactmethodcode,donotemail" + filter + orderBy + expand ;

        var query = "accounts(" + accountGuid + ")?$select=name,_primarycontactid_value,preferredcontactmethodcode,donotemail" + expand;
        var webapiPath = Xrm.Page.context.getClientUrl() + "/api/data/v8.0/" + query;

        var service = new XMLHttpRequest();
        service.open("GET", webapiPath, false);
        service.setRequestHeader("Accept", "application/json");
        service.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        service.setRequestHeader("OData-MaxVersion", "4.0");
        service.setRequestHeader("OData-Version", "4.0");
        service.setRequestHeader("Prefer", "odata.include-annotations=*"); 
        //service.setRequestHeader("Prefer", "odata.maxpagesize = 3"); this tag isused to retrieve no of records
        service.send();

        if (service.readyState == 4 /* complete */) {

            if (service.status == 201 || service.status == 200) {

                var result = eval('(' + service.responseText + ')');
                var name = result.name; // extract single line of text field
                var lookupGuid = result._primarycontactid_value; // extract Look up guid
                var lookupEntityName = result['_primarycontactid_value@Microsoft.Dynamics.CRM.lookuplogicalname']; //extract look up logical name
                var lookupName = result['_primarycontactid_value@OData.Community.Display.V1.FormattedValue']; // extract lookup text
                var booleanFieldValue = result.donotemail; // boolean field
                var booleanFieldText = result['donotemail@OData.Community.Display.V1.FormattedValue']; // boolean field text
                var optionsetValue = result.preferredcontactmethodcode; // extract optionSetValue
                var optionsetText = result['preferredcontactmethodcode@OData.Community.Display.V1.FormattedValue'];//extract optionset text
                var releatedEntityCaseTitle = result.incident_customer_accounts[0].title;
            }
        }
    }
}  

4 comments:

  1. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Big Data Hadoop and Spark Developer, kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on MS Dynamic CRM. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us.
    Sangita Mohanty
    MaxMunus
    E-mail: sangita@maxmunus.com
    Skype id: training_maxmunus
    Ph:(0) 9738075708 / 080 - 41103383
    http://www.maxmunus.com/


    ReplyDelete
  2. Thanks and Welcome Naseela

    ReplyDelete
  3. Hi syed, i have doubt the last line of expand value i am unable to get .. can you please explain how to get expand values.

    ReplyDelete
  4. Apply online for Microsoft Recruitment. Get the information about the current and upcoming vacancies here.

    ReplyDelete

Read only field , get latest value from read only field in in power app portal

Below line is used to make field readonly in power app portal through js $( "#bdo_relationshiprole_name" ).parent().css( "po...