- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2024 06:31 AM - edited 08-18-2024 06:32 AM
Dear Team,
I want to make a variable (impacted_on_selection_of_user) mandatory when variable (user) changed.
Query (DotWalk path)- User>Department>Department Fields-->'u_group' Contains 'ATF Service Level Management Group'
Note: I have created a custom field 'u_group' (Type- List (Glidelist)), Reference- sys_user_group) on Department table.
I have created a onChange Client Script (onChange of variable 'user'), Script is shown below, but it is not working as expected as when I select any user whose Department Group contains 'ATF Service Level Management Group' then the variable 'impacted_on_selection_of_user' should become mandatory else non mandatory.
Kindly help to modify the script so that it will work as expected
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var grpname = g_form.getValue('user');
var gr = new GlideRecord('sys_user');
if (grpname == gr.getEncodedQuery('department.u_groupLIKE561cc5360f6e33004fbc309c4e767eb8')) {
g_form.setMandatory("impacted_on_selection_of_user", true);
} else {
g_form.setMandatory("impacted_on_selection_of_user", false);
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2024 09:42 AM
Thank you @Mark Manders and @Isaac Vicentini for your help,
I have worked on this and now it is working as expected. The Client Callable Script Include and onChange Client Script are shown below;
Script Include
var testUtil = Class.create();
testUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserDeptGrp: function() {
var grpname = this.getParameter('sysparm_user');
var result = this.newItem('check');
var gr = new GlideRecord('sys_user');
gr.addEncodedQuery('department.u_groupLIKE561cc5360f6e33004fbc309c4e767eb8^sys_id='+grpname);
gr.query();
if(gr.next()) {
result.setAttribute('check','yes');
} else {
result.setAttribute('check','no');
}
},
type: 'testUtil'
onChange Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('testUtil');
ga.addParam('sysparm_name', 'getUserDeptGrp');
ga.addParam('sysparm_user', g_form.getValue('user'));
ga.getXML(myCallback);
function myCallback(answer) {
if (answer) {
var result = answer.responseXML.getElementsByTagName("check");
var check = result[0].getAttribute("check");
if (check == 'yes') {
g_form.setMandatory("impacted_on_selection_of_user", true);
} else {
g_form.setMandatory("impacted_on_selection_of_user", false);
}
}
}
}
If possible requesting you to please mark my answer as correct this will small help for a script learner like me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2024 07:33 AM - edited 08-18-2024 07:34 AM
Hi!
Glide Record usage in client script is not a good practice.
You should easily be able to convert your code into a GlideAjax call and/or getReference(callback) function, depending on the need of the requirement.
You can use GlideAjax like this:
var ga = new GlideAjax('script_include_name');
ga.addParam('sysparm_name', 'function_name');
ga.addParam('sysparm_user', g_form.getValue('user'));
ga.getXML(mycallback);
function mycallback(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'true') { //assuming you are returning true as answer from script include
g_form.setMandatory("impacted_on_selection_of_user", true);
} else {
g_form.setMandatory("impacted_on_selection_of_user", false);
}
}
// Script Include
function_name: function() {
var answer ='';
var grpname = this.getParameter('sysparm_user');
var gr = new GlideRecord('sys_user');
if (grpname == gr.getEncodedQuery('department.u_groupLIKE561cc5360f6e33004fbc309c4e767eb8')) {
answer = "true";
} else {
answer = "false"
}
return answer;
}
If my answer helped you in any way, please mark it as correct/helpful 🙂
Regards,
Isaac Vicentini.
MVP 2025 ✨
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2024 07:44 AM
Try it like this:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var userId = g_form.getValue('user'); // Get the user ID
var gr = new GlideRecord('sys_user');
if (gr.get(userId)) { // Ensure the user record is found
// Perform your logic based on the user's department group
if (gr.department.u_groupLIKE'561cc5360f6e33004fbc309c4e767eb8') {
g_form.setMandatory("impacted_on_selection_of_user", true);
} else {
g_form.setMandatory("impacted_on_selection_of_user", false);
}
}
}
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2024 09:42 AM
Thank you @Mark Manders and @Isaac Vicentini for your help,
I have worked on this and now it is working as expected. The Client Callable Script Include and onChange Client Script are shown below;
Script Include
var testUtil = Class.create();
testUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserDeptGrp: function() {
var grpname = this.getParameter('sysparm_user');
var result = this.newItem('check');
var gr = new GlideRecord('sys_user');
gr.addEncodedQuery('department.u_groupLIKE561cc5360f6e33004fbc309c4e767eb8^sys_id='+grpname);
gr.query();
if(gr.next()) {
result.setAttribute('check','yes');
} else {
result.setAttribute('check','no');
}
},
type: 'testUtil'
onChange Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('testUtil');
ga.addParam('sysparm_name', 'getUserDeptGrp');
ga.addParam('sysparm_user', g_form.getValue('user'));
ga.getXML(myCallback);
function myCallback(answer) {
if (answer) {
var result = answer.responseXML.getElementsByTagName("check");
var check = result[0].getAttribute("check");
if (check == 'yes') {
g_form.setMandatory("impacted_on_selection_of_user", true);
} else {
g_form.setMandatory("impacted_on_selection_of_user", false);
}
}
}
}
If possible requesting you to please mark my answer as correct this will small help for a script learner like me.