The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Reusing Client Scripts

yundlu316
Kilo Guru

Our team is creating a number of record producers that utilize the same Client Scripts and Business Rules.   As of right now, we are just copying and pasting them individually into each record producer, but is wondering if there's a better way to do this.   The way we're currently doing this could be a pain to update or maintain in the future.   Is there a way to use UI Macros or something similar that would save the code and we can just call upon it in each of our record producers?   That way, if changes in the code arise, we wouldn't have to update the code in each record producer.

Thanks!

1 ACCEPTED SOLUTION

I don't know the details of your requirements, but this is how you can create shareable code for catalog items, including Record Producers:



1. Create a new Variable Set


2. Create a new onLoad Client Script as part of that Variable Set and include the following code:



function onLoad() {


    //this is just a placeholder, but is required


}



function u_sharedFunction1(){


  //add your code here


  alert("You called?");


}



function u_sharedFunction2(){


  //add your code here


}



The onLoad() snippet is just a placeholder and does not need to contain anything, but is nonetheless required to be there.   The u_sharedFunction1 and 2 are whatever shared functions you have.   Add as much as you need.   Adding them to the onLoad script makes them available within the Record Producers.



3. Add the Variable Set to whatever Record Producers needs it


4. Call one of the functions from a Client Script in one of your Record Producers.   Example:



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


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


  return;


  }



  u_sharedFunction1();


}



A "You called?" alert would appear on screen in the example above:


find_real_file.png




Now you have simple, shareable AND easily managed code available to your Record Producers without the overhead of Global UI Scripts.   The code is only loaded for those catalog items you add the Variable Set to.


View solution in original post

19 REPLIES 19

Jim Coyne
Kilo Patron

For the Client Scripts, you can actually create Variable Sets that only contain those scripts and add the Variable Sets to the Record Producers.   They do not have to contain any variables.



As for the Business Rules, you should be able to share them as well, I'd just need a bit more information in order to propose something.   What are they used for, what table are they running on? etc...


I don't know the details of your requirements, but this is how you can create shareable code for catalog items, including Record Producers:



1. Create a new Variable Set


2. Create a new onLoad Client Script as part of that Variable Set and include the following code:



function onLoad() {


    //this is just a placeholder, but is required


}



function u_sharedFunction1(){


  //add your code here


  alert("You called?");


}



function u_sharedFunction2(){


  //add your code here


}



The onLoad() snippet is just a placeholder and does not need to contain anything, but is nonetheless required to be there.   The u_sharedFunction1 and 2 are whatever shared functions you have.   Add as much as you need.   Adding them to the onLoad script makes them available within the Record Producers.



3. Add the Variable Set to whatever Record Producers needs it


4. Call one of the functions from a Client Script in one of your Record Producers.   Example:



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


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


  return;


  }



  u_sharedFunction1();


}



A "You called?" alert would appear on screen in the example above:


find_real_file.png




Now you have simple, shareable AND easily managed code available to your Record Producers without the overhead of Global UI Scripts.   The code is only loaded for those catalog items you add the Variable Set to.


amazing, thanks so much for the detailed suggestion!   Also thank you and hardikv for the explanation of best practices, very very useful.  


Hey Jim,



One follow up question regarding the onLoad placeholder, is there any significance with it being onLoad or can it be onSubmit or onChange as well?   Just curious.



Thanks!