The CreatorCon Call for Content is officially open! Get started here.

To Send Notification through Advanced Condition and need help on Email Script to set email body

rishabh31
Mega Sage

Dear Team,

 

I am stuck in the requirement, where I have a Catalog Item (Access Change Request) and want to trigger a notification email to some users based on the condition that when in this catalog item, a checkbox variable 'SAP Real Estate' is TRUE (Checked)

rishabh31_0-1688825605857.png

Now I don't want to achieve this through Workflow as I do not have a workflow for this catalog item, So I created a Notification and set below 'When to Send' Advanced Condition Script for this particulate catalog item.

rishabh31_1-1688825790880.png

'What it will contain'

rishabh31_3-1688825879551.png

For practising purposes, I want to set the email body through email script, thus I created an Email Script and called it in this Notification (what it will contain) as shown in the above screenshot.

 

The Email Body Should be:

rishabh31_4-1688826553547.png

For the above Email Body I tried the below Email Script but not working as it's not displaying any content (Checked through 'Preview Notification' button).

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */ event) {

    // Get Variables
    var reqFor = current.variables.requested_for.getDisplayValue(); //Variable 'requested_for' is Reference 
    var prop = current.variables.properties_this_change_affect.getDisplayValue(); //Variable 'properties_this_change_affect' is Reference 
    var effDt = current.variables.effective_date; //variable 'effective_date' is Date type

 template.print('Hi,');
 template.print("<br>"); //To Line Break
 template.print('There is a new Workiva change for SAP Real Estate. Please find the details below:');
 template.print("<br>"); //To Line Break
 template.print('User: ');
 template.print(reqFor);
 template.print("<br>"); //To Line Break
 template.print('Location: ');
 template.print(prop);
 template.print("<br>"); //To Line Break
 template.print('Effective Date: ');
 template.print(effDt);
 template.print("<br>"); //To Line Break
 template.print('Thank you,');
 template.print("<br>"); //To Line Break
 template.print('Service Desk');


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

rishabh31_5-1688826936430.png

rishabh31_6-1688826993030.png

 

Requesting help on

1st- I checked the Email logs and did not find any notification email triggered, this means the notification is not triggered, seems I wrote the wrong advanced condition script or something, please help with advanced script condition so that It will get triggered (PS: through normal Condition filter I can dot walk like 'show related fields>variables>-------' but I want this to be achieved through Advanced Condition Script to access the Check box variable of this catalog item)

2nd- The Email body is not displaying content as set through Email Script, all the Variable Name is Correct, but still not displaying any content. (PS: This can be directly applied on Notification Body, but for practising I want this to be displayed through Email Script)

 

Thanks in Advance, need help for self-learning purposes.

 

 

 

2 ACCEPTED SOLUTIONS

Pavankumar_1
Mega Patron

Hi @rishabh31 ,

In future try to ask those as separate questions not as a single question then it will be easy to understand and others will refer aswell.

 

1. for advance condition try below code

 

var grRITM = new GlideRecord("sc_req_item");
grRITM.addQuery('sys_id',current.sys_id);//to get the current RITM
grRITM.addQuery("cat_item", "cbd3f97b97e4111097af9300f153af71");//catlog item sysid
grRITM.query();
if (grRITM.next()){
	var sapVal = grRITM.variables.u_sap_real_estate;//give your variable name
	if (sapVal){
		answer = true;
	}
}

 

Screenshot (821).png

2. To call email script in notification use below 

${mail_script:name_of_your_mail_script}

you given  email instead of mail

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

View solution in original post

Hello Sir,

 

Just to revise the script to be more performance-optimized and when there is more than 1 such check box. Then the script would be:

var grRITM = new GlideRecord("sc_req_item");
grRITM.addQuery('sys_id',current.sys_id);//to get the current RITM
grRITM.addQuery("cat_item", "cbd3f97b97e4111097af9300f153af71");//catlog item sysid
grRITM.query();
if (grRITM.next()){
	var sapVal = grRITM.variables.u_sap_real_estate;//give your variable name
	if (sapVal == "true"){ //Pass 'true' here
		answer = true;
	}
}

 

View solution in original post

3 REPLIES 3

Pavankumar_1
Mega Patron

Hi @rishabh31 ,

In future try to ask those as separate questions not as a single question then it will be easy to understand and others will refer aswell.

 

1. for advance condition try below code

 

var grRITM = new GlideRecord("sc_req_item");
grRITM.addQuery('sys_id',current.sys_id);//to get the current RITM
grRITM.addQuery("cat_item", "cbd3f97b97e4111097af9300f153af71");//catlog item sysid
grRITM.query();
if (grRITM.next()){
	var sapVal = grRITM.variables.u_sap_real_estate;//give your variable name
	if (sapVal){
		answer = true;
	}
}

 

Screenshot (821).png

2. To call email script in notification use below 

${mail_script:name_of_your_mail_script}

you given  email instead of mail

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Thank you @Pavankumar_1 Sir, This is working fine🙂. Solution Marked Helpful and Accepted as Solution.

Sure, will ask separate questions for such asks.

 

Hello Sir,

 

Just to revise the script to be more performance-optimized and when there is more than 1 such check box. Then the script would be:

var grRITM = new GlideRecord("sc_req_item");
grRITM.addQuery('sys_id',current.sys_id);//to get the current RITM
grRITM.addQuery("cat_item", "cbd3f97b97e4111097af9300f153af71");//catlog item sysid
grRITM.query();
if (grRITM.next()){
	var sapVal = grRITM.variables.u_sap_real_estate;//give your variable name
	if (sapVal == "true"){ //Pass 'true' here
		answer = true;
	}
}