How to wait for a response from a Script Include in an onLoad Catalog Client Script?

apjohn2
Mega Sage

Hi Community!

 

Seeking help with the following issue. Gist: An onLoad Catalog Client script with Script Include (SI) call works fine in Portal (/sp or /esc) but in Platform UI the form loads too fast, and the response from the SI is not received, so the variable set (where the Client Script is configured) fails to load properly.

 

  • We have a custom field in the Catalog Item [sc_cat_item] table that determines how approvals work (one group or another or manager etc.)
  • We have a variable set that looks up that value to set its variables and those values are fed into the given flow / workflow to get approval
  • We use a Script Include to fetch that value
  • The Script Include was configured originally for only Catalog Items, i.e., not for Order Guides
  • When it checks the custom approval type field it sends the current item sys ID to the Script Include and checks only the sc_cat_item table for that ID
  • However we have a few Order Guides now that also need to use this variable set
  • The variable set seems to work ok in portals but not in the platform UI and I discovered it was having an issue when looking up the current item approval type because the table is sc_cat_item_guide.
  • I have updated the Script Include function to look up the table but no matter what method I try the response from the SI is not coming fast enough and the values come back 'null' which breaks the Client Script functionality, causing fields to not show up in the form that are necessary for proper approval functionality.

So what is a functional method to find the current table / class name in an onLoad Client Script?

 

I have tried g_form.getTableName() but it fails and I get 'null' values. I have also (as you'll see below) tried using a global table lookup function (which I found here) that works fine in a background script and in a portal but in platform UI this too returns nothing.

 

Client Script:

 

function onLoad() {
    // load Manager
    g_form.getReference("requested_for", function(user) {
		
		if(!user.manager || user.manager == '' || user.manager == undefined) {
			g_form.setValue("is_my_manager", 'No');
			g_form.setReadOnly("is_my_manager", true);
			// g_form.showFieldMsg("requester_manager", "Manager is empty");
			return;
		}
		
        g_form.setValue("requester_manager", user.manager);

        // Get from back-end the Manager Information
        var ga = new GlideAjax('LippertSCUtil');
        ga.addParam('sysparm_name', 'getUserInfo');
        ga.addParam('sysparm_user_id', user.manager);
        ga.getXML(function(response) {
            var answer = response.responseXML.documentElement.getAttribute("answer");
            var usr = JSON.parse(answer);

			// boolean values come as 0 and 1
			// 0 = false    1 = true
            if (usr.active == 0) {
                g_form.setValue("is_my_manager", 'No');
                g_form.setReadOnly("is_my_manager", true);
                g_form.showFieldMsg("requester_manager", "Manager is inactive");
            }
        });
    });

    // By default Hide the fields
    g_form.setVisible("requester_manager", false);
    g_form.setVisible("is_my_manager", false);

    // Get the current Catalog Item
    var id = g_form.getUniqueValue();

    // Get from back-end the Approving Authority
    var ga = new GlideAjax('LippertSCUtil');
    ga.addParam('sysparm_name', 'getApprovingAuthority');
    ga.addParam('sysparm_cat_item_id', id);
    ga.getXML(function(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");

        // When Immediate supervisor, then display the manager fields
        if (answer == "Immediate Supervisor") {
            g_form.setVisible("requester_manager", true);
            g_form.setVisible("is_my_manager", true);
            g_form.setMandatory("is_my_manager", true);
        }
    });
}

 

 

Script Include (just the relevant functions):

 

    getApprovingAuthority: function() {
        var itemID = this.getParameter("sysparm_cat_item_id");

        // Find the sc_cat_item[_x] table name to ensure Approving Authority is not null
        var classTable = findAnywhere(itemID);
        
        var catItem = new GlideRecord(classTable);
        catItem.get(itemID);
        return catItem.getValue("u_approving_authority") + '';

        // findAnywhere function
        function findAnywhere(sysIDToFind, getHTML) {
            if (getHTML !== true && getHTML !== 'true') {
                getHTML = false;
            }
            var grCheck;
            var tableName;
            var grTable = new GlideRecord('sys_db_object');
            //Only check tables with names starting with "sc_cat_item".
            grTable.addEncodedQuery('sys_update_nameISNOTEMPTY^nameISNOTEMPTY^nameSTARTSWITHsc_cat_item');
            grTable.query();
            while (grTable.next()) {
                tableName = grTable.getValue('name');
                grCheck = new GlideRecord(tableName);
                if (grCheck.get(sysIDToFind)) {
                    return tableName;
                }
            }
        }
    },

    getUserInfo: function() {
        var userID = this.getParameter("sysparm_user_id");

        var user = new GlideRecord("sys_user");
        user.get(userID);

        var o = {
            'active': user.getValue('active'),
            'email': user.getValue('email'),
            'user_name': user.getValue('user_name')
        };

        return JSON.stringify(o);
    },

 

 

1 ACCEPTED SOLUTION

AJ M
Mega Sage

I'm confused.

  1. Order Guides do not directly create RITMs and do not have Flows or Workflows. They order Catalog Item(s). 
  2. Order Guide extends Catalog Item, so searching for the Order Guide on the Catalog Item table should work
  3. Global table lookup is highly inefficient
  4. Defaults are being set via script rather than on the variables themselves, which is inefficient
  5. You have visibility and mandatory conflicts between your two sections: Inactive Manager and Approving Authority

 

If you cannot set the Defaults at the field level, move them to the top of the script.

If you cannot nest or merge your functions, start a scratchpad.

 

var usr = JSON.parse(answer);
g_scratchpad.manager_active = usr.active;

 

 

g_scratchpad.approving_auth = answer

 

 

Then in each function, you will want to check BOTH

 

if (usr.active == 0 && g_scratchpad.approving_auth == "Immediate Supervisor") 
if (answer == "Immediate Supervisor" && g_scratchpad.manager_active == 1) 

 

View solution in original post

5 REPLIES 5

If you need help with any or just want to try all of these methods, let me know and I can throw together a few things for you!