We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Mailscript for Notification coding help required

ChrisWing
Mega Guru

Upfront my grasp of Glide and coding in general is rudimentary and working on being more proficient but it's a challenge.  What I am trying to do is display specific variables based on the Approver's ID.  This mail script is called by a notification that is specific to this one catalogue item.  What seems to be happening is the validation piece isn't working though I have no errors.  Below is the script I have (cobbled together using a couple other community posts I have seen). Variable 1 and Variable 2 were the two paths I was going down without much success but hopefully someone can guide me and it is just a small change.

(function runMailScript(current, template, email, email_action, event) {
template.print("<b>Access requiring your approval:\n</b>");
var sysapprover = current.approver;
var getvariables=new GlideRecord('sc_req_item');
getvariables.addQuery('sys_id',current.sysapproval);
getvariables.query();
while(getvariables.next())
{
 if(current.getvariables.uar_approver_1 == sysapprover){
template.print('Access Details:'+getvariables.uar_reason_1);}
 if('uar_approver_2'+getvariables == 'sysapprover'){
 template.print('Access Details:'+getvariables.uar_reason_2);}
 if('uar_approver_3'+getvariables == 'sysapprover'){
 template.print('Access Details:'+getvariables.uar_reason_3);}
}
});

 

 

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron

If the notification calling this mail script is running on the sysapproval_approver table, then 'current' will refer to the approval record.  Technically, your 'while(getvariables... should be if(... since only one record will be returned, but in this case 'while' will also work.  Your if statement would look more like this to access the 'variables' object on the retrieved sc_req_item record:

if (getvariables.variables.uar_approver_1 == sysapprover) {
    template.print('Access Details:' + getvariables.variables.uar_reason_1);
}

There are still a bunch of reasons this could not be working - incorrect variable names or types,... so try adding some gs.info messages to log the progress.

 

View solution in original post

5 REPLIES 5

ChrisWing
Mega Guru

My issue was I had deleted the last line <current, template, email, email_action, event) which is required for the script to run, added it back in and everything worked with the suggestions from Brad before.  Thanks Gentlemen for the assistance.