- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2025 11:16 AM
I have below two variables: need help in script to achieve this requirement.
1. requested_for: referring to sys_user
2. approval_group: referring to sys_user_group
Populate the Group as 'GROUP_Nuclear' only when
- Requested For.u_snc_l_company contains "CANDU" or "Nuclear"
- or Requested For.cost_center.u_hierarchy_path path contains "Nuclear"
->
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2025 01:26 AM
Hi @Bindhu1 ,
my bad I didn't notice u_snc_l_company as a reference field
is it referencing to core_company table if the below script doesn't work as stated in the line number 7 of below code dot walk further to field where nuclear or Candu exist
update the script include
var CheckNuclearConditions = Class.create();
CheckNuclearConditions.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkConditions: function(requestedFor) {
var user = new GlideRecord('sys_user');
if (user.get(requestedFor)) {
var companyName = user.u_snc_l_companyName.getDisplayValue(); /*if gtDisplayValue() doesn't work try dot walkin to name or the field where Nuclear or CANDU you are trying to find*/
var companyContainsKeywords = companyName.includes("CANDU") || companyName.includes("Nuclear");
var costCenterContainsKeywords = user.cost_center.u_hierarchy_path.includes("Nuclear");
return companyContainsKeywords || costCenterContainsKeywords;
}
return false;
},
// This function is client-callable
checkConditionsClient: function() {
var requestedFor = this.getParameter('sysparm_requested_for');
var result = this.checkConditions(requestedFor);
return result.toString();
},
type: 'CheckNuclearConditions'
});
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2025 01:48 AM
Did you add logs in script include?
Did you add alert in client script?
Your Script Include should be client callable
Something like this, I assume u_snc_l_company is referring to core_company table
var UserCheck = Class.create();
UserCheck.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getGroup: function() {
var user = this.getParameter('sysparm_user');
var gr = new GlideRecord("sys_user");
gr.addQuery("sys_id", user);
// give the field on that table which holds this value, I gave name field
gr.addQuery('u_snc_l_company.nameLIKECANDU,Nuclear').addOrCondition('cost_center.u_hierarchy_pathLIKENuclear');
gr.query();
if (gr.hasNext()) {
return 'nuclearGroupSysId';
}
return '';
},
type: 'UserCheck'
});
onChange client script: On Requested For field on form
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if (newValue == '')
g_form.clearValue('approval_groupField');
var ga = new GlideAjax('UserCheck');
ga.addParam('sysparm_name', 'getGroup')
ga.addParam('sysparm_user', newValue);
ga.getXMLAnswer(function(answer) {
if (answer.toString() != '') {
g_form.setValue('approval_groupField', answer.toString());
}
});
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2025 01:38 AM
My Sample script is here
Client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.clearValue('approval_group2');
return;
}
var ga = new GlideAjax('CheckApprovalGroup');
ga.addParam('sysparm_name', 'getApprovalGroup');
ga.addParam('sysparm_requested_for', newValue);
ga.getXMLAnswer(function(response) {
if (response == 'GROUP_Nuclear') {
g_form.setValue('approval_group2', 'GROUP_Nuclear');
} else {
g_form.clearValue('approval_group2'); // Clear if condition not met
}
});
}
Script include:
var CheckApprovalGroup = Class.create();
CheckApprovalGroup.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getApprovalGroup: function() {
var requestedForSysId = this.getParameter('sysparm_requested_for');
if (!requestedForSysId) {
return '';
}
var userGR = new GlideRecord('sys_user');
if (userGR.get(requestedForSysId)) {
var companySysId = userGR.getValue('u_snc_l_company'); // Reference field
var costCenterSysId = userGR.getValue('cost_center'); // Reference field
// Check company name
if (companySysId) {
var companyGR = new GlideRecord('u_company_org_list');
if (companyGR.get(companySysId)) {
var companyName = companyGR.getValue('name');
if (companyName.includes('CANDU') || companyName.includes('Nuclear')) {
return 'GROUP_Nuclear';
}
}
}
// Check cost center hierarchy path
if (costCenterSysId) {
var costCenterGR = new GlideRecord('cost_center');
if (costCenterGR.get(costCenterSysId)) {
var hierarchyPath = costCenterGR.getValue('u_hierarchy_path');
if (hierarchyPath && hierarchyPath.includes('Nuclear')) {
return 'GROUP_Nuclear';
}
}
}
}
return ''; // Return empty if no match
}
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2025 02:08 AM
I got it @Bindhu1
Just change one thing, instead of "SETTING THE VALUE WITH GROUP NUCLEAR, PQSS SYS_ID"
I was not aware of sys_id hence passed that.
g_form.setValue('approval_group2', 'GROUP_Nuclear'); - this line
And also remove below 👇 lines -
else {
g_form.clearValue('approval_group2'); // Clear if condition not met
}
Next add logs everywhere and let me know which one is not printing value, try after above @Bindhu1 Edits only.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2025 11:52 AM
Hi @Bindhu1
create a onchange client script on requested_for variable
CS
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
g_form.clearValue('approval_group')
return;
}
var ga = new GlideAjax('CheckNuclearConditions');
gs.addParam('sysparm_name', 'checkConditionsClient')
ga.addParam('sysparm_requested_for', newValue);
ga.getXMLAnswer(function(answer) {
if (answer === 'true') {
g_form.setValue('approval_group', 'put SYSID OF the Group');
}
});
}
Client callable script include
var CheckNuclearConditions = Class.create();
CheckNuclearConditions.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkConditions: function(requestedFor) {
var user = new GlideRecord('sys_user');
if (user.get(requestedFor)) {
var companyContainsKeywords = user.u_snc_l_company.includes("CANDU") || user.u_snc_l_company.includes("Nuclear");
var costCenterContainsKeywords = user.cost_center.u_hierarchy_path.includes("Nuclear");
return companyContainsKeywords || costCenterContainsKeywords;
}
return false;
},
// This function is client-callable
checkConditionsClient: function() {
var requestedFor = this.getParameter('sysparm_requested_for');
var result = this.checkConditions(requestedFor);
return result.toString();
},
type: 'CheckNuclearConditions'
});
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2025 01:00 AM
Hi Chaitanya, thanks for the script , but this is not setting approval_group
or can we break down this like below please?
if Requested For.u_snc_l_company [reference field] contains "CANDU" or "Nuclear"
set the approval_group to 'Nuclear'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2025 01:26 AM
Hi @Bindhu1 ,
my bad I didn't notice u_snc_l_company as a reference field
is it referencing to core_company table if the below script doesn't work as stated in the line number 7 of below code dot walk further to field where nuclear or Candu exist
update the script include
var CheckNuclearConditions = Class.create();
CheckNuclearConditions.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkConditions: function(requestedFor) {
var user = new GlideRecord('sys_user');
if (user.get(requestedFor)) {
var companyName = user.u_snc_l_companyName.getDisplayValue(); /*if gtDisplayValue() doesn't work try dot walkin to name or the field where Nuclear or CANDU you are trying to find*/
var companyContainsKeywords = companyName.includes("CANDU") || companyName.includes("Nuclear");
var costCenterContainsKeywords = user.cost_center.u_hierarchy_path.includes("Nuclear");
return companyContainsKeywords || costCenterContainsKeywords;
}
return false;
},
// This function is client-callable
checkConditionsClient: function() {
var requestedFor = this.getParameter('sysparm_requested_for');
var result = this.checkConditions(requestedFor);
return result.toString();
},
type: 'CheckNuclearConditions'
});
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya