Dynamically filter the lookup on Field Service Mobile App

Sending
User Review
0 (0 votes)

The Field Service Mobile app helps organizations to deliver services to customer locations. It provides the ability to work on the Bookings by the Technician i.e. Bookable Resource.

This application includes various features to automate the process so that technicians can work efficiently and flawlessly. In this blog, we will walk through the ability to dynamically filter the lookup in the Field Service Mobile app using updateLinkItemViews function.

Consider this example: we have to filter out Company Name lookup on Contact based on City field value. Therefore, whenever the City on the Contact form is updated, Company Name lookup should be filtered out with the Accounts containing City as City of the Contact record.

Please find the below steps to follow to achieve our considered request:

  1. Customize contact form to include the Company Name (parentcustomerid) and City fields as shown in the below screenshot.Dynamically filter the lookup on Field Service Mobile App
  2. Add HTML and script within Offline HTML section, this includes the logic to bind the dynamic filters to the lookup on the form.

    AccountFilter.HTML: This html includes reference to the Javascript file as we can add only HTML file on the form. Below is the code snippet for the same.

    <!DOCTYPE html>

    <html xmlns=”http://www.w3.org/1999/xhtml”>

    <head>

    <title></title>

    <script type=”text/javascript” src=”JSBridge.js”></script>

    <script type=”text/javascript” src=”JSBridgeExtension.js”></script>

    <script type=”text/javascript” src=”Schema.js”></script>

    <script type=”text/javascript” src=”Common.js”></script>

    <script type=”text/javascript” src=”Enums.js”></script>

    <script type=”text/javascript” src=”Telemetry.js”></script>

    <script type=”text/javascript” src=”FilterAccount.js” ></script>

    </head>

    <body onload=”AccountFilter();”>

    </body>

    </html>

    FilterAccount.js:  This includes the business logic for filtering the Company Name lookup based on the City value entered on the Contact Form. Below is the code snippet for the same.

    function AccountFilter() {

    try {

    // Change the parent customer on currently edited contact entity

    MobileCRM.UI.EntityForm.onChange(function (entityForm) {

    if (entityForm.context.changedItem != “address1_city”) return;

    MobileCRM.UI.EntityForm.requestObject(function (entityForm) {

    // Take detail view with name “General”

    var detailsView = entityForm.getDetailView(“General”);

    var inlineSetup = new MobileCRM.UI.DetailViewItems.LookupSetup();

    var City = entityForm.entity.properties.address1_city;

    inlineSetup.addView(“account”, “Default”, true);

    // Alternatively, specify the explicit fetch XML filter for inline lookup

    inlineSetup.addFilter(“account”, ‘<fetch version=”1.0″><entity name=”account”><attribute name=”name” /><attribute name=”primarycontactid” /><attribute name=”telephone1″ /><attribute name=”accountid” /><order attribute=”name” descending=”false” /><filter type=”and <condition attribute=”address1_city” operator=”eq” value=”‘ + City + ‘” /> <condition attribute=”statecode” operator=”eq” value=”0″ /></filter></entity></fetch>’);

    dialogSetup = new MobileCRM.UI.DetailViewItems.LookupSetup();

    dialogSetup.addView(“account”, “Default”, true);  // Set “Default” view for expanded lookup form

    dialogSetup.addFilter(“account”, ‘<fetch version=”1.0″><entity name=”account”><attribute name=”name” /><attribute name=”primarycontactid” /><attribute name=”telephone1″ /><attribute name=”accountid” /><order attribute=”name” descending=”false” /><filter type=”and <condition attribute=”address1_city” operator=”eq” value=”‘ + City + ‘” /> <condition attribute=”statecode” operator=”eq” value=”0″ /></filter></entity></fetch>’);

    dialogOnly = false; // Allow both inline lookup and expanded lookup dialog

    // Allow both inline lookup and expanded lookup dialog

    detailsView.updateLinkItemViews(0, dialogSetup, inlineSetup, dialogOnly);

    }, null, null);

    },

    function (error) {

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

    },null);  }

    catch (err) {

    MobileCRM.bridge.alert(err);

    }

    }

  3. Once completed with adding offline html, we need to include the AccountFilter.html file on the Contact Form by adding an Iframe as shown in the below screenshot.
    Dynamically filter the lookup on Field Service Mobile App
  4. Save and close the form and Publish the Mobile project to reflect the customizations in the Field Service Mobile App.
  5. Now Sync the Field Service Mobile app and navigate to the Contact record. Change the City on the contact and click on the Company Name lookup. This will filter and display the accounts containing the City as that of the City of the contact.As shown in the below screenshot, we have added City as Clinton then the Company Name lookup will filter the accounts containing the City as Clinton.Dynamically filter the lookup on Field Service Mobile App

Conclusion

Field Service provides us the flexibility to achieve our business requirements by filtering lookups using updateLinkItemViews function within Field Service Mobile App.