How do you create a 'global' variable?

___miked___
Mega Contributor

I'm looking for a simple way to pass a variable between forms so that the variable value is set onSubmit of the first form and is read onLoad of the second form.   Totally new to ServiceNow and javaScripting so, after reading dozens of posts and following dozens of suggested links in those posts, I'm still coming up empty handed.   In VB, you'd simply declare a global variable and call it when needed.   Not sure what the counterpart is in JavaScript.   Any help you can provide would be appreciated -- especially if you have a code snippet that you've used to do this in the past.

Thanks in advance for your help.

1 ACCEPTED SOLUTION

Ah I see. Then I would say the UI Action is a good bet.



If you just want to create a record populating the one field with the one field from the current record is perform a glide record script before the current.update().


Something like this:



var pvg_patient = new GlideRecord('pvg_patients'); // of course replace with the real name of the table


pvg_patient.client_number = current.client_number;


pvg_patient.insert();



current.update();



action.setRedirectUrl(pvg_patient); //redirects them to the "PVG Patients" form


action.setReturnUrl(current);   //puts the previous form in this case 'PVG Client' form on the stack




And that should do it.


View solution in original post

26 REPLIES 26

Ok, so I tried this but haven't got it working.   I'm thinking it might be a global vs scoped application issue.   All of my tables are in a scoped app.   I mimiced the solution provided in this article and while it runs without error, nothing happens.   The only difference between my UI action and the one in the thread is that mine that the application field is not Global.   Posted a reply on that thread as well.   Here's my code.      



function AddPatient() {


var MyString = 'x_166009_pocket_ve_pvg_test.do?sys_id=-1&sysparm_query=client_number=' + g_form.getUniqueValue('sys_id') + '^client_number=' + g_form.getValue('client_number');


alert(MyString);


document.location = MyString;


}



Thanks again.   Very cool approach.


adityaghosh
Tera Guru

g_scratchpad may help but only in client side



e.g.


g_scratchpad.someName = "someValue";



var temp = g_scratchpad.someName ; //temp has the value of g_scratchpad.someName  



The g_scratchpad is an empty object you can use to push information (properties, objects, functions, etc.) from the server to the client. It is only available in display business rules and client scripts.


Thank you.   I'll try it.


Jamsta1912
Tera Guru

Hi Mike,



Another approach might be to create a system property, which you could set from one record, read from another, and so on...



Jamie.


That sounds like a cool approach but being a new developer in ServiceNow, I don't have a clue as to how to do this.   Will do some research.   Thanks for the pointer.