how to print the labels of True/False variables that are true on email templates?

humblecommitted
Kilo Guru

Hello community,

I have Change Requests and Service Requests (really any template that has True/False check boxes) that I need to populate or print out on email templates when they are "checked marked" or marked True.

Here are the fields on the Change Request:

1.2.jpg

Here are their respected variable element names in xml format:

<u_stx01>false</u_stx01>

<u_stx02>false</u_stx02>

<u_sys01>false</u_sys01>

<u_tim01>false</u_tim01>

<u_trn01>false</u_trn01>

<u_uat01>false</u_uat01>

<u_statehub>false</u_statehub>

<u_stg01>false</u_stg01>

<u_oem___monitoring>false</u_oem___monitoring>

<u_prd01>false</u_prd01>

<u_psd02>false</u_psd02>

<u_kolea>false</u_kolea>

<u_int01>false</u_int01>

<u_dev01>false</u_dev01>

<u_dev03>false</u_dev03>

<u_drx01>false</u_drx01>

<u_ecm>false</u_ecm>

I am assuming there is some sort of ${email_script:} that I would need to create to run some type of query to pull the labels of all the ones that are "true" and include that in the email template body but not sure how to capture that.   Maybe print them out in an array of elements that are true?  

Any and all help is greatly appreciated.   Thanks in advanced.

1 ACCEPTED SOLUTION

The more I look at what you have, it appears as if these are actual fields and not Variables so my solution would not be truly applicable.



Probably the best way would be to write a repeatable method for checking a field. You can actually pull together an array of fields and process each by surrounding the field name in brackets. Using your environments checkboxes as an example:



template.print('<p>Impact:<br />');


var fStr ='u_stx01,u_stx02,u_sys01,u_tim01,u_trn01,u_uat01,u_statehub,u_stg01,u_oem___monitoring'


var fArray = fStr.split(',');


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


      if(!current[fArray[i]]) {


              var label = current[fArray[i]].getLabel() + '<br />';


              template.print(label);


      }


}


template.print('</p>');



Since true/false values can be evaluated by pointing to the field itself, we just use the field in the evaluation. Typically we are doing a current.field_name combination, but since we are evaluating a slice of an array, we replace the dot accordingly: current[fArray[i]]


We also do the same with getting the label of the field using the getLabel() method: current[fArray[i]].getLabel()



This should get you started in writing a more concise script that will give you the Labels of the fields if they are true.



As for the Environment 1 and Environment 2 fields, you should be able to do a similar evaluation:



template.print('<p>Environment Tiers:<br />');


var fStr2 ='u_environment1,u_environment2'


var fArr2 = fStr.split(',');


for (x = 0; x < fArr2.length; x++) {


      if(current[fArr2[x]] != '') {


              var label = current[fArr2[x]].getLabel() + '<br />';


              template.print(label);


      }


}


template.print('</p>');



Let me know if you have any questions.


View solution in original post

13 REPLIES 13

joel_olives
Kilo Contributor

Take a look at the requested_items_summary_with_options mail script.     It will show you how to loop through the variable pool and display the variable labels and values in a notification.  


andrewdunn
Giga Expert

Hi Orlando - the following may assist https://community.servicenow.com/thread/145257


or https://community.servicenow.com/message/822538#822538




The specific line that will determine if the variable is printed is along the lines of:


if(vs.get(i).getDisplayValue() != 'No' && vs.get(i).getDisplayValue() != 'false' && vs.get(i).getDisplayValue() != '' && vs.get (i).getDisplayValue() != "-- None --"){



It is this check that determines if the if a check box gets printed.


Hope this helps


Cheers


Hello Andrew,



Is this in context/combination with the following script posted by Joel?


Hello Andrew,



So far I have the following code that I am playing around with.   I am by no means a coding expert and would appreciate additional assistance.


template.print("Summary of Change Request:<br />");




var gr = new GlideRecord("change_request");  


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


  gr.query();  


  while(gr.next()) {  


template.print(gr.number + ":   " + gr.quantity + " X " + gr.cat_item.getDisplayValue() + "\n");  


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


 


var varown = new GlideRecord('change_request');  


varown.addQuery("sys_id", current.sys_id);  


varown.query();  


while (varown.next()){  


var visible = varown.change_request.visible_summary;     //sc_item_option.item_option_new.visible_summary;  


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


question.setValue(varown.change_request.value);  


if(vs.get(i).getDisplayValue() != 'No' && vs.get(i).getDisplayValue() != 'false' && vs.get(i).getDisplayValue() != '' && vs.get (i).getDisplayValue() != "-- None --"){


  //(question.getLabel() != "" &amp;&amp; question.getDisplayValue() != "" &amp;&amp; visible == true){  


template.space(4);  


template.print('         ' +   question.getLabel() + " = " + question.getDisplayValue() + "\n");  


}  


}  


 


}  


So far the email only produces the following:


1.2.jpg



Thanks again for all this community does!   Happy Saint Patrick's day!