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

SanjivMeher
Kilo Patron
Kilo Patron

Hi Mike,



You can't define such global variable. Can you provide me the use case you are working on?



Are you working on a Service Catalog or Regular forms? Where is the form, which have next button to take you to the next page.



Please mark this response as correct or helpful if it assisted you with your question.

Thank you, Sanjiv, for the quick reply.   Here is the use case;


I have a form called 'PVG Client' which contains a field called 'client_number.'


I have a related list on the bottom of the 'PVG Client' form that shows records from a second table called 'PVG Patients.'


The 'PVG Patients' table also has a field called 'client_number.'



When I am on the first form ('PVG Client'), I fill in the client_number and save the form.   Then, I click the NEW button on the related list which opens the new 'PVG Patient' form.   However, on the second form, the client_number is not filled in.   I want the value that I entered and saved on the first form to appear on the second form.



I tried creating my own UI action to use instead of using the NEW button on the related list but still can't figure out how to pass the parameter.   Here is what I have so far:


        current.update();


        gs.addInfoMessage('Creating new instance');  


        var inst = new GlideRecord("x_166009_pocket_ve_pvg_test");  


        var recordTarget = "x_166009_pocket_ve_pvg_test";     // <== I was hoping I could pass the client_number here but don't know if that is possible or how to do it.


      //insert new record  


          inst.insert();  


          gs.addInfoMessage('New Instance created');  


        // Navigate to the appointments new record form.  


          action.setRedirectURL(recordTarget);



But, I'm not sure this is even the correct approach.



Thanks you again for looking at this.


Hi Mike,



Is the client number field on PVG Patients table referencing the PVG Clients table?




Kr,


Alyssa


Hi Alyssa.   Yes, the client_number field on the PVG Patient form is a reference field that references the PVG CLient table.   Howver, the client_number field on the PVG Client table is a string.   Should they both be reference fields referring to each other?