Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Survey Notification

Juhi Chawla2
Tera Contributor

Hi Experts, 

 

I have a requirement where I have created the notification on the core case table and same notification is sent for all COEs. Now I want to insert a line in notification body to tell if this is a payroll case or benefits case or any other COE . I want to achieve this through an email script . Can any one of you suggest how we can do this . 

 

Thanks in advance !

8 REPLIES 8

G Ponsekar
Tera Guru

Hi @Juhi Chawla2 

 

Yes you can achieve through Email Script:
  1. Navigate to System Notification > Email > Notification Email Scripts.
  2. Click New.
  3. Fill out the form:
  4. Name: case_coe_identifier (or similar descriptive name)
    • Script:
// Get the display name of the COE from the current case record
	// The exact field name might vary (e.g., 'coe_code', 'topic_category.name', 'hr_service.name')
	// We will try using 'topic_category.name' which is common.

	var coeName = current.getValue('coe_code'); // Try this common field first

	if (!coeName) {
		// If coe_code is empty, try getting it from the HR Service or Topic Category reference
		if (current.hr_service.topic_category.name) {
			coeName = current.hr_service.topic_category.name;
		} else if (current.hr_service.name) {
			coeName = current.hr_service.name; // Fallback to HR service name		}
	}

	if (coeName) {
		template.print("This case is related to the **" + coeName + "** Center of Excellence.");
	} else {
		template.print("This case is related to a specific COE."); // Fallback if no specific COE is found	}

 

And In your notification include this email script

${mail_script:case_coe_identifier}

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

Thanks, GP

Sarthak Kashyap
Kilo Sage

Hi @Juhi Chawla2 ,

 

I tried your problem in my PDI and it works for me please Check steps below 

 

I created email script and add below code

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

    // Add your code here

    gs.info("current table = " + current.getValue('sys_class_name'));

	var tableName = current.getValue('sys_class_name');
	if(tableName == "sn_hr_core_case_payroll" || tableName == "sn_hr_core_case_total_rewards"){ // you can add your own tables here
		template.print("This is payroll and benefit table");
	}else{
		template.print("This is other table");
	}

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

SarthakKashyap_0-1763019044745.png

 

Add that email script in notification body

SarthakKashyap_1-1763019102874.png

 

 

Result 

For normal table

SarthakKashyap_2-1763019126616.png

For payroll/ benefit table 

SarthakKashyap_3-1763019150940.png

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards,

Sarthak

 

 

 

Hi @Sarthak Kashyap 

 

Can you help me how we can write the template.print if  I want the COE name mentioned in the below screenshot .if it is payroll we should be getting payroll in place of HR in the below screenshot.

 

 

Thanks a lot for your help in advance. 

Hi @Juhi Chawla2 ,

 

I tried to add COE name and HR Service please check below image

SarthakKashyap_0-1763020722015.png

Script 

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

    // Add your code here

    gs.info("current table = " + current.getValue('sys_class_name'));

	var tableName = current.getValue('sys_class_name');
	if(tableName == "sn_hr_core_case_payroll" || tableName == "sn_hr_core_case_total_rewards"){
		template.print("This is payroll and benefit table" + " Coe name = " + current.hr_service.topic_detail.topic_category.coe  + " HR Service = " + current.hr_service.getDisplayValue());
	}else{
		template.print("This is other table" + current.hr_service.topic_detail.topic_category.coe);
	}

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

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards,

Sarthak