- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 05:45 PM
I am trying to create a client script on the sc_task to populate the following fields when "device ordered" is populated
Order date: current date
Ordered by: The person who selected the "device ordered".
Is the best way to do that with a client script type of onCellEdit, and Field name of Variables? If so where do I select the variable within the script?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 06:29 PM
@Scott29
Do not write client script on sc_task instead write it on the catalog item which is creating that task.
The client script should be onChange on Device ordered field and applies to target record.
Here is the sample script below.
function onChange(control, oldValue, newValue, isLoading) {
// Only run if the form is not in "loading" state
if (isLoading) {
return;
}
// Check if the "Device Ordered" field is set to true
if (newValue == 'true') {
// Set the Order Date to the current date
g_form.setValue('order_date', new Date().toISOString().split('T')[0]); // Set today's date
// Set the Ordered By field to the current user's name
g_form.setValue('ordered_by', g_user.name);
} else {
// If the device is not ordered, clear the fields
g_form.setValue('order_date', '');
g_form.setValue('ordered_by', '');
}
}
Please mark this as helpful / accepted solution if its resolves your issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 05:55 PM
Hi @Scott29
You can use onchange as well. Oncell edit work at list view.
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 06:29 PM
@Scott29
Do not write client script on sc_task instead write it on the catalog item which is creating that task.
The client script should be onChange on Device ordered field and applies to target record.
Here is the sample script below.
function onChange(control, oldValue, newValue, isLoading) {
// Only run if the form is not in "loading" state
if (isLoading) {
return;
}
// Check if the "Device Ordered" field is set to true
if (newValue == 'true') {
// Set the Order Date to the current date
g_form.setValue('order_date', new Date().toISOString().split('T')[0]); // Set today's date
// Set the Ordered By field to the current user's name
g_form.setValue('ordered_by', g_user.name);
} else {
// If the device is not ordered, clear the fields
g_form.setValue('order_date', '');
g_form.setValue('ordered_by', '');
}
}
Please mark this as helpful / accepted solution if its resolves your issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 06:55 PM
Thanks, I wouldn't have thought about running the script from the catalog item directly.