How to work with Combined Filters of Fetch Function in Resco Mobile App

By | August 14, 2020

Introduction

Recently, we had a business requirement where we had to retrieve entity records by satisfying a combination of conditions in the script and perform operations on them in Resco Mobile App.

For example, on Account entity form we added a custom button. And on click of that button we wanted to retrieve active Work Order records associated with that Account having ‘System Status = Open – Unscheduled’ and ‘Work Order Type = Maintenance / Inspection’ and populate ‘Work Order Instructions’ on them from the Account record.

So first, we added a custom button ‘Process Work Orders’ on the Account entity form and then added a HTML File in ‘Offline HTML’ of Resco Project with below code snippet.

Code Snippet:

//Event of CustomCommand

MobileCRM.UI.EntityForm.onCommand(“custom_ProcessWorkOrders”, function (entityForm) {

//FetchXML Entity for retrieving Work Order records

var entity = new MobileCRM.FetchXml.Entity(“msdyn_workorder”);

entity.addAttribute(“msdyn_workorderid”);

entity.addAttribute(“msdyn_name”);

//Sub filter with AND clause

var subFilter1 = new MobileCRM.FetchXml.Filter();

subFilter1.type = “and”;

subFilter1.where(“msdyn_serviceaccount”, “eq”, entityForm.entity.id); //Service Account

subFilter1.where(“msdyn_systemstatus”, “eq”, 690970000); //Open – Unscheduled

subFilter1.where(“statecode”, “eq”, 0); //Active

//Sub filter with OR clause

var subFilter2 = new MobileCRM.FetchXml.Filter();

subFilter2.type = “or”;

subFilter2.where(“msdyn_workordertype”, “eq”, “ad2d8b0a-2fe7-e611-8110-e0071b66bf01”); //Inspection

subFilter2.where(“msdyn_workordertype”, “eq”, “69a69cba-bad3-ea11-a812-000d3a569dff”); //Maintenance

//Main Filter i.e. group of subfilters

var mainFilter = new MobileCRM.FetchXml.Filter();

mainFilter.type = “and”;

mainFilter.filters = [subFilter1, subFilter2]; //sub filters

//Add Main Filter in Entity Filter

entity.filter = mainFilter;

var fetch = new MobileCRM.FetchXml.Fetch(entity);

//fetching data of entity

fetch.execute(“DynamicEntities”, function (result) {

//validate result

if (typeof (result) != “undefined” && result.length > 0) {

//Here you can perform operation on the retireve records.

//You will get the retrieved records in the “result” object

}

}, function (error) {

MobileCRM.bridge.alert(“An Error Has occurred ” + error);

});

});

Note: We need to add all ‘sub filters’ in the ‘main filter’ and then associate that ‘main filter’ to the ‘FetchXML Filter’ to make it work.

Next, we added this Html file in the ‘Iframe’ of the Account entity form and published the Resco Project. Then after publishing Resco Project we synced the Resco Mobile App with that environment and navigated to the Account record. Next, we clicked on the ‘Process Work Orders’ for retrieveing and updating the Work Order records based on the conditions.

Now after the execution of this function, only the active Work Order records associated with that Account having ‘System Status = Open – Unscheduled’ and ‘Work Order Type = Maintenance / Inspection’ was retrieved and processed.

Please refer below screenshot:

Resco

Conclusion

In this way, instead of performing multiple Fetch Functions or Fetch XML for retrieving entity records of the same entity, we can use ‘Combined Filter’ in a single Fetch Function for retrieving the records of same entity based on multiple conditions.