Organize your catalog variables in an e-mail using HTML

General Add comments
by:

Catalog variables can be placed in an email notification by scripting. This is easy.
Placing the variables in a nice table is more difficult, a little html knowledge is required.

Building the html code has to be done by using the template.print(”); functionality.

Normally you would build a table like this:

<mail_script>

template.print(‘ <table border = “1”>’);

template.print(‘<tr><td>Field</td> <td>Value</td></tr>’);

template.print(‘<tr><td> UserName</td><td>stefan</td>’);

template.print(‘<tr><td> Phone</td><td>0612345678</td></tr>’);

template.print(‘</table>’);

</mail_script>




However, Service-now wants to end the table as fast as possible and finds an end of a row (</tr>) often a good place to end the table.
Also Service-now can find the end of a table cell </td> a possibility to end the table.
A simple script like this will probably go alright, but when there is other code between the ‘template.
Print’ actions, the system adds code automatically, this will change the script with errors as a result.

To prevent this from happening, the code can be shuffled a bit. This way the system can not recognize that the cell or row has been ended/closed.

<mail_script>

template.print(” <table “);

template.print(‘border = “1”‘);

template.print(‘><tr><td>Field</td> <td>Value’);

template.print(‘</td></tr><tr><td> UserName’);

template.print(‘</td><td>stefan’ );

template.print(‘</td><tr><td> Phone’);

template.print(‘</td><td>0612345678’ );

template.print(‘</td></tr>’);

template.print(‘</table>’);

</mail_script>

With this knowledge we can make the mail script to visualize the variables of the sc_req_item.
Unfortunately we can not use functions in the script otherwise the system will also end the table somewhere in between the code.
Please use the comments in the code for guidance trough the script.

<mail_script>

template.print(“Summary of Requested item:\n”);

//quey the current request item

var gr = new GlideRecord(“sc_req_item”);

gr.addQuery(“sys_id”, current.request_item);

gr.query();

if(gr.next()) {

template.print(gr.number + “: “ + gr.cat_item.getDisplayValue() +” \n”);

template.print(” Options:\n”);

 

//start building the table

template.print(” <table “);

template.print(‘border = “1”‘);

template.print(‘><tr><td’);

template.print(‘ colspan=”2″>’);

template.print(‘<b><i>Field</td></b></i> <td><b><i>Value </b></i></td>’);

 

 

//Query the many 2 many table between the request item and the variables


var varown = new GlideRecord(‘sc_item_option_mtom’);

varown.addQuery(“request_item”, current.request_item);

varown.query();


while (varown.next()){


var visible = varown.sc_item_option.item_option_new.visible_summary;


var question = Packages.com.glideapp.questionset.Question.getQuestion(varown.sc_item_option.item_option_new);

question.setValue(varown.sc_item_option.value);

 

 


//if the question field and the value is not empty, the lable and the value is placed in the table


if (question.getLabel() != “” && question.getDisplayValue() != “” && visible == true){

template.print(‘</tr><tr><td’);

template.print(‘ colspan=”2″>’);

template.print(‘ ‘ + question.getLabel() );

template.print(“</td><td”);

template.print(‘ colspan=”2″>’);

template.print(question.getDisplayValue() );

template.print(“</td>”);

 

}

}template.print(‘</tr></table>’);

}

</mail_script>

 

Result:

Field

Value

Please Specify an Operating System 

Windows XP SP 2 

Which project code is this server to be billed to? 

abc 

What budget code should this server be billed to?

xyz 

Please Specify an Operating System 

Red Hat Enterprise Linux 5.0 

 

The script below has been made for a customer. The customer wanted to get more details of the users in the list collector. Using the colspan function to build the HTML table we are able to indent the list Collector values.
Please use the comments in the code for guidance trough the script.

<mail_script>

template.print(“Summary of Requested item:\n”);

 

//quey the current request item

var gr = new GlideRecord(“sc_req_item”);

gr.addQuery(“sys_id”, current.request_item);

gr.query();

if(gr.next()) {

template.print(gr.number + “: “ + gr.cat_item.getDisplayValue() +” \n”);

template.print(” Options:\n”);


//start building the table, the colspan

template.print(” <table “);

template.print(‘border = “1”‘);

template.print(‘><tr><td’);

template.print(‘ colspan=”2″>’);

template.print(‘<b><i>Field</td></b></i> <td><b><i>Value </b></i></td>’);

 


//Query the many 2 many table between the request item and the variables


var varown = new GlideRecord(‘sc_item_option_mtom’);

varown.addQuery(“request_item”, current.request_item);

varown.query();


while (varown.next()){


var visible = varown.sc_item_option.item_option_new.visible_summary;


var question = Packages.com.glideapp.questionset.Question.getQuestion(varown.sc_item_option.item_option_new);

question.setValue(varown.sc_item_option.value);

 


//if the question field and the value is not empty, the lable and the value is placed in the table


if (question.getLabel() != “” && question.getDisplayValue() != “” && visible == true){

template.print(“</tr><tr> <td”);

template.print(‘ colspan=”2″>’);

template.print(‘ ‘ + question.getLabel() );

template.print(“</td> <td”);

template.print(‘ colspan=”2″>’);

template.print(question.getDisplayValue() );

template.print(“</td> “);

 


/*

If the variable type = 21 (list collector) and the table the list collector retrieves information from = sys user

*/


if (varown.sc_item_option.item_option_new.type == ’21’ && varown.sc_item_option.item_option_new.list_table == ‘sys_user’){


var listCol = varown.sc_item_option.value.split(“,”);


for(i=0; i< listCol.length; i++)

{


var userref = new GlideRecord(‘sys_user’);

userref.addQuery(“sys_id”, listCol[i]);

userref.query();


if (userref.next())

{

template.print(‘</tr><tr><td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</td><td> Username’);

template.print(‘</td><td>’ );

template.print(userref.user_name);

template.print(‘</td><tr><td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</td><td> Email’);

template.print(‘</td><td>’ );

template.print(‘</td>’);

template.print(userref.email);

}

} } } } }

template.print(‘</tr></table>’);

</mail_script>


Result:

Field

Value

User 

stefan.bohncke

Project/Maintenance 

Maintenance 

Program Name 

Not Applicable 

Project code 

E10229

Task code 

Project phase 

— None — 

Person(s) 

Person 1 (team 26), Person 2(team 34), Person 3 (team 21), Person 4 (team 01)

       

Username 

Person 1 

       

Email 

Person_1@company.com

       

Username 

Person 2 

       

Email 

person_2@company.com

       

Username 

Person 3 

       

Email 

Person_3@company.com

       

Username 

Person 4 

       

Email 

Person_4@company.com

 

Please let me know if you have any questions.
You can contact me by mail stefan.bohncke@2e2.nl.

2 Responses to “Organize your catalog variables in an e-mail using HTML”

  1. Joel Says:

    i’d like to do something similar for an application i made where i’d like to email only the fields that have values or are true and exclude the blank or false fields. How might i go about doing that?

  2. Marcel Says:

    Hi, I was wondering if you are still monitoring this post if you might have any experience doing this same thing but using GlideappVariablePoolQuestionSet? I have a mail script that was created by a vendor working on a project for us that shows the questions and values for email notifications, but I’d like to get it into a formatted table to match our other notifications. Below is what I have currently, and it doesn’t fill the table properly (sets the first label and variable correctly, but then just puts the answers in the first column with no labels)

    template.print(“Information: “);
    var keys = [];
    var set = new GlideappVariablePoolQuestionSet();
    set.setRequestID(current.sys_id);
    set.load();

    //start building the table
    template.print(” ‘);
    template.print(‘FieldValue‘);

    var vs = set.getFlatQuestions();
    for (var i=0; i < vs.size(); i++) {

    //if the question field and the value is not empty, the label and the value is placed in the table
    if(vs.get(i).getLabel() != "" && vs.get(i).getDisplayValue() != "") {
    template.print('’);
    template.print(” + vs.get(i).getLabel());
    template.print(“‘);
    template.print(vs.get(i).getDisplayValue());
    template.print(“”);

    }
    }template.print(”);

Leave a Reply