29 Nov’17

Dynamics 365, EnterpriseRetrieve Multiple Records using Web API in Dynamics 365 version 9.0


In this blog article, we will be showing how use fetch XML to retrieve multiple records with the new Web API in Dynamics 365 version 9.0


Step 1: The retrieveMultipleRecords() method retrieves a collection of entity records.

The basic syntax is as follows:

Xrm.WebApi.retrieveMultipleRecords(entityLogicalName,options,maxPageSize).then(successCallback, errorCallback);

Here the options parameter refers to the query that will decide the data which has to be retrieved from the system. We will be using the fetchXml attribute  to specify a FetchXML query  to retrieve contacts of a specific account.

The maxPageSize indicates number of records to be returned per page. If this is not specified the default value is 5000. In this example we have not specified the maxPageSize.

Step 2:  First we write the code and upload it as a JavaScript web resource.


var scripting = {
retrieveMultipleContacts(executioncontext) {
var formContext = executioncontext.getFormContext();
var accountId = formContext.data.entity.getId();
var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'><entity name='contact' ><attribute name='fullname' /><attribute name='telephone1' /><attribute name='contactid' /><order attribute='fullname' descending='false' /><filter type='and'><condition attribute='parentcustomerid' operator='eq' uitype='account' value ='" + accountId + "' /></filter></entity ></fetch > ";
Xrm.WebApi.retrieveMultipleRecords("contact", "fetchXml= " + fetchXml).then(
function success(result) {
for (var i = 0; i < result.entities.length; i++) {
function (error) {

Here we take the execution context as the input parameter and we get the form context using the getFormContext() method. This method returns a reference to the form or an item on the form.

Using the formContext we get get the account id which is used to fetch the contacts of that specific account.

Step 3: On the account form, in  the form properties we set the Event to OnSave as shown below.

Step 4: In the handler properties we set the function name, in our case it is scripting.retrieveMultipleContacts.

And it is important to check the “Pass execution context as the first parameter” checkbox as shown below.

Step 5: We see that the account A. Datum Corporation (sample) has two contacts.

Step 6:  The script runs when the form is saved and while debugging we can see in the console, two contacts are returned in the results. We get the the attributes that were present in the FetchXML query.

Hope this article was helpful!

Written by

Clinton D'Mello

Trainee Software Engineer

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.

Want to streamline your business processes?

  • This field is for validation purposes and should be left unchanged.

Recent Articles

  • Managing mailbox through Email Archiving

    20 March’ 2018

    Introduction: Email can fill your Outlook Inbox quickly – new messages, replies, and forwards. Before you know it, yo...

    Read more
  • Workaround to Report.SAVEASPDF in NAV 2018

    20 March’ 2018

    Objective: In NAV 2017 Emailing the PDF by running the report using Report.SAVEASPDF is now not allowed in NAV 2018. Th...

    Read more
  • Dynamics 365 Client Diagnostics

    15 March’ 2018

    Main cause of Performance Issues: Bandwidth and latency are the primary characteristics which affects the performance o...

    Read more
  • Closing Blind Shift in MPOS and CPOS in Dynamics 365 for Retail

    15 March’ 2018

    Manual For closing Blind shift in POS: Blind shifts terminate the currently ongoing shift and when logged in a new shif...

    Read more
  • Persistent Filters in the Power BI Service

    13 March’ 2018

    Introduction: The feature is finally release, and it is power BI has announced general availability of persistent filt...

    Read more