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

Hi Mike,



What you need to do is on form layout of PVG Patient, search for the parent record field PVG Client. expand the record and you will see the client number. Move it to the right. So you wont even need an additional field on PVG Patient. You can utilize the existing field.



But If you still want to populate the field in PVG Patient, what you can do is write a Before insert business rule.



In the Set Value tab, set Client number = PVG Client->Client Number



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

___miked___
Mega Contributor

So, here's how it ended up... works like a charm.   Could never have done this without all the great input posted by all of you today.   A special thanks to ChrisB for pointing me in the right direction and to   Chris Ironside for pointing out the need to initialize the new glide record.- Thanks everyone.



UI Action Script to Create a New record on a different table and populate it with a value of a field from the current form.:



        // Create a new record


              var inst = new GlideRecord("x_166009_pocket_ve_pvg_test");  



      //Specify the target table the new record is to be stored in


            var recordTarget = "x_166009_pocket_ve_pvg_test";



      //Get the client number entered on the current form


          var clientNo = current.getValue('client_number');



    //Initialize the new patient record


        inst.initialize();



  //Set the client number field in the new patient record to the current record's client number


      inst.client_number = clientNo;



  //Insert the record into the patient target table


    inst.insert();



  //Redirect the load back to the current form and update the form


    action.setRedirectURL(current);


    current.update();