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.

RITM variables in email template script

sterrell24
Mega Expert

Hi everyone,

I am a ServiceNow rookie so there is probably an easy solution to what I am about to ask.

We have an email template that sends information based on the script below. I would like to add the variables/questions at the RITM level to be included in the email.  I would like to create a query to get return the variables/questions, and loop through each and dynamically add the entries to the email. Since the variables are different for various catalog items, I don't want to hardcode variable names.

Thanks,
Shane

 

-------------------------------

Click here to view Request:

 

Number:

Due date:

Opened:

Approval:

 

<mail_script>

  template.print("<p></p>Requested items:\n");

 

  var gr = new GlideRecord("sc_req_item");

  gr.addQuery("request", current.sys_id);

  gr.query();

  while(gr.next()) {

    var stage = gr.stage.getDisplayValue();

    if (JSUtil.nil(stage))

        stage = gr.stage.getChoiceValue();

    template.print(gr.number + ":  " + gr.cat_item.getDisplayValue() + ", Stage: " + stage + "\n");

  }

</mail_script>

1 ACCEPTED SOLUTION

sterrell24
Mega Expert

Got it going. I found another post that referenced how to loop through variables. Updated script below.


---------------------


Click here to view Request: ${URI_REF}




Number: ${number}


Due date: ${due_date}


Opened: ${opened_at}


Approval: ${approval}




<mail_script>


  template.print("<p></p>Requested items:\n");




  var gr = new GlideRecord("sc_req_item");


  gr.addQuery("request", current.sys_id);


  gr.query();


  while(gr.next()) {


      var stage = gr.stage.getDisplayValue();


      var misc = gr.variable_pool.alt_poc;


      if (JSUtil.nil(stage))


              stage = gr.stage.getChoiceValue();


      template.print(gr.number + ":   " + gr.cat_item.getDisplayValue() + ", Stage: " + stage + "</br>");



for (key in gr.variables) {


          var v = gr.variables[key];


          if(v.getGlideObject().getQuestion().getLabel() != '') {


                template.space(4);


                template.print('         ' +   v.getGlideObject().getQuestion().getLabel() + " = " + v.getDisplayValue() + "\n");  


          }


      }


  }


</mail_script>


View solution in original post

3 REPLIES 3

Anurag Tripathi
Mega Patron
Mega Patron

Hi Jeromy,



This should work



<mail_script>


var item = new GlideRecord("sc_req_item");


item.addQuery("request", current.sys_id);


item.query();


while(item.next()) {


var catalogItem = item.number + ': ' + item.cat_item.getDisplayValue();


var misc = item.variable_pool.alt_poc;


template.print(catalogItem + "<br/> Field: " + misc);


}


</mail_script>


-Anurag

Thanks for the reply. I saw the community post where that script applied, but I don't have a variable called of alt_pos. I am wanting to look through all variables for the item and display each, not just specific ones.


sterrell24
Mega Expert

Got it going. I found another post that referenced how to loop through variables. Updated script below.


---------------------


Click here to view Request: ${URI_REF}




Number: ${number}


Due date: ${due_date}


Opened: ${opened_at}


Approval: ${approval}




<mail_script>


  template.print("<p></p>Requested items:\n");




  var gr = new GlideRecord("sc_req_item");


  gr.addQuery("request", current.sys_id);


  gr.query();


  while(gr.next()) {


      var stage = gr.stage.getDisplayValue();


      var misc = gr.variable_pool.alt_poc;


      if (JSUtil.nil(stage))


              stage = gr.stage.getChoiceValue();


      template.print(gr.number + ":   " + gr.cat_item.getDisplayValue() + ", Stage: " + stage + "</br>");



for (key in gr.variables) {


          var v = gr.variables[key];


          if(v.getGlideObject().getQuestion().getLabel() != '') {


                template.space(4);


                template.print('         ' +   v.getGlideObject().getQuestion().getLabel() + " = " + v.getDisplayValue() + "\n");  


          }


      }


  }


</mail_script>