Unable to fetch reference field value from script includes but the value is return in background

Karter83
Tera Contributor

I tried writing the following script in Script - Background to test the values:

 

 

(function(){
        var currentUser = new GlideRecord('sys_user');
        currentUser.get( gs.getUserID());
        var requesterCostCenter = currentUser.getValue('cost_center');

        var costCenter = new GlideRecord('cmn_cost_center');
        costCenter.addQuery('sys_id', requesterCostCenter);
        costCenter.query();

        var costCenterName = {};

        while (costCenter.next()) {
            costCenterName = costCenter.code.toString();
        }

        var approvalMatrixGr = new GlideRecord('sys_user_group');
        approvalMatrixGr.addQuery('name', costCenterName + ' Team Lead');
        approvalMatrixGr.query();

        var approvers = [];

        while (approvalMatrixGr.next()) {
            gs.info(approvalMatrixGr.name.toString());
            approvers.push(approvalMatrixGr.name.toString());
        }
})();

 

And I received the values which I desire, which is the correct sys_user_group based on the conditions given.

Karter83_1-1709883718560.png

Then I rewrote the script to insert the class function:

 

var AORApprovalMatrix1 = Class.create();
AORApprovalMatrix1.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
	getApproverList: function()
	{
		var currentUser = new GlideRecord('sys_user');
		currentUser.get( gs.getUserID());
		var requesterCostCenter = currentUser.getValue('cost_center');
        gs.info(requesterCostCenter);

        var costCenter = new GlideRecord('cmn_cost_center');
        costCenter.addQuery('sys_id', requesterCostCenter);
        costCenter.query();

        var costCenterName = {};

        while (costCenter.next()) {
            gs.info(costCenter.code.toString());
            costCenterName = costCenter.code.toString();
		}

        var approvalMatrixGr = new GlideRecord('sys_user_group'); 
		approvalMatrixGr.addQuery('name', costCenterName + ' Team Lead'); 
		approvalMatrixGr.query();

		var approvers = [];

		while (approvalMatrixGr.next()) {
            gs.info(approvalMatrixGr.name.toString());
			approvers.push(approvalMatrixGr);
		}
		return 'sys_idIN'+approvers.join(',');
    },
    type: 'AORApprovalMatrix1'
});

 

But the result is not shown in Script background output:

Karter83_2-1709883936303.png

 

Within the reference variable dropdown list:

I use advance in reference qualifier and enter

 

 javascript: new AORApprovalMatrix1().getApproverList();

 

Karter83_3-1709883979592.png

 

However, the results are not showing only "SSC_CC00042 Team Lead", but the entire list of sys_user_group are shown in the list.

 

Karter83_0-1709883596538.png

May I seek for someone to help me what is wrong with my scripts that is given me this issue? Please help me as soon as you can, as I am rushing for a tight timeline to get this resolved. Thank you so much!

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

When there is an error in the qualifier, calling the Script Include, or even within the GlideRecord in the Script Include, the default will be to show all records.  Are the Catalog Item / variable and the Script Include in the same Application/Scope (Global?) ? If you change your logs to include a static value to search on, like:

 

gs.info('MY reference qualifier - cost center = ' + requesterCostCenter);

 

What are you seeing in the system log?  Changing it to an infoMessage should also work, and will display in the reference search window for immediate feedback:

 

gs.addInfoMessage('cost center = ' + requesterCostCenter);

You are also not building the array correctly in the Script Include version of your attempt.

approvers.push(approvalMatrixGr.sys_id.toString());

 

swathisarang98
Giga Sage
Giga Sage

Hi @Karter83 ,

 

In return you are sending object so try sending sys_id 

 

 

approvers.push(approvalMatrixGr.sys_id.toString());
return 'sys_idIN'+approvers;

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

SunilKumar_P
Giga Sage

Hi @Karter83, Looks like the script is client callable as the script contains 'Object.extendsObject'. Can you uncheck Client Callable and that look something like below and try once?

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

    type: 'AORApprovalMatrix1'
});

 

Regards,
Sunil