- 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 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-25-2024 12:07 AM
I’m very glad that I could help you!
Thank you so much, @Pradeepa B , for accepting my solution. If you could also mark it as helpful, it would be greatly appreciated.
I hope to assist you in the future as well! 😊
Thanks and Regards,
Ashish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2025 03:30 AM
Hi @Ashish Parab ,
Could you please assist me with the same requirement, but with the "onload".
Previously, the user requested the functionality to trigger when the value of "Charge Method" was changed. However, they now want the "Charge Number" value to be displayed without modifying the "Charge Method" value.
I have set the default value for "Charge Method" as "ABC2".
Your help on this would be greatly appreciated.
Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2025 04:26 AM
Hello @Pradeepa B ,
I am assuming that the requested for value is auto-populating at the time of loading, and the charge method is ABC 2.
You can try the below onLoad client script. Please adjust the code as per your requirement and appropriate field values.
function onLoad() {
var chargeMethod = g_form.getValue('charge_method');
var requestedFor = g_form.getValue('requested_for');
// Check if the selected value of Charge method is 'ABC 2'
if (chargeMethod === 'ABC 2') {
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', '');
g_form.addInfoMessage("Requested for is empty");
}
} else {
// Clear the Charge number field if Charge method is not 'ABC 2'
g_form.setValue('charge_number', '');
}
}
Do not forget to mark my solution as "correct" and "helpful" if you feel this answer helped you in anyway. Hope this will work for you!
Thanks and Regards,
Ashish