Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Reuse data in across client scripts

kiwi07
Tera Contributor

I have created a catalog with  3 fields and i have 1 onLoad script and 1 onChange script , in onLoad we do a REST call and get the data but i have to use a portion of the same data in onChange as well is there a way to store the REST call data in an object and access it across or is that a way i can set the whole data in g_form somehow and then resue it?

1 ACCEPTED SOLUTION

@kiwi07 

 

It should be string and you can mention the size as 5000.

for storing the data use g_form.setValue('DATA', JSON.stringify(response));

for getting the same data use : var data = JSON.parse(g_form.getValue('DATA'));

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

View solution in original post

3 REPLIES 3

Prince Arora
Tera Sage

@kiwi07 ,

 

1) Create a new variable lets say "DATA" and make it hidden using the UI policy or on load client script (g_form.getDisplay('DATA','false')) and when you made API call into on load client script store the data into "DATA" variable using g_form.setValue('DATA',response_from_API_call) and get the value into onchange client script using g_form.getValue('DATA') and use i accordingly.

2) Second way is you can store data into window's session:
sessionStorage.setItem("Data", response_from_API_call); // storing data into the session
var data = sessionStorage.getItem("Data"); //getting data from the session


If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

@Prince Arora  what should be the type of "DATA" variable 

@kiwi07 

 

It should be string and you can mention the size as 5000.

for storing the data use g_form.setValue('DATA', JSON.stringify(response));

for getting the same data use : var data = JSON.parse(g_form.getValue('DATA'));

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.