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.

How can I define a Custom Global Variable?

G24
Kilo Sage

Hello.  I am aware of the magical ServiceNow global objects such as:

  gs - Available in Server Side code.

  g_form - Available in Client Side code.

 

My question is this:  Can I define and instantiate my own custom global variable which is also uber available and does not require instantiation by every piece of code which wants to make use of it?

 

I know that there are Classless Functions which work this way on the Server Side, but I'd kind of like for them to live inside an object, as opposed to floating around independently.  That way, I could simply type the name of my global object and then a DOT, and I would immediately see all of my magical functions which are available.

 

Make sense?  Please advise.  Thank you.

5 REPLIES 5

Community Alums
Not applicable

Hi @G24 ,

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

 

 

scott barnard1
Kilo Sage

Hi Geoffrey

The g_scratchpad can be accessed both client side and server side. 

You just build your g_scratchpad.element then access whatever value element has in that context from client scripts, policies, ui actions, business rules, workflow etc

Regards

G24
Kilo Sage

@Community Alums and @scott barnard1 Thanks for trying to help, but I don't think you guys are understanding what I'm trying to do.

 

I want to be able to define my own custom classes (objects) that I can call functions on, like gs.whatever() and g_form.whatever(), but my variable will be ggb, so I would call ggb.whatever().  Is there a way to make ggb visible everywhere gs is visible?

 

Without the need to say:

ggb = new class GGBClass(); // <-- Do not want to have to call this constructor.

ggb.whatever();  //Want to be able to make DIRECT global object function calls, like this.

 

That is the question.  Thanks.

CatQT
Tera Contributor

Hey-hey @G24,

 

first of all, it's a really cool question, I've even marked it in case someone actually finds out how to do it.

However, what I see is SNOW is not willing to allow randos to tinker with their library so easily. 😁

 

A straight forward workaround, that kinda does the same thing, but looks less fancy - is to have a combination of two things:

1. A global Script Include with whatever objects and their respective functions you want.

2. A macro(syntax editor macro) that will make a call for you. 

 

And the coolest thing is that you can just take those two entities and add them to your personal 'toolbox' installing them on any environment.