D365 Omnichannel Filter Form

D365 Omnichannel formulario

Recently a version of Omnichannel has been updated, in which the Omnichannel form has changed drastically, allowing it to be edited quickly thanks to a new Microsoft PCF. The problem we had so far in our projects is that we could not filter this field in a standard way, as the PCFs did not correctly recognise the parameter that we included in the field.

Previously the form looked like this:

It is now shown as follows:

Even if you want to change the characteristics you can do it from here:

Javascript

Within Javascript, we will see that we have several methods that we can play with. Typically, the following method is used:


function preSearchFunction(executionContext) {
    // get the form context
    formContext = executionContext.getFormContext();
    formContext.getControl("customerid").addPreSearch(filterFunction);
}

function filterFunction() {

    // Only show accounts with the type 'Preferred Customer'
    var customerAccountFilter = "<filter type='and'><condition attribute='accountcategorycode' operator='eq' value='1'/></filter>";
    formContext.getControl("customerid").addCustomFilter(customerAccountFilter, "account");
}

function onLoad(){
    filterFunction();
}

The problem is that no matter how much you use it, it doesn’t work.

Solution

In order to solve this problem it is super simple. You don’t just have to use the line above.

También hay que añadir:

formContext.getControl("customerid").startCustomFilter()
formContext.getControl("customerid").endCustomFilter()

As follows:


function preSearchFunction(executionContext) {
    // get the form context
    formContext = executionContext.getFormContext();
    formContext.getControl("customerid").addPreSearch(filterFunction);
}

function filterFunction() {

    // Only show accounts with the type 'Preferred Customer'
    var customerAccountFilter = "<filter type='and'><condition attribute='accountcategorycode' operator='eq' value='1'/></filter>";
    formContext.getControl("customerid").startCustomFilter()
    formContext.getControl("customerid").addCustomFilter(customerAccountFilter, "account");
    formContext.getControl("customerid").endCustomFilter()
}

function onLoad(){
    filterFunction();
}

Another solution

Another solution that can be implemented is the following:

function filterContacts() {
    // the view ID can be anything unique
    var viewId = "{10101010-1010-1010-1010-101010101010}";
    var entityName = "contact";
    var viewDisplayName = "Contacts which start with Victor Sanchez";
    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="fullname" operator="like" value="Victor Sanchez%" />
            </filter>
        </entity>
    </fetch>
    `

    var layoutXml = `<grid name="resultset" object="2" jump="lastname" select="1" icon="1" preview="1"><row name="result" id="contactid"><cell name="firstname" width="300"/><cell name="lastname" width="100"/></row></grid>`;

    formContext.getControl("customerid").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, false);
    formContext.getControl("customerid").setDefaultView(viewId);
}

## How to try quick things like this

Normally, in order to test all this kind of things, I play a lot with the DOM and the console. To do that I mess around with Xrm.Page (bless it), so I can see what the fields are capable of. For this, I see that the Xrm.Page.getControl(“customerid”) has all these attributes and methods that allow us to perform different actions on it.

Conclusions

I think this is something that Microsoft will fix in some version of this PCF, as it should be able to be handled in a standard way and not have to develop a line of code.

However, if you have any questions, please contact me at me@victorsolaya.com and I will be happy to help you.


Join the newsletter