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.

Script type dictionary entry

MKhan1
Giga Contributor

Hi,

Has anybody used dictionary entry of "script" type. If yes, Please tell me how do i use this dictionary entry?. How can we execute the code written in this dictionary entry?.

7 REPLIES 7

Jim Coyne
Kilo Patron

You can create fields of type "script" if you want, but they certainly will not be run anywhere. You cannot change the basic behavior of the SN platform like that, as it knows what tables contains code that it can run.


CapaJC
ServiceNow Employee
ServiceNow Employee

You can evaluate/execute a script in a field with Evaluator.evaluateString(string)

e.g., the following will print a line in the log:


Packages.com.glide.script.Evaluator.evaluateString("gs.log('hey there');");


Eventually when Packages calls are no longer allowed, this line would have to be changed to the following (but the following line won't work until the Calgary release):

GlideEvaluator.evaluateString("gs.print('heya');");


I know of one customer who created a field to store scripts, then had a UI Action on the form that used Evaluator.evaluateString to run the script. Something like the following:

Packages.com.glide.script.Evaluator.evaluateString(current.u_my_custom_script_field);


I'm sure I don't have to say that it would be extremely ill-advised to make something like this available to non-admins 🙂


MKhan1
Giga Contributor

Thanks a lot. i have been searching for that since a long time. Can you also tell me like what will 'current' refers to in these types of script. For example if i write
current.u_table == 'sc_request' // where u_table is a field on my table which references to all the tables.
will it refer to the current record? or the behaviour is different?.



CapaJC
ServiceNow Employee
ServiceNow Employee

Pretty sure "current" won't exist. In things like Business Rules, "current" is a GlideRecord object provided by the code running the business rules, set up to be the record the rules are running against. For an arbitrary script in a text field, there's nothing that will do that.

If you described in excruciating detail what you're trying to implement, what you want it to do and when, etc., someone in this community might have an idea of how you might go about it.