- 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 02:27 AM
Thanks for helping. Just changed few things to your script and it worked:
Here is the updated on.
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
g_form.clearValue('approval_group2');
return;
}
var ga = new GlideAjax('CheckNuclearConditions');
ga.addParam('sysparm_name', 'checkConditionsClient');
ga.addParam('sysparm_requested_for', newValue);
ga.getXMLAnswer(function(answer) {
if (answer === 'true') {
g_form.setValue('approval_group2', 'a293e2c333e86650e8f08c889d5c7b65');
} else {
g_form.clearValue('approval_group2');
}
});
}
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 = '';
if (user.u_snc_l_company) { // Ensure field exists
var company = new GlideRecord('u_company_org');
if (company.get(user.u_snc_l_company)) {
companyName = company.getValue('name'); // Get company name
}
}
var companyContainsKeywords = companyName.toLowerCase().includes("candu") || companyName.toLowerCase().includes("nuclear");
var costCenterContainsKeywords = false;
if (user.cost_center) { // Ensure cost_center exists
var costCenter = new GlideRecord('cost_center');
if (costCenter.get(user.cost_center)) {
var hierarchyPath = costCenter.getValue('u_hierarchy_path');
if (hierarchyPath && hierarchyPath.toLowerCase().includes("nuclear")) {
costCenterContainsKeywords = true;
}
}
}
return companyContainsKeywords || costCenterContainsKeywords;
}
return false;
},
checkConditionsClient: function() {
var requestedFor = this.getParameter('sysparm_requested_for');
var result = this.checkConditions(requestedFor);
return result.toString();
},
type: 'CheckNuclearConditions'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2025 02:34 AM
Hello @Bindhu1
These are the same changes I recommended in my above reply. If it helped you, Kindly accept my solution as well, as we can accept multiple solutions 😀
Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket.
Regards,
Shivalika
My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194
My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY
- 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:55 AM
Thank you for marking my response as helpful.
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