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

newValue != oldValue



check the section 9.4



Client Script Best Practices - ServiceNow Wiki



whats your business need actually?


Hi Dinesh,



Good Day



Please have a look into the below script :-



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);


}


}



Please hit it as helpful/correct answer/like, based on the impact!



Thanks,


Priyanka R


PriyaRanji
Tera Guru

Hi Dinesh,



Good Day!



Please refer the below links which gives you an idea about the client scripts :-



http://wiki.servicenow.com/index.php?title=Client_Scripts#gsc.tab=0



http://wiki.servicenow.com/index.php?title=Client_Script_Best_Practices#gsc.tab=0



http://wiki.servicenow.com/index.php?title=Client_Scripting#gsc.tab=0



Please hit it as helpful/like/correct based on the impact.



Thanks,
Priyanka R


Ujjawal Vishnoi
Mega Sage

Hi Dinesh,



When you have requirement on any field, like you need any pop up or any client side action (like setvalue, mandatory etc) ONCHANGE of field then you should go with onchange script because you not need to submit your form in this case. But if requirement is same but only the triggering event is different means you need client action once the user submit the form then use onsubmit script.



On the other side whenever a page is loading then a document downloads at the client browser and you want to change some element of the document as they downloaded originally then you should use onload as it is running after the page downloaded to the browser and viewed on the screen.



Lastly, Oncelledit are the least used client script as they work on list view. Almost same as onchange but not used for form view this is written to handle on change on list view.



Hope this helps.



Regards


Ujjawal


Midhun1
Giga Guru

HI Dinesh,



It works on client level (Browser level).



onLoad: When a form loads (If you want to do any functionality/action to fields/variables while displaying the form)


onChange: When you are changing particular Fields/ Variables, (If you want to do any functionality/Validation to fields/variables on form based on a particular field)
onSubmit: when user tries to update/save the form.(If you want to do any Validation when a form is submitting)
onCelledit: Works on list view of the table, When you try to edit any field on list layout