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.

Prefix Subject of Certain Email Notifications only if Service = 'ABC'

Leon Els
Tera Expert

Greetings friends,
I have a new requirement to Prefix email Notifications if Service = 'ABC', then the email subject should start with 'ABC - '.
Ideally, I would prefer not creating duplicate email notifications just for this Service and was hoping to use a script if possible.
Already consulted ChatGPT/CoPilot, could not get their recommended solutions to work.
Thanks in advance for any guidance.

Happy 2025 in advance!

2 ACCEPTED SOLUTIONS

@Leon Els 

use this in the IF condition

if(current.business_service.toString() == 'Give Your Exact Business Service Name Here')

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

Hi @Leon Els ,

 

Your text is incorrect, let me give you text.

 if (current.business_service && current.getDisplayValue('business_service') == 'SAP Enterprise Services') {
        // Prefix the subject with 'ABC - '
       
		email.setSubject('ABC - Incident ' + current.number + 'has been created - '+ current.short_description);

        // if getSubject() wont work then simply type your subject 
    }else{
		email.setSubject('Incident - ' + current.number + 'has been created - '+ current.short_description);
	}

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

View solution in original post

18 REPLIES 18

@Leon Els 

it would be something like this

email.setSubject('ABC - Incident ' + current.number + ' has been Created - ' + current.short_description);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi @Leon Els ,

 

Your text is incorrect, let me give you text.

 if (current.business_service && current.getDisplayValue('business_service') == 'SAP Enterprise Services') {
        // Prefix the subject with 'ABC - '
       
		email.setSubject('ABC - Incident ' + current.number + 'has been created - '+ current.short_description);

        // if getSubject() wont work then simply type your subject 
    }else{
		email.setSubject('Incident - ' + current.number + 'has been created - '+ current.short_description);
	}

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

Success!!

LeonEls_0-1735649649158.png




Below is the final code that worked for me and I would not have been able to accomplish this without @Ankur Bawiskar  & @Runjay Patel , thank you both SOO much!!

__________

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

    // Check if the business service is 'ABC'
    if (current.business_service && current.getDisplayValue('business_service') == 'ABC') {
        // Prefix the subject with 'ABC - '
        email.setSubject('ABC - Incident ' + current.number + ' has been Created - ' + current.short_description);
    } else {
        email.setSubject('Incident ' + current.number + ' has been Created - ' + current.short_description);
    }

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

@Leon Els 

Glad to know.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader