How to run a same script on 2 tables ?

AbdulAzeez
Mega Guru

Hello Everyone,

I have a client script which i wanted to run on 2 tables like Incident & task table ?

How can i achieve this ?

1 ACCEPTED SOLUTION

Hi Abdul,



The Inherited functionality, alone, on a client script would be useful only if you would want that script to run all the extended tables of a base table.



However, since in your case, you want the script to run only on Incident and Catalog Task table, you can do the following:




1) Create the client script on the base table, i.e. TASK


2) Check the Inherited checkbox on the script


3) In the starting of the script, use the following condition to ensure that the script is running only on Incident and Catalog Task table and not on other inherited tables from the TASK table:


        function onLoad() {    


                  var table = g_form.getTableName();


                  if (table == 'incident' || table == 'sc_task')


                  {


                            //alert('Run my script');


                            //place your code here


                  }


}




This should work and will make this script run on only the Incident and the Catalog Task tables extended from Task. Let me know if it doesn't.



PS: Mark this answer as Correct if it solved your query or hit Like/Helpful depending upon the usefulness of the response.



Regards


Supriya Bisht


View solution in original post

18 REPLIES 18

lks
ServiceNow Employee
ServiceNow Employee

Hello Abdul,



Though, I am still not fully clear with your requirement but considering the table names as incident and catalog task, I would suggest you better go with a separate catalog client script for your catalog task. It will make the things easy to manage in future. Please remember if you write a client script on Task table and use the inherit checkbox, it will apply to all the tables that extends Task table therefore, be careful when choosing this option.



This is true that when you make the script as inherited and then add a condition to verify the table name (incident or catalog task) for further execution, the script will completely run for incident and catalog task only but it will be evaluated for every table that inherits the task table. However, you will be able to achieve your objective here but it will certainly impact your instance performance in the long run.



And, when you write two different scripts one for incident and another for catalog task table, the scripts will evaluate and run only for mentioned tables and not for all tables that extends task. Option this solution, will keep your instance performance better in the long run.


Although this is an old question, I just wanted to add my opinion.

Whereas using 1 inherited Client Script on the Task table is not "wrong", it will do the trick, it is not the best idea in terms of performance as it runs on every single Task-derived table.  Using 2 separate Client Scripts specifically on the Incident and Catalog Task tables would have been the better way to handle the situation.

Although this is an old question, I just wanted to add my opinion.

Whereas using 1 inherited Client Script on the Task table is not "wrong", it will do the trick, it is not the best idea in terms of performance as it runs on every single Task-derived table.  Using 2 separate Client Scripts specifically on the Incident and Catalog Task tables would have been the better way to handle the situation.

So @lks 's suggestion would be the preferred method.

Shilpi Sharma2
Tera Contributor

Hi All,

 

I want to Know how to achieve above functionality for on change client script.

Please help