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

Chris, I like the logic here and expected it to work but the script fails when it tries to create a new glide record.   No error message. The link stays highlighted and nothing happens.   Here's my code with alert embedded to figure out where the script fails



function addPatient(){


    alert("P1");


          var pvg_patient = new GlideRecord('x_166009_pocket_ve_pvg_test'); //This is the patient table


    alert("P2"); //Never gets to this point.   No error. Just nothing happens.


        pvg_patient.client_number = current.client_number;


  alert("P3");


      pvg_patient.insert();


  alert("P4");


      current.update();


  alert("P5");


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


  alert("P6");


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


}


Here's a screenshot of the UI Action settings:


find_real_file.png



Does anything pop-out as being wrong?   Thanks again for your help.   Much appreciated.


Hi Mike,



The reason your script isn't working is because of the alert() function. UI Actions, although they can do client side, by default are server-side scripts. Use gs.log() or something like that to print out checkpoints.



Edit:



Scratch that. I missed that you clicked "Client" on the form.


Oh I should have been more specific.   The alert("P1") is displaying but it doesn't get to "p2".   Haven't had a problem with using these alerts before but might not have used them in UI actions where the Client box was checked.   In any case, I put those alerts in because the script wasn't running and I wanted to see if I could figure out what line it didn't like.   Will remove them and try again.


Hi Mike,



You should also have initialize() in your script, it should read as the below



var pvg_patient = new GlideRecord('x_166009_pocket_ve_pvg_test'); //This is the patient table


pvg_patient.initialize();


    alert("P2"); //Never gets to this point.   No error. Just nothing happens.


        pvg_patient.client_number = current.client_number;


  alert("P3");


      pvg_patient.insert();



That should work fine.



Thanks


Also, I just looked up sysverbs and saw that there is one called sysverb_insert.   I'm wondering if I should be using that instead of the sysverb_new_assigned_to.   Not really sure what these things do.