Can we use "Current" object in Script Include? Please be specific. Yes or No.

SwarnadeepNandy
Mega Sage

Can we use "Current" object in Script Include? Please be specific. Yes or No.
If 'Yes', then how? Shouldn't we need to pass the "Current" object to the function or instantiate a GlideRecord object in the script include function?

Thanks in advance.

1 ACCEPTED SOLUTION

Indeed. Here goes an example using a classless Script Include:



Script include Name: sayHello


Script:



function sayHello(){


  gs.addInfoMessage(current.number);


}



I can then call that script include from an incident onBefore insert/update Business Rule with the script:



function onBefore(current, previous) {


    sayHello();


}




And upon an insert or update of an incident it will display the incident number as an Info Message in the screen.



Thanks,


Berny


View solution in original post

14 REPLIES 14

You mean it will work only for client callable script include? Or else it will not wrok?


Yes. If you follow Berny's example above.


bernyalvarado
Mega Sage

Hi swarnadeepnandy,



Do you have any further questions or can we close this thread by marking the responses as helpful/correct?



Thanks,


Berny


The SN Nerd
Giga Sage
Giga Sage

Another Use Case - If your UI Action conditions are hard to read, you can put them in Script Includes instead.


Make sure your naming convention makes it clear that the function is in the context of a particular record type.



Example



Script Include: IncidentContext



var IncidentContext= Class.create();


IncidentContext.IsResolvedAndFPOC = function() {


return (


        current.state == 10


        && current.u_fpoc == true


        && current.u_long_custom_field == 'meaningless string'


);


}



UI Action Condition


IncidentContext.IsResolvedAndFPOC();



Much easier to read!


You can now immediately understand what your condition is looking for.



Don't fall into the trap of naming is something like IsUIActionBlaVisible - because that tells you nothing about what your code is actually checking for.



Hope this helps.



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Swarnadeep,



Yes, You can use the current object in script include provided you pass it through initialize() function and in the initialize() function


have this this.currentObj = current;



This variable this.currentObj can then be used throughout the script include functions.



Mark my reply as Correct and also hit Like and Helpful if you find my response worthy.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader