Email Script to populate different variables based on different catalog

Appy2
Tera Contributor

Hi All,

My requirement is to create one notification for 2 different catalogs and notification should contain variable of particular catalog item.For example i have 2 catalogs:

Catalog 1(Server Request ) have fields "Server 1,Server 2 and Server 3"

Catalog 2(database Request ) have field "database 1,database 2 and database 3"

notifications should be triggered once RITM get created and this notification should contains "Server 1 and Server 2" if Catalog 1 gets submitted and "database 1,database 2" should be populated it Catalog 2 gets submitted.

how we can achieve this with one email script?

If any idea please share with me.

Thanks in Advance.

 

1 ACCEPTED SOLUTION

Ankur Swami
Tera Guru

Hi,

You can write the email script and call it in your notification.

 

Name = "multiple_cat_item_example"

 

(function runMailScript(current, template, email, email_action, event) {
    (function runMailScript(current, template, email, email_action, event) {

        // Add your code here
        var gr = new GlideRecord('sc_cat_item');
        gr.addQuery('sys_id', current.sys_id);
        gr.query();
        if (gr.next())

        {
            if (gr.cat_item == 'sys_id_of_catalog1') {
                template.print(current.variables.server_1);
                template.print(current.variables.server_2);
                template.print(current.variables.server_3);
                email.setSubject("For catalog item 1");
            }

           else  if (gr.cat_item == 'sys_id_of_catalog2') {
                template.print(current.variables.database1);
                template.print(current.variables.database2);
                template.print(current.variables.database3);
                email.setSubject("For catalog item 2");
            }
        }

    })(current, template, email, email_action, event);

})(current, template, email, email_action, event);

 

Now call the email script in the notification What it will Contain >> Message HTML

${mail_script:multiple_cat_item_example}

View solution in original post

10 REPLIES 10

Hi,

 

Please check if you are getting the sys_id of the user in 'current.variables.owner' by applying the logs.

If it is coming then you can do another glide record on sys_user table something like:

 

var glr = new GlideRecord('sys_user');

glr.addQuery('sys_id','current.variables.owner');

glr.query();

if(glr.next())

{

email.setReplyTo(glr.email);

}

 

Thanks for the quick response Ankur

but its not working.

 var gr1 = new GlideRecord('sys_user');
gr1.addQuery('sys_id', current.variables.owner);
gr1.query();
while(gr1.next()) {
template.print("in while 1 <br />"+current.variables.owner;
template.print("in while 2 <br />"+current.variables.owner.email);
email.setReplyTo(gr1.email);
}
}

its going in while loop and prining correct values,even its printing gr1.email also correctly but not sending notification to owner.

Kindly help

You mean you want to set the recipients. 

I am sure you can not add the recipients directly from the mail script.

for that you must need to use the gs.eventQueue('eventName', object, recipient) in your email script and you need to create two different notifications those will be event based.

 

Your email script will be same for both the notifications but the notifications will be separated for different catalogs.

 

Is there another query i can help you with?

 

Thanks,

Ankur

No, Thanks Ankur for your help 🙂

Appy2
Tera Contributor

Hi Ankur,

we same requirement (populating variables based on catalog) but this time its on when approval is getting triggered.

so this my notification is on approval table and my mail script should do glide on "sysapproval_approver" right?

can you please help me with the query? how can we access variable from below code?

var grA= new GlideRecord('sysapproval_approver');
grA.addQuery('sys_id', current.sys_id);
grA.query();
while (appr.next()) {
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('sys_id', grA.sysapproval);
ritm.query();
while (gr.next()) {
if (ritm.sys_id == 'XXXXXXXXXXXXXXXXXXXXX') { 

template.print("Test: " + current.variables.category1);//not sure how can we access variable from here
email.setSubject("For catalog item 1");


} else if (ritm.sys_id == 'YYYYYYYYYYYYYYYYYYYYYYYYY') { 


ttemplate.print("Test: " + current.variables.category1);
email.setSubject("For catalog item 1 " + current.number);
}

}
}

Thank you in Advance!