Trigger Catalog UI Policy in an Onload / Onchange Event for Record Producer Catalog Item Variable

BabyYoda
Tera Expert

I'll layout what I'm trying to achieve first and then delve into how I'm thinking of designing it.

Triggering onchange event for Catalog Item Variable in onload script; Triggering Catalog UI Policy.

I created a Record Producer with a number of fields on the form. One of these fields is called Inquiry which is a dropdown menu/choice field. Inquiry is a Variable. It's Type is Select Box. It contains a number of Question Choices. Include none is checked (more on that later).

 

I have a Catalog UI Policy which applies to said Record Producer and applies when the Catalog Condition of Inquiry is "Marketing". Applies on a Catalog Item is checked. Applied on the Target Record is checked. On load is checked. Reverse if false is checked. In the Catalog UI Policy Actions, I have a number of fields where Visible is set to True.

 

I open /CSM and click on Request Something to browse the catalog and look for my catalog/Record Producer I just created to test the UI. Looks good. However, users later complain about the None choice. They want the form to load with the most popular Inquiry choice already selected and the appropriate fields visible per the Catalog UI Policy.

 

I go back to the Record Producer, open the Inquiry Variable, and uncheck Include none to remove the "--none--" menu option in the Inquiry variable. Then I go to the Default Value tab and enter "Marketing" (without quotes) in the Default value field.

 

I reload the form to test changes and Inquiry dropdown field is set to "Marketing" and there's no "--none--" choice. However, the Catalog UI Policy didn't fire. I have to select another Inquiry field choice and then go back to "Marketing", in order for the Catalog UI Policy to fire where Inquiry == "Marketing". This is no good. I need the proper fields to display when Inquiry == "Marketing" during on load event. In other words, forcibly trigger an onchange event on an element within an on load event. 

 

Is it possible to trigger the Catalog UI Policy from a script? By trigger, I mean in terms of triggering a click event in JavaScript, you can force the event to occur without actually clicking on it, i.e. $("#link a").trigger("click"). Does something like this exist in ServiceNow? I think you see where I might be going with this. I don't want to reinvent the wheel and create a client script that reproduces the Catalog UI Policy. I'd rather just trigger the Catalog UI Policy in the client script. Write it once. No need to create boiler plate code. I'd rather keep things simple. Think of how you can trigger a Scheduled Job in ServiceNow from a script -- we know this is doable in ServiceNow. Same idea here.

 

 

 

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (!isLoading || newValue == '') {	   
        return;
    }

    if (g_form.getValue("inquiry") == "Marketing") {
        // Trigger Catalog UI Policy where Inquiry == "Marketing", avoiding boiler plate code
    }
}

 

 

 

 

If triggering the Catalog UI Policy from another script is not possible, then what would you suggest to effectively realize an onchange event, when Inquiry is set to "Marketing" during an on load event? How about exporting the Catalog UI Policy as a script? That way, even if you are writing boiler plate code, ServiceNow has done the work for you and it's just a copy/paste job.

 

Or maybe there's another way to set the Default Value in a script so that the Catalog UI Policy fires AFTER the Default Value is set programmatically. I'm trying to think of a simple solution.

 

Thank you for your help and consideration.

1 REPLY 1

RaghavSh
Kilo Patron

Why don't you create one UI policy with lower order maybe 50 with no condition and on load true.

In the script action of UI policy (execute if true) set g_form.setValue('inquire','Marketing');


Raghav
MVP 2023