Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Dipenduroy/f59e50f4cd6b1123927d4bafffac7e1b to your computer and use it in GitHub Desktop.
Save Dipenduroy/f59e50f4cd6b1123927d4bafffac7e1b to your computer and use it in GitHub Desktop.

Revisions

  1. @nguyen0096 nguyen0096 revised this gist Jul 2, 2020. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions ServiceNow - Jelly scripting cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ modal.setPreference('sysparm_varName', value);
    Impact: ${jvar_var_name}
    ```

    * To use those `sysparm` variable in client script, create a hidden input that hold the value then use DOM manipulation to value of hidden field
    * To use those `sysparm` variable in client script, create a hidden input that hold the value then use DOM manipulation to value of hidden field (*see snippet 3.1, 3.2*)
    ```html
    <!-- Snippet 3.1 -->
    <input id="var_name" value="${sysparm_varName}" hidden="true" />
    @@ -43,7 +43,7 @@ var value = gel('var_name').value;

    * You can use `g:evaluate` tag evaluate an expression written in javascript which will be executed in server-side. If you need to use Jelly variable inside, set attribute `jelly="true"`

    * To loop through array, use `j:forEach`
    * To loop through array, use `j:forEach` (*see snippet 4*)
    ```html
    <!-- Snippet 4 -->
    <g:evaluate>
    @@ -68,8 +68,9 @@ var value = gel('var_name').value;
    </j:forEach>
    ```

    * For UI macro, you can put a script into HTML. Expression is working on all HTML code, include parts enclosed in `script` tag [Ref](https://community.servicenow.com/community?id=community_question&sys_id=91925401dbad1f80032a7a9e0f961999)
    * For UI macro, you can put a script into HTML. Expression is working on all HTML code, include parts enclosed in `script` tag [Ref](https://community.servicenow.com/community?id=community_question&sys_id=91925401dbad1f80032a7a9e0f961999) (*see snippet 5*)
    ```html
    <!-- Snippet 5 -->
    <?xml version="1.0" encoding="utf-8" ?>
    <j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
    <j:set var="jvar_n" value="ip_rep_src_ip_${ref}" />
    @@ -96,9 +97,10 @@ var value = gel('var_name').value;
    </j:jelly>
    ```

    * RenderProperties API is provided inside Jelly script [API official docs](https://developer.servicenow.com/dev.do#!/reference/api/orlando/server_legacy/RenderProperites-API#RP-getEncodedQuery)
    * RenderProperties API is provided inside Jelly script [API official docs](https://developer.servicenow.com/dev.do#!/reference/api/orlando/server_legacy/RenderProperites-API#RP-getEncodedQuery) (*see snippet 6*)
    * If you use an UI macro on a form (using a formmatter), to get sys_id of the record, you can query parameter [Ref]()
    ```html
    <!-- Snippet 6 -->
    <g:evaluate var="sys_id">
    var sys_id = RP.getParameterValue("sys_id");
    sys_id;
  2. @nguyen0096 nguyen0096 renamed this gist Jul 2, 2020. 1 changed file with 11 additions and 14 deletions.
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,9 @@
    # UI Page/Macro and Portal Widgets
    # ServiceNow - Jelly scripting cheatsheet

    ## UI Page
    * UI Page consists of jelly, client script and processing script. UI Macro has the jelly script only (but client script can be injected in jelly script)
    * Usages:
    * Open as a page
    * Open as a modal using client script
    UI Page consists of jelly, client script and processing script. UI Macro has the jelly script only (but client script can be injected in jelly script)
    Usages:
    * Open as a page using URI `https://<instance_name>.service-now/<ui_page_name>.do`
    * Open as a modal using client script (UI action, client script, etc) (*see snippet 1*)
    ```js
    // Snippet 1
    var gm = new GlideModal('UI_dialog_name');
    @@ -13,14 +12,13 @@ gm.setWidth(550);
    gm.render();
    ```

    * To pass variable from client script to modal, use `setPreference` method of GlideModal
    * To pass variable from client script to modal, use `setPreference` method of GlideModal. In UI page, to print `sysparm` variables, use `<j:set>` tag set a jvar variable. Note that var name must have `jvar_` prefix in order to be printed to HTML (*see snippet 2.1, 2.2*)
    ```js
    // Snippet 2.1
    // Replace ui_page_name with your UI Page name, varName, value with your variable name and value
    var modal = new GlideModal('ui_page_name');
    modal.setPreference('sysparm_varName', value);
    ```
    * In UI page, to print `sysparm` variables, use `<j:set>` tag set a jvar variable. Note that var name must have `jvar_` prefix in order to be printed to HTML
    ```html
    <!-- Snippet 2.2 -->
    <j:set var="jvar_var_name" value="${sysparm_varName}"/>
    @@ -32,19 +30,18 @@ Impact: ${jvar_var_name}
    <!-- Snippet 3.1 -->
    <input id="var_name" value="${sysparm_varName}" hidden="true" />
    ```

    ```js
    // Snippet 3.2
    var value = gel('var_name').value;
    ```

    * To submit form so that processing script can be executed, use `<g:ui_form>` tag with a `<g:dialog_button type="submit">`. Button element can also execute an onSubmit function that return true if the form is okay to submit.
    * To submit form so that processing script can be executed, use `<g:ui_form>` tag with a `<g:dialog_button type="submit">`. `<g:dialog_button>` has `onSubmit` attribute which is a function that return true if the form is okay to submit, otherwise, return false.

    * Similar to client script, you will need a hidden field but for processing script, you just use the `name` attribute of input field as variable name. Note that `input` elements should be placed inside `g:ui_form` so that its values can be sent to server as form data.
    * To pass variable to processing script, similarly to client script, you will need a hidden field but for processing script, you just use the `name` attribute of input field as variable name. Note that `input` elements should be placed inside `g:ui_form` so that its values can be sent to server as form data.

    * ServiceNow supports extension jelly tags `g:ui_form`, `g:ui_input_field`, `g:ui_checkbox`, `g:dialog_buttons_ok_cancel`, `g:ui_reference`

    * You can use `g:evaluate` tag evaluate an expression written in javascript which will be executed in server-side. If you need to use Jelly variable inside, set attribute `jelly="true"`. `g:evaluate` is an extensions for jJelly tag.
    * You can use `g:evaluate` tag evaluate an expression written in javascript which will be executed in server-side. If you need to use Jelly variable inside, set attribute `jelly="true"`

    * To loop through array, use `j:forEach`
    ```html
    @@ -71,7 +68,7 @@ var value = gel('var_name').value;
    </j:forEach>
    ```

    * For UI macro, you can put a script into HTML. Expression is working on all HTML code, include parts enclosed in `script` tag [Refs](https://community.servicenow.com/community?id=community_question&sys_id=91925401dbad1f80032a7a9e0f961999)
    * For UI macro, you can put a script into HTML. Expression is working on all HTML code, include parts enclosed in `script` tag [Ref](https://community.servicenow.com/community?id=community_question&sys_id=91925401dbad1f80032a7a9e0f961999)
    ```html
    <?xml version="1.0" encoding="utf-8" ?>
    <j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
    @@ -99,7 +96,7 @@ var value = gel('var_name').value;
    </j:jelly>
    ```

    * RenderProperties API is provided inside Jelly script [Docs](https://developer.servicenow.com/dev.do#!/reference/api/orlando/server_legacy/RenderProperites-API#RP-getEncodedQuery)
    * RenderProperties API is provided inside Jelly script [API official docs](https://developer.servicenow.com/dev.do#!/reference/api/orlando/server_legacy/RenderProperites-API#RP-getEncodedQuery)
    * If you use an UI macro on a form (using a formmatter), to get sys_id of the record, you can query parameter [Ref]()
    ```html
    <g:evaluate var="sys_id">
  3. @nguyen0096 nguyen0096 created this gist Jul 2, 2020.
    109 changes: 109 additions & 0 deletions ServiceNow - Jelly scripting.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,109 @@
    # UI Page/Macro and Portal Widgets

    ## UI Page
    * UI Page consists of jelly, client script and processing script. UI Macro has the jelly script only (but client script can be injected in jelly script)
    * Usages:
    * Open as a page
    * Open as a modal using client script
    ```js
    // Snippet 1
    var gm = new GlideModal('UI_dialog_name');
    gm.setTitle('Show title');
    gm.setWidth(550);
    gm.render();
    ```

    * To pass variable from client script to modal, use `setPreference` method of GlideModal
    ```js
    // Snippet 2.1
    // Replace ui_page_name with your UI Page name, varName, value with your variable name and value
    var modal = new GlideModal('ui_page_name');
    modal.setPreference('sysparm_varName', value);
    ```
    * In UI page, to print `sysparm` variables, use `<j:set>` tag set a jvar variable. Note that var name must have `jvar_` prefix in order to be printed to HTML
    ```html
    <!-- Snippet 2.2 -->
    <j:set var="jvar_var_name" value="${sysparm_varName}"/>
    Impact: ${jvar_var_name}
    ```

    * To use those `sysparm` variable in client script, create a hidden input that hold the value then use DOM manipulation to value of hidden field
    ```html
    <!-- Snippet 3.1 -->
    <input id="var_name" value="${sysparm_varName}" hidden="true" />
    ```

    ```js
    // Snippet 3.2
    var value = gel('var_name').value;
    ```

    * To submit form so that processing script can be executed, use `<g:ui_form>` tag with a `<g:dialog_button type="submit">`. Button element can also execute an onSubmit function that return true if the form is okay to submit.

    * Similar to client script, you will need a hidden field but for processing script, you just use the `name` attribute of input field as variable name. Note that `input` elements should be placed inside `g:ui_form` so that its values can be sent to server as form data.

    * ServiceNow supports extension jelly tags `g:ui_form`, `g:ui_input_field`, `g:ui_checkbox`, `g:dialog_buttons_ok_cancel`, `g:ui_reference`

    * You can use `g:evaluate` tag evaluate an expression written in javascript which will be executed in server-side. If you need to use Jelly variable inside, set attribute `jelly="true"`. `g:evaluate` is an extensions for jJelly tag.

    * To loop through array, use `j:forEach`
    ```html
    <!-- Snippet 4 -->
    <g:evaluate>
    var grQuestion = new GlideRecord('u_mn_feedback_category');
    grQuestion.orderBy('sys_created_on');
    grQuestion.query();
    var questions = [];
    var rowCount = grQuestion.getRowCount();

    while (grQuestion.next()) {
    questions.push({
    'id': grQuestion.u_name+'',
    'question': grQuestion.u_question+''
    });
    }
    </g:evaluate>
    <j:forEach var="jvar_question" items="${questions}">
    <div class="form-group">
    <label>${jvar_question.question}</label>
    <input class="form-control" id="${jvar_question.id}" name="${jvar_question.id}" />
    </div>
    </j:forEach>
    ```

    * For UI macro, you can put a script into HTML. Expression is working on all HTML code, include parts enclosed in `script` tag [Refs](https://community.servicenow.com/community?id=community_question&sys_id=91925401dbad1f80032a7a9e0f961999)
    ```html
    <?xml version="1.0" encoding="utf-8" ?>
    <j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
    <j:set var="jvar_n" value="ip_rep_src_ip_${ref}" />
    <span id="${jvar_n}" onChange="onChange('${ref}')" onclick="ip_rep_src_ip('${ref}')" title="IP Reputation" alt="IP Reputation" tabindex="0" class="btn btn-default icon-search">
    <span class="sr-only">IP Reputation</span>
    </span>

    <script>
    function onChange(element, original, changed, loading) {
    var visibility = 'visible';
    var sysID = g_form.getValue('u_source_ip_s');
    if (!sysID)
    visibility = 'hidden';
    var e = gel('${jvar_n}'); // Here, Jelly will evaluate this expression and replace it by the value of that variable
    e.style.visibility= visibility;
    }
    function ip_rep_src_ip(reference){
    window.open('https://www.talosintelligence.com/reputation_center/lookup?search=' + encodeURI(g_form.getValue('u_source_ip_s')));
    }
    </script>
    </j:jelly>
    ```

    * RenderProperties API is provided inside Jelly script [Docs](https://developer.servicenow.com/dev.do#!/reference/api/orlando/server_legacy/RenderProperites-API#RP-getEncodedQuery)
    * If you use an UI macro on a form (using a formmatter), to get sys_id of the record, you can query parameter [Ref]()
    ```html
    <g:evaluate var="sys_id">
    var sys_id = RP.getParameterValue("sys_id");
    sys_id;
    </g:evaluate>
    ```