Category: Uncategorized

WEB API- Part 5 : Get lookup text

Get lookup text using WEB API

Following code will fetch the originating lead lookup text and Id from an opportunity .

var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/opportunities?$select=_originatingleadid_value&$filter=_originatingleadid_value ne null", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");
req.onreadystatechange = function () {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var results = JSON.parse(this.response);
            for (var i = 0; i < results.value.length; i++) {
                 var _originatingleadid_value = results.value[i]["_originatingleadid_value"];// Id
                 var _originatingleadid_value_formatted = results.value[i]["_originatingleadid_value@OData.Community.Display.V1.FormattedValue"];// text value
            }
        }
        else {
            alert(this.statusText);
        }
    }
};
req.send();

WEB API- Part 4 : Get option-set text

Get option-set text and value using Web API

Following code fetches lead source option-set value and text from lead entity.

var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/leads?$select=firstname,leadsourcecode&$filter=fullname eq 'Alex%20Eric'", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");
req.onreadystatechange = function () {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var results = JSON.parse(this.response);
            for (var i = 0; i < results.value.length; i++) {
                 var firstname = results.value[i]["firstname"];
                 var leadsourcecode = results.value[i]["leadsourcecode"];
                 var leadsourcecode_formatted = results.value[i]["leadsourcecode@OData.Community.Display.V1.FormattedValue"];
            }
        }
        else {
            alert(this.statusText);
        }
    }
};
req.send();

Please note the following code

" var leadsourcecode_formatted = results.value[i]["leadsourcecode@OData.Community.Display.V1.FormattedValue"];"

“fields name@OData.Community.Display.V1.FormattedValue” will give you the text value of the filed.

Note : Don’t forget to add req.setRequestHeader(“Prefer”, “odata.include-annotations=\”OData.Community.Display.V1.FormattedValue\””);
in the request header

WEB API- Part 3 : Retrieve records modified within last few hours

Using Query Functions:  Retrieve records modified within last few hours 

Query Functions  are intended to be used to compose a query. These functions can be used in a manner similar to the standard library functions,

The following example shows how to use the “LastXHours” to return all account entities modified in the past 12 hours.

var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() +/api/data/v8.1/accounts?$select=name,accountnumber&$filter=Microsoft.Dynamics.CRM.LastXHours(PropertyName=@p1,PropertyValue=@p2)&@p1='modifiedon'&@p2=12

For complete list of query functions please follow the link Query functions.

 

WEB API- Part 2: Retrieve Single Record Using WEB API

Retrieve Single Record Using WEB API

Following  code shows how to fetch an account record using  Guid

var req = new XMLHttpRequest();

var AcccountID="257bAC5D5240-8B31-E611-80E4-5065F38B4681";
req.open("GET", Xrm.Page.context.getClientUrl() +"/api/data/v8.0/accounts(AcccountID)?$select=name", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var result = JSON.parse(this.response);
var name = result["name"];
}
else {
alert(this.statusText);
}
}
};
req.send();

Let’s Play with WEB API – Part -1 : Retrieve Multiple Records

Retrieve Multiple  Records using Web API

The Web API is a newly introduced open standard endpoint currently available in CRM 2016  at [organization uri]/api/data/v8.0/ which implements ODATA v4 and allows you the full blown functionality that you would typically experience when interacting through the CRM Organisation Service.

The Web API service is said to eventually replace the existing Organisation and Organisation Data service and MS recommend that if you writing new code that you begin using the Web API endpoint as the existing ODATA REST endpoint is officially deprecated but still available for backward compatibility.

The new API has introduced some impressive functionality that was not available to us previously with the existing ODATA service. Some of that functionality includes:

  • Ability to execute saved queries
  • Ability to execute FetchXML
  • Use Queries Up to Second Level

Following code shows how to retrieve records using web API, I have tested to retrieve 2000+ records and it is working fine.

This code alerts the number of contacts under an Account entity.

function AccountOnLoad()

{

    var Id = Xrm.Page.data.entity.getId().substring(1, 37);

    var entity = "contacts";

 var columnSet = "?$select=firstname&$filter=_accountid_value eq " + Id + "&$count=true";

    var options = columnSet;

  

    var serverURL = Xrm.Page.context.getClientUrl();

    var Query = entity + options;

    var req = new XMLHttpRequest();

    req.open("GET", serverURL + "/api/data/v8.0/" + Query, true);

    req.setRequestHeader("Accept", "application/json");

    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

    req.setRequestHeader("OData-MaxVersion", "4.0");

    req.setRequestHeader("OData-Version", "4.0");

    req.onreadystatechange = function () {

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

            req.onreadystatechange = null;

            if (this.status == 200) {

                var data = JSON.parse(this.response);

                if (data['@odata.count'] != null)

                    alert("This Account Has" + data['@odata.count'] + "Contacts");

            } else {

                var error = JSON.parse(this.response).error;

                alert(error.message);

            }

        }

    };

    req.send();

}

 

There are many other features too which I will be demonstrating in the upcoming posts.

 

Field Conflicts

Recently I faced a strange issue, I used a filed (lookup) twice in the form and applied a pre-filtering java-script to the field. But only one filed is getting filtered by the script.I tried many options and I couldn’t identify the issue. Have you even faced  this issue ? Finally I got the answer from  Scott Durow ( Thank you Scott!!! ). I decided to post this issue because if someone is facing the same issue , this will help them to solve the issue. Following is the explanation for this issue.

The issue you are experiencing is because the ‘getControl’ method returns only the first control – so in fact you are only adding the custom filter to the first control.

An attribute object has a ‘controls’ collection that can give you all the controls for a specific attribute. So you can add the filter to all the controls by using something like:

Xrm.Page.getAttribute(“fieldname”).controls.forEach(

function (control, i) {

control.addPreSearch(filterCustomerAccounts);

var customerAccountFilter = “<filter type=’and’><condition attribute=’accountcategorycode’ operator=’eq’ value=’1’/></filter>”;

control.addCustomFilter(customerAccountFilter, “account”);

}

);

Hope this helps!!!

Nijo

 

 

Document Template in CRM 2016

Dear all ,

End-users will now be able to generate documents such as contracts, quotes, orders, invoices and other template (Word or Excel) documents with a single click. Templates can be generated and pre-defined using a wizard. You do not need to depend on other add-ons or build custom reports anymore!

Following steps explain step by step process of word document generation from CRM.

Create a Template

Go to Settings > Templates > Document Templates > New ()

OR

Open a record such as an account in Sales. Go to Sales > Accounts. Open an account and click MoreWord Templates > Create Word Template.

Picture1

Select Primary Entity

Now  you will be asked to select the primary entity and required relation ships from the entity

Picture2

Download Template

Click Download Template on the Select Entity page to download the template with embedded xml .Now the template is ready for field mapping , you can do the required formatting’s and mappings on the template.

You need to enable the developer tab in word to view the XML mapping pane.To enable the tab Go to File > Options > Customize Ribbon, and then enable Developer.

Picture4.png

Now the developer tab will be available in the Toolbar.

Picture5Picture6

Design  your Template

Picture8.png

Picture9

Once required fields are mapped you can upload the template back to CRM.

Go to Settings > Templates > Document Templates > Upload Template.

Run Template

Once the template is uploaded you can run it from the related entity.

Picture10.png

Hope this helps you to generate documents directly from CRM.

This is my first blog post, Please update your suggestions and comments!!!!!!!!!!!!!!!