Search code examples
htmlcssservicenow

ServiceNow storing and calling upon glide scripts


I am creating a front end portal in ServiceNow that requires using g:evaluate to call on variables from different tables.

For example:

<g:evaluate var="jvar_user_name"> 
var gr = new GlideRecord('sys_user'); 
gr.get('sys_id', gs.getUserID()); 
gr.first_name; 
</g:evaluate>

Is there a way to store a bunch of these somewhere and then call upon them as needed, much like UI Scripts for JS and Style Sheets for CSS? If so, how would I go about doing this?

Thanks!


Solution

  • Yes you should be able to do this by using UI Macros.

    You could create a UI Macro called set_user_name_var like below:

    <?xml version="1.0" encoding="utf-8"?>
    <j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
    <g:evaluate var="jvar_user_name"> 
        var gr = new GlideRecord('sys_user'); 
        gr.get('sys_id', gs.getUserID()); 
        gr.first_name; 
    </g:evaluate>
    </j:jelly>
    

    And then invoke the UI Macro any place where you can write jelly code, like so:

    <g:set_user_name_var />
    

    or alternatively:

    <g:macro_invoke macro="set_user_name_var" />
    

    And from that point on, the jvar_user_name jelly variable will exist and be able to be accessed.

    For example:

    <?xml version="1.0" encoding="utf-8"?>
    <j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
    <g:set_user_name_var />
    ${jvar_user_name}
    </j:jelly>