Auto-Populating Approver (review_manager) via Variable Set on Catalog Items

JGuerrero0323
Tera Expert

When a specific Catalog Item is selected, I want to automatically populate the review_manager variable (reference to sys_user), which is inside a Variable Set for reusability.

  • If the Catalog Item is:
    sys_id: CATALOG_ITEM_SYS_ID

  • Then the approver (review_manager) should be the Manager of the group:
    sys_id: GROUP_SYS_ID

------

What I’ve done so far

  1. Script Include

var GetCatalogItemApprover = Class.create();
GetCatalogItemApprover.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getApprover: function() {
        var catalogItemId = this.getParameter('sysparm_catalog_item');

        if (catalogItemId == '8680af5133b26a10219be1f13d5c7bce') {
            var group = new GlideRecord('sys_user_group');
            if (group.get('9016d41b83ea1250145417447daad3f2')) {
                return group.getValue('manager') || '';
            }
        }

        return '';
    }
});
  • Catalog Client Script (Applies to: variable Set) 
function onLoad() {
    var catalogItemId = g_form.getValue('sysparm_item_guid');

    var ga = new GlideAjax('GetCatalogItemApprover');
    ga.addParam('sysparm_name', 'getApprover');
    ga.addParam('sysparm_catalog_item', catalogItemId);
    ga.getXMLAnswer(function(answer) {
        if (answer) {
            g_form.setValue('review_manager', answer);
        }
    });
}
​
  • Ref qualifier review_manager
     JGuerrero0323_0-1753066779152.png



    The Issue: 
    My variable displays all user not just the manager for the group.  

    JGuerrero0323_1-1753066956702.png

    • Any insights on why the review_manager is not displaying the user even though a sys_id is being set?

    • Is there a better approach to dynamically populate a reference field in a Variable Set on Catalog Item load?

    Any guidance or examples would be greatly appreciated!

    Thanks in advance,

     

    Juan 
1 ACCEPTED SOLUTION

@JGuerrero0323 

okay then do this in onLoad catalog client script Applies to Catalog Item

function onLoad() {

    var url = top.location.href;
    var ga = new GlideAjax('GetCatalogItemApprover');
    ga.addParam('sysparm_name', 'getApprover');
    ga.addParam('sysparm_catalog_item', url);
    ga.getXMLAnswer(function(answer) {
        if (answer) {
            g_form.setValue('review_manager', answer);
        }
    });
}

Script Include:

var GetCatalogItemApprover = Class.create();
GetCatalogItemApprover.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getApprover: function() {
        var url = this.getParameter('sysparm_catalog_item');
        if (url.indexOf('8680af5133b26a10219be1f13d5c7bce') > -1) {
            var group = new GlideRecord('sys_user_group');
            if (group.get('9016d41b83ea1250145417447daad3f2')) {
                return group.getValue('manager') || '';
            }
        }
        return '';
    }
});

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Di Zhang
Tera Guru

I suggest add your code to reference Reference qualifier instead of using client script or change the field type to look up field and using client script to clear the options and add the correct option to field. 

Ankur Bawiskar
Tera Patron
Tera Patron

@JGuerrero0323 

is that variable set used across multiple catalog items and hence you want to check the catalog item sysId?

you want to populate that user variable with which group manager?

You told Manager of group -> which one? is that a variable on catalog form?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar,

Thanks for the reply!

Yes — the Variable Set is designed to be reusable across multiple Catalog Items, which is why I’m checking the Catalog Item sys_id.

To clarify:

For the Catalog Item with sys_id: 8680af5133b26a10219be1f13d5c7bce.

I want the review_manager variable (reference to sys_user) to be automatically populated with the Manager of the group with sys_id: 9016d41b83ea1250145417447daad3f2.

-----

When I say “Manager of the group”, I’m referring to the manager field on the Group record in sys_user_group — not a variable on the form. 

JGuerrero0323_0-1753071602630.png

 



Thanks again for your help!

Regards,
Juan

 

@JGuerrero0323 

okay then do this in onLoad catalog client script Applies to Catalog Item

function onLoad() {

    var url = top.location.href;
    var ga = new GlideAjax('GetCatalogItemApprover');
    ga.addParam('sysparm_name', 'getApprover');
    ga.addParam('sysparm_catalog_item', url);
    ga.getXMLAnswer(function(answer) {
        if (answer) {
            g_form.setValue('review_manager', answer);
        }
    });
}

Script Include:

var GetCatalogItemApprover = Class.create();
GetCatalogItemApprover.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getApprover: function() {
        var url = this.getParameter('sysparm_catalog_item');
        if (url.indexOf('8680af5133b26a10219be1f13d5c7bce') > -1) {
            var group = new GlideRecord('sys_user_group');
            if (group.get('9016d41b83ea1250145417447daad3f2')) {
                return group.getValue('manager') || '';
            }
        }
        return '';
    }
});

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader