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.

How to repeat css style inside Email Script Template - template.print()

Elton2
Tera Contributor

Hi everyone! How are you?!


I am using the Email Script (Template script - Email Notification), including It's done, but I did the inline css style for each <th>, so to reduce the number of lines of code and reuse them (because they're the same) image_1, I thought about creating an Array of Objects and using a for() loop and inside the "For" block insert the template.print (image_2):

 

var arryStyleElement = [{
            Label: 'Name Company',
            Name: 'u_company',
            TH: 'th'
        },
        {
            Label: 'Name Software',
            Name: 'u_software',
            TH: 'th'
        },
        {
            Label: 'Area',
            Name: 'u_area',
            TH: 'th'
        },
        {
            Label: 'New Department',
            Name: 'u_department',
            TH: 'th'
        },

    ];

    for(var i = 0; i < arryStyleElement.length; i++){
    template.print('<th style="font-size:12px; border:1px solid #000;background: #BFE1D0">' + arryStyleElement[i].TH + '</th>');
   
    }


template.print('<th style="font-size:12px; border:1px solid #000;background: #BFE1D0">Name Company</th>');

 

In this case, the customization would apply to each <th>, without the need to write the lines of code several times, since they are the same, but is not working.

 

Would anyone have any tips?


I thank!

1 ACCEPTED SOLUTION

Prince Arora
Tera Sage

@Elton2 

 

Could you try something like this:

 

var arryStyleElement = [{

Label: 'Name Company',

Name: 'u_company',

TH: 'th'

},

{

Label: 'Name Software',

Name: 'u_software',

TH: 'th'

},

{

Label: 'Area',

Name: 'u_area',

TH: 'th'

},

{

Label: 'New Department',

Name: 'u_department',

TH: 'th'

},

];

var str = "<table><tr>";

for(var i = 0; i < arryStyleElement.length; i++){

str = str + '<th style="font-size:12px; border:1px solid #000;background: #BFE1D0">' + arryStyleElement[i].TH + '</th>';

}

str = str + "</tr></table>";

template.print(str);

 

 

If my answer solved your issue, please mark my answer as  Correct & 👍Helpful based on the Impact.

View solution in original post

2 REPLIES 2

Prince Arora
Tera Sage

@Elton2 

 

Could you try something like this:

 

var arryStyleElement = [{

Label: 'Name Company',

Name: 'u_company',

TH: 'th'

},

{

Label: 'Name Software',

Name: 'u_software',

TH: 'th'

},

{

Label: 'Area',

Name: 'u_area',

TH: 'th'

},

{

Label: 'New Department',

Name: 'u_department',

TH: 'th'

},

];

var str = "<table><tr>";

for(var i = 0; i < arryStyleElement.length; i++){

str = str + '<th style="font-size:12px; border:1px solid #000;background: #BFE1D0">' + arryStyleElement[i].TH + '</th>';

}

str = str + "</tr></table>";

template.print(str);

 

 

If my answer solved your issue, please mark my answer as  Correct & 👍Helpful based on the Impact.

Hi @Prince Arora , how are you?!

I really want to thank you!
I performed the test here with your code and initially it worked, it just crashed, because you called the variable <table> <tr> at the same time. example var str = "<table><tr>".
Thank you very much!