The CreatorCon Call for Content is officially open! Get started here.

when we use client script types in Servicenow

Dinesh Kumar C
Tera Contributor

I see there are 4 types in client scripts

1.onLoad

2onChange

3.onSubmit

4.onCellEdit

When we use these functions in which scenario we segregate these functions ? For eg To validate mobile number format we can use onSubmit and onChange . Where is the exact difference between these 4 .

Thank you

1 ACCEPTED SOLUTION

PriyaRanji
Tera Guru

Hi Dinesh,



Good Day



Client Script : it will works in client (browser) side. Please find the details below for more information.



onLoad()



An onLoad() script runs when a form is first drawn and before control is given to the user to begin typing. Typically, you use an onLoad() script to perform some client side manipulation of the document on screen.



onChange()



An onChange() script can run when a particular field changes value or when the form is first drawn and no fields have changed value from previous script actions. As such, onChange scripts can potentially act as onLoad scripts with extra conditions.



onCellEdit()



Scripts can be defined as onCellEdit to run on the client side when the list editor interacts with a cell.



onSubmit()



An onSubmit() script runs when a form is submitted. Typically, you use an onSubmit() script to validate things on the form and ensure that the submission makes sense. As such, onSubmit()scripts can potentially cancel a submission by returning false.



To validate mobile number format :- please refer the below thread which helps you out!



Client side number validation



Thanks,


Priyanka R


View solution in original post

10 REPLIES 10

adilrathore
ServiceNow Employee
ServiceNow Employee

onChange would be the best for validating mobile number format. Here are some examples that you can refer to:



Client Script Examples — ServiceNow Elite


Thank you Adil , But could you please explain more when we use these functions ?


Hi Dinesh,



1.onLoad : when ever you load or refresh the browser then onLoad() will work. eg: you have filled some value there and save the form then the script you had mentioned there that will execute after the loading of the form.


2onChange : doing some changes on form field before saving the form. eg: changing on category making assigned to field mandatory.


3.onSubmit : when ever you submit the form it will work.


4.onCellEdit: from thew list view if you made any changes on column then you need to use onCellEdit client script.



Note: getValue() setValue() and many more functions does not work in onCellEdit().


Onchange() can run on form loading as well as changing in field value.



Client Scripts - ServiceNow Wiki


Thank you Harsha ,



I have another issue on below script . In my form short description is by default mandatory . I would like to write a client script on existing forms like if the old and new value hasnt changed then that field shouldnt be mandatory   , If value of that particular field changes then automatically it should mandaory . Where i am doing wrong?



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


    if (isLoading   ) {


          return;


    }


        if (oldValue !== newValue)


{


          g_form.setMandatory('short_description', true);


}


else


{


        g_form.setMandatory('short_description', false);


}


   


}