- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-21-2024 04:23 AM
We have 3 variables in Catalog item.
1. Requested for and it's type is Requested For(sys_user).
2. Charge method and it's type is drop down, we have option: ABC 1 and ABC 2.
3. Charge number and it's type is single line text.
The requirement is when Charge method field as ABC 2 then Charge number field should auto populate with Requestor for - cost center in user table. how to configure this in client script?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-21-2024 05:46 AM
Hi @Pradeepa B ,
Create a onChange client script and a script include as below:
Client Script-
Type - onChange
UI Type - All
Variable name - Charge method
Script -
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Check if the selected value of Charge method is 'ABC 2'
if (newValue === 'Charge Method') {
var requestedFor = g_form.getValue('requested_for');
if (requestedFor) {
var ga = new GlideAjax('GetUserCostCenter');
ga.addParam('sysparm_name', 'getCostCenter');
ga.addParam('sysparm_user', requestedFor); // Pass the sys_id of Requested for
ga.getXMLAnswer(function(response) {
var costCenter = response;
if (costCenter) {
// Set the Charge number field with the cost center value
g_form.setValue('charge_number', costCenter);
} else {
// Optionally clear the field if no cost center is found
g_form.setValue('charge_number', '');
g_form.addInfoMessage("Cost center is not available for the selected user")
}
});
} else {
// Clear the Charge number field if no user is selected
g_form.setValue('charge_number', '');
}
} else {
// Clear the Charge number field if Charge method is not 'ABC 2'
g_form.setValue('charge_number', '');
}
}
Script Include -
Name - GetUserCostCenter
Client callable - Checked
Script -
var GetUserCostCenter = Class.create();
GetUserCostCenter.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCostCenter: function() {
var userSysId = this.getParameter('sysparm_user');
if (!userSysId) {
return '';
}
var userGR = new GlideRecord('sys_user');
userGR.get(userSysId);
if (userGR.isValidRecord()) {
return userGR.getDisplayValue('cost_center'); // Assuming 'cost_center' is the field name
}
return '';
},
type: 'GetUserCostCenter'
});
Output -
I have applied this script to Phone variable.
Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.
Thanks and Regards,
Ashish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-21-2024 07:51 AM
Create an onChange client script to clear the value. In this example, I have cleared both the 'Charge Method' and 'Charge Number' fields to allow the 'Cost Center' to populate, as it requires selecting the 'ABC 2' option again.
Script-
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.clearValue('charge_method');
g_form.clearValue('charge_number');
}
Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway. You can mark multiple answers as helpful and correct.
Thanks and Regards,
Ashish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-21-2024 04:52 AM
Hi @Pradeepa B ,
You can follow below steps.
- Create onchange client script and call script include to get the cost center name from server side of requested for.
- In client script set that value in Charge number
Onchange Client script code:
function onChange(control, oldValue, newValue, isLoading) {
if (newValue != '') {
var ga = new GlideAjax('UserUtils');
ga.addParam('sysparm_name', 'getCostCenter');
ga.addParam('sysparm_caller_sys_id', newValue);
ga.getXML(CostCenter);
}
}
function CostCenter(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('charget_number', answer);
}
Server Side code:
var UserUtils = Class.create();
UserUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCostCenter: function() {
var caller = this.getParameter('sysparm_caller_sys_id');
var gr = new GlideRecord('sys_user');
gr.get(caller);
return gr.cost_center.name;
},
type: 'UserUtils'
});
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-21-2024 05:34 AM
you can write onChange catalog client script on 2nd variable and have something like this with GlideAjax
Script Include: It should be client callable
var checkRecords = Class.create();
checkRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkRecordPresent: function(){
var id = this.getParameter('sysparm_userID');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', id); // use valid field name here
gr.query();
if(gr.next()){
return gr.getDisplayValue() + ' - ' + gr.cost_center.name;
}
},
type: 'checkRecords'
});
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (newValue == 'ABC2') { // give correct choice value here
var ga = new GlideAjax('checkRecords');
ga.addParam('sysparm_name', "checkRecordPresent");
ga.addParam('sysparm_userID', g_form.getValue('requested_for')); // give here requested for variable name
ga.getXMLAnswer(function(answer) {
g_form.setValue('charge_number', answer); // give here charge number variable name
});
//Type appropriate comment here, and begin script below
}
else{
g_form.clearValue('charge_number'); // give here charge number variable name
}
}
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
‎11-21-2024 05:46 AM
Hi @Pradeepa B ,
Create a onChange client script and a script include as below:
Client Script-
Type - onChange
UI Type - All
Variable name - Charge method
Script -
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Check if the selected value of Charge method is 'ABC 2'
if (newValue === 'Charge Method') {
var requestedFor = g_form.getValue('requested_for');
if (requestedFor) {
var ga = new GlideAjax('GetUserCostCenter');
ga.addParam('sysparm_name', 'getCostCenter');
ga.addParam('sysparm_user', requestedFor); // Pass the sys_id of Requested for
ga.getXMLAnswer(function(response) {
var costCenter = response;
if (costCenter) {
// Set the Charge number field with the cost center value
g_form.setValue('charge_number', costCenter);
} else {
// Optionally clear the field if no cost center is found
g_form.setValue('charge_number', '');
g_form.addInfoMessage("Cost center is not available for the selected user")
}
});
} else {
// Clear the Charge number field if no user is selected
g_form.setValue('charge_number', '');
}
} else {
// Clear the Charge number field if Charge method is not 'ABC 2'
g_form.setValue('charge_number', '');
}
}
Script Include -
Name - GetUserCostCenter
Client callable - Checked
Script -
var GetUserCostCenter = Class.create();
GetUserCostCenter.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCostCenter: function() {
var userSysId = this.getParameter('sysparm_user');
if (!userSysId) {
return '';
}
var userGR = new GlideRecord('sys_user');
userGR.get(userSysId);
if (userGR.isValidRecord()) {
return userGR.getDisplayValue('cost_center'); // Assuming 'cost_center' is the field name
}
return '';
},
type: 'GetUserCostCenter'
});
Output -
I have applied this script to Phone variable.
Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.
Thanks and Regards,
Ashish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-21-2024 07:16 AM
Hi @Ashish Parab ,
It's working, but when I attempt to change the 'Requested For' field, the 'Charge Number' value should clear. but it's remains the same value.
Regards,
Pradeepa Boopathi