How to add affected CI's to email notifications using Notification Email Scripts

calissev
Giga Contributor

I am a newbie and am trying to create a script in Notification Email Scripts (Eureka Version) that I can use in any email Notification and/or Template.   The affected CI's list resides in the task_ci table.   Within the task_ci table there is a field 'task' that has the Change_Request number in it.   The affected CI's are in a field called 'ci_item'.   I don't understand how to define this script when it doesn't have a qualifier for what table it is coming from. When I create an email notification it is created referencing a table.   In the cases that I am interested in are the "change_request" and "sysapproval_approver" as table types for the email notifications.   So the script I have so far:

template.print("<p></p>Affected CI's:<br />");

var taskCI = new GlideRecord("task_ci");

taskCI.addQuery("task", current.change_request.number);

taskCI.addQuery("ci_item");

taskCI.query();

while(taskCI.next()) {

  template.print(taskCI.ci_item.getDisplayValue() + "<br />");

}

What this script is giving me is every affected CI.   What I am trying to get at is for the 'task' field that matches the 'change_request.number' of the event that fired this email request loop through and print the 'ci_item' associated with this 'task'.

                                                                                   

task_ci.list

                                                                                                                                  ci_item                                                                                                                                           task

affectedci.PNG

How do I do this so that when I fire the event that is to mail something. I capture the variables that I need for this script so that it can add all the affected CI's for the particular change to a list on the email.

Any help would be greatly appreciated.

Thanks,

Calisse

1 ACCEPTED SOLUTION

calissev
Giga Contributor

I figured it out.   I ended up not using the Notification Email Scripts section and just adding it to the email within the mail script tags.   Since I wasn't sure how it would know which record I was on I figured I would start by just embedding it so that it knew it was using the change_request table.



Here is what worked on an email template that references the change_request table.   I still have to work on the one for the sysapproval_approver table email



<mail_script>



template.print("<p></p>Affected CI's:<br />");



var taskCI = new GlideRecord("task_ci");


taskCI.addQuery("task", current.sys_id);     //no idea why this works


taskCI.query();


while(taskCI.next()){


template.print(taskCI.ci_item.getDisplayValue() + "</br>");


}


</mail_script>



Business rule on the change_request table.   After rule



var taskCI = new GlideRecord("task_ci");


taskCI.addQuery("task", current.sys_id);


taskCI.query();


while(taskCI.next()){


gs.print("Task:" + taskCI.task.getDisplayValue());   //to see if the task matched the change number


  gs.print("Configuration Item:" + taskCI.ci_item.getDisplayValue());   //the items I wanted.


}



If someone can please explain to me why when querying the task_ci.task it matched with the change_request table on current.sys_id I would appreciate it.   I kept trying to taskCI.addQuery("task", current.number);. When I would print current.number I would get what I thought I wanted to query to but it wasn't right.   Go figure. Also to check this since I am a newbie and wasn't sure how to check scripts I put it in a business rule so that I could see with gs.print was was returning.  


View solution in original post

2 REPLIES 2

SteveS
Kilo Expert

The script might need to be corrected but really depends on what table your notification is setup for. If it's setup on the Change Request table (change_request) then you'd need to do something like this



template.print("<p></p>Affected CI's:<br />");


var taskCI = new GlideRecord("task_ci");


taskCI.addQuery("task", current.change_request);


taskCI.query();




while(taskCI.next()) {


  template.print(taskCI.ci_item.getDisplayValue() + "<br />");


}



If your notification is on the task_ci table then you'd need to do something like this.



template.print("<p></p>Affected CI's:<br />");


var taskCI = new GlideRecord("task_ci");


taskCI.addQuery("task", current.task);


taskCI.query();




while(taskCI.next()) {


  template.print(taskCI.ci_item.getDisplayValue() + "<br />");


}



Again it really depends on what table the notification is setup for.


calissev
Giga Contributor

I figured it out.   I ended up not using the Notification Email Scripts section and just adding it to the email within the mail script tags.   Since I wasn't sure how it would know which record I was on I figured I would start by just embedding it so that it knew it was using the change_request table.



Here is what worked on an email template that references the change_request table.   I still have to work on the one for the sysapproval_approver table email



<mail_script>



template.print("<p></p>Affected CI's:<br />");



var taskCI = new GlideRecord("task_ci");


taskCI.addQuery("task", current.sys_id);     //no idea why this works


taskCI.query();


while(taskCI.next()){


template.print(taskCI.ci_item.getDisplayValue() + "</br>");


}


</mail_script>



Business rule on the change_request table.   After rule



var taskCI = new GlideRecord("task_ci");


taskCI.addQuery("task", current.sys_id);


taskCI.query();


while(taskCI.next()){


gs.print("Task:" + taskCI.task.getDisplayValue());   //to see if the task matched the change number


  gs.print("Configuration Item:" + taskCI.ci_item.getDisplayValue());   //the items I wanted.


}



If someone can please explain to me why when querying the task_ci.task it matched with the change_request table on current.sys_id I would appreciate it.   I kept trying to taskCI.addQuery("task", current.number);. When I would print current.number I would get what I thought I wanted to query to but it wasn't right.   Go figure. Also to check this since I am a newbie and wasn't sure how to check scripts I put it in a business rule so that I could see with gs.print was was returning.