Unable to fetch reference field value from script includes but the value is return in background
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2024 11:50 PM
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.
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:
Within the reference variable dropdown list:
I use advance in reference qualifier and enter
javascript: new AORApprovalMatrix1().getApproverList();
However, the results are not showing only "SSC_CC00042 Team Lead", but the entire list of sys_user_group are shown in the list.
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2024 05:39 AM - edited 03-08-2024 10:03 AM
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());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2024 06:00 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2024 06:10 AM
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