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

Auto tick checkbox using a client script

ngriffiths
Mega Expert

Hi all,

 

I need some help writing a client script.   I have a business request and since I am not a developer and have little to no coding experience, I am hoping someone out there can assist me.

 

Requirement:

 

On a request:

 

IF

Desktop checkbox (chk_Desktop) = true

OR

Laptop checkbox (chk_Laptop) = true

OR

Thin Client checkbox (chk_Thin_Client) = true

THEN

Computer Accessories checkbox (chk_Computer_Accessories) = true

IF NOT THEN

Computer Accessories checkbox remains false

1 ACCEPTED SOLUTION

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Agree with Kenneth.   Does this need to be dynamic or can this be done on submit or on the creation of the request?   If submit you can create an onSubmit client script to check it or a business rule to set it, or you could even do it in the workflow.



If you want it done dynamically when the user is filling out the request form, you will need 3 separate onChange client scripts for the chk_Desktop, chk_Laptop, chk_Thin_Client fields.   Below is an example for the chk_Desktop.   You can use the same code for the other two client scripts too, just change the Variable name and script name..



Client Script


Name: Check Computer Accessories on Desktop - or whatever you want


Type: onChange


Variable: chk_Desktop


Script:


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


      if (isLoading || newValue == '') {


              return;


      }


 


      if (newValue == "true") {


              g_form.setValue('chk_Computer_Accessories', "true");


      }


 


      if (newValue == "false") {


              if (g_form.getValue("chk_Desktop") == "false" && g_form.getValue("chk_Laptop") == "false" && g_form.getValue("chk_Thin_Client") == "false") {


                      g_form.setValue('chk_Computer_Accessories', "false");


              }


      }


 


}


View solution in original post

3 REPLIES 3

Ken83
Mega Guru

Your client script could go a couple of different ways. If you want this value to be set when the Request is created, then you need to do a Business Rule instead of a Client Script. If you want to see this only when the form loads, or when a certain value on the form changes, then the Client Script is sufficient. Anywho, this should work...



var dbox = g_form.getValue('chk_Desktop');


var lbox = g_form.getValue('chk_Laptop');


var tbox = g_form.getValue('chk_Thin_Client');



if(dbox || lbox || tbox){


        g_form.setValue('chk_Computer_Accessories', true);


}



You may need to test that with quotes around the true. I always forget if you need them or not for client scripts.


Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Agree with Kenneth.   Does this need to be dynamic or can this be done on submit or on the creation of the request?   If submit you can create an onSubmit client script to check it or a business rule to set it, or you could even do it in the workflow.



If you want it done dynamically when the user is filling out the request form, you will need 3 separate onChange client scripts for the chk_Desktop, chk_Laptop, chk_Thin_Client fields.   Below is an example for the chk_Desktop.   You can use the same code for the other two client scripts too, just change the Variable name and script name..



Client Script


Name: Check Computer Accessories on Desktop - or whatever you want


Type: onChange


Variable: chk_Desktop


Script:


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


      if (isLoading || newValue == '') {


              return;


      }


 


      if (newValue == "true") {


              g_form.setValue('chk_Computer_Accessories', "true");


      }


 


      if (newValue == "false") {


              if (g_form.getValue("chk_Desktop") == "false" && g_form.getValue("chk_Laptop") == "false" && g_form.getValue("chk_Thin_Client") == "false") {


                      g_form.setValue('chk_Computer_Accessories', "false");


              }


      }


 


}


Hi Kenneth and Michael,



To answer your questions it needs to be dynamic...basically there is a list of options for the user to select in the request.   When they select desktop then computer accessories needs to be auto selected.   Same if they were to select laptop or thin client.   However, if they are to select printer then computer accessories remains false.



Kenneth - I did try yours and unfortunately I needed to have it be dynamic.   Also, I did have to add quotes around true in order for it to work .



Michael - your dynamic onChange client script is what I need to do.  



Thanks for both of your help with this, I really appreciate it!



Best Regards,


Niccole