Service Catalog: Dynamic Field Messages for Choice Variables with Hints




First off, I have to give credit to Jace Benson for his code and offering a starting point for what I needed to do. Most of the functionality discussed is his, and this post is how I tweaked it for my requirements on the Service Catalog.


Here is his original blog post: https://blog.jace.pro/post/2017-09-08-show-hints-for-choice-lists/


My requirements were to show dynamic field messages for choice list selections on a catalog item/record producer in the platform UI and portal. My choice list variable was pointing to an existing choice list on my table, and I didn't want to hardcode a new widget or catalog client script to show messages dependent upon what the user selected.. So here's how I accomplished this:


The Hint column on the sys_choice table is a string field, and has been around forever. I was looking for functionality to put some instructions or a description or something along with my choice and this field works great. The silver lining to my implementation of this solution are that fulfillers are not utilizing the Platform UI (they're using Workspace) and if they do for whatever reason, I don't think fulfillers are going to be hovering over the choice list enough to even see the hint (and perhaps it doesn't matter if they do see it). My fulfillers are going to be working from the Workspace UI and at this current point in time the Workspace UI doesn't show hints to my knowledge (I tried, I couldn't get them to come up), so I got to use this field to my needs.


First, I created a choice field on my table (I'm using a scoped 'Orders' table for demo purposes), entered some choice demo data with some Hint values.



Next, I created my variable and pointed it to my choice field on my Orders table.



Now, to put in the underlying logic to make this work.


Here is the Script Include from Jace, I didn't have to do any tweaking on this (yes, you can create this in scope but make sure to check Client Callable and allow all application scopes to call this if you're using the portal as a lot of out-of-the-box widgets are in the global scope)


var ChoiceUtil = Class.create();
ChoiceUtil.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    getHint: function() {
        try {
            var returnObj = {};
            var hint = new GlideRecord('sys_choice');
            hint.addQuery('name', this.getParameter('sysparm_table'));
            hint.addQuery('element', this.getParameter('sysparm_field'));
            hint.addQuery('value', this.getParameter('sysparm_val'));
            hint.addQuery('inactive', 'false');
            hint.query();
            if (hint.next()) {
                returnObj.hint = hint.getValue('hint');
            } else {
                returnObj.error = 'no choice found for ';
                returnObj.error += hint.getEncodedQuery();
            }
            //returnObj.query = hint.getEncodedQuery();
            return JSON.stringify(returnObj);
        } catch (error) {
            return JSON.stringify(error, '', '  ');
        }
    },
    type: 'ChoiceUtil'
});


Finally, here's the Catalog Client Script on the Catalog Item that I tweaked to meet the needs of being in the catalog and using variables instead of on the table form. This is an onChange client script, for all UI types, based on the Order Reason field changing:


function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
	
	
    var ga = new GlideAjax('ChoiceUtil');
    ga.addParam('sysparm_name', 'getHint');
    ga.addParam('sysparm_table', 'x_422452_orders_orders');
    ga.addParam('sysparm_field', 'order_reason');
    ga.addParam('sysparm_val', newValue);
    ga.getXML(HintParse);

    function HintParse(response) {
        var answer = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
        g_form.hideFieldMsg('order_reason', true);
        if (answer.hint) {
            g_form.showFieldMsg('order_reason', answer.hint, 'info', true);
        }
    }

}

Here are what the results look like on both the Platform UI and Service Portal:





Recap

  1. Create a choice field or use an existing one, in the choice list values for your table, enter in some hints

  2. Set up your choice list variable on your catalog item

  3. Set up your Script Include

  4. Set up your Catalog Client Script


While this method does require development to change the hints, they at least aren't hardcoded in a script somewhere and I didn't have to create a special table to store the messages, and the Script Include can be re-used for multiple catalog items.


Again, props to Jace for documenting this way back in 2017!


Resources


https://blog.jace.pro/post/2017-09-08-show-hints-for-choice-lists/

189 views0 comments

Recent Posts

See All