Question: sc_task client script, on variable edit.

Scott29
Kilo Sage

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".

Scott29_0-1740015705724.png

 

 

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?

 

Scott29_1-1740015871670.png

 

 

1 ACCEPTED SOLUTION

Veer
Tera Guru

@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.

View solution in original post

3 REPLIES 3

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Scott29 

 

You can use onchange as well. Oncell edit work at list view. 

 

https://www.servicenow.com/community/developer-forum/how-to-get-value-of-a-variable-from-sc-task-in-...

https://www.servicenow.com/community/developer-forum/client-script-on-catalog-task-table-to-access-v...

https://www.servicenow.com/community/now-platform-blog/what-is-the-best-way-to-make-catalog-task-var...

 

*************************************************************************************************************
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]

****************************************************************************************************************

Veer
Tera Guru

@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.

Thanks, I wouldn't have thought about running the script from the catalog item directly.