Input Parameters on Notification email script

joao_graca
Tera Contributor

Can I use input parameter in an email script?

I have this code on my notification's body

HTTP Status: ${u_notes}

${mail_script:ErrorReportNotificationScript}

<mail_script>errorReport(${u_notes});</mail_script>  

And this on my Email Script

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

	function errorReport(status){
		if (status >= 400 && status < 499) {
			template.print("<p>Client Error!</p>");	
		} else if (status >= 500 && status< 599) {
			template.print("<p>Server Error!</p>");	
		} else {
			template.print("<p>Error!</p>");	
		}
	}
	

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

I know that the code is not working but i put the code here just for an simple representation

2 ACCEPTED SOLUTIONS

Sandeep Rajput
Tera Patron
Tera Patron

@joao_graca Unfortunately, you can't pass any parameters directly in email script from a notification body. However, you can still pass parameters through event parameters (Event param 1 or param 2). You can set these param values while triggering the event.

 

Source: https://docs.servicenow.com/bundle/tokyo-servicenow-platform/page/script/server-scripting/concept/c_...

 

https://docs.servicenow.com/en-US/bundle/vancouver-build-workflows/page/administer/platform-events/c...

View solution in original post

Vishal Birajdar
Giga Sage

Hello joao_graca,

can you try this...!!

In the mail script you can get value of  "u_notes" by using current object. then you can call errorReport function

 

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

//Get the value of notes
var status = current.u_notes;

//call the function
errorReport(status);


function errorReport(status){

if (status >= 400 && status < 499) {

template.print("<p>Client Error!</p>");

} else if (status >= 500 && status< 599) {

template.print("<p>Server Error!</p>");

} else {

template.print("<p>Error!</p>");

}

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

 

Notification  body : 

HTTP Status: ${u_notes}

${mail_script:ErrorReportNotificationScript}

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

View solution in original post

3 REPLIES 3

Sandeep Rajput
Tera Patron
Tera Patron

@joao_graca Unfortunately, you can't pass any parameters directly in email script from a notification body. However, you can still pass parameters through event parameters (Event param 1 or param 2). You can set these param values while triggering the event.

 

Source: https://docs.servicenow.com/bundle/tokyo-servicenow-platform/page/script/server-scripting/concept/c_...

 

https://docs.servicenow.com/en-US/bundle/vancouver-build-workflows/page/administer/platform-events/c...

Tushar
Kilo Sage
Kilo Sage

Hi @joao_graca 

 

While you cannot directly pass parameters to an email script from a notification body, you can use event parameters to achieve a similar outcome.

Event parameters allow you to pass additional data when triggering an event.

 

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

    // Retrieve event parameters
    var parm1 = event.parm1;

    // Check if parm1 has a value
    if (parm1) {
        // Now you can use 'parm1' in your email script logic
        if (parm1.indexOf("400") !== -1 || parm1.indexOf("499") !== -1) {
            template.print("<p>Client Error!</p>");
        } else if (parm1.indexOf("500") !== -1 || parm1.indexOf("599") !== -1) {
            template.print("<p>Server Error!</p>");
        } else {
            template.print("<p>Error!</p>");
        }
    } else {
        template.print("<p>No HTTP Status found!</p>");
    }

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

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar

 

Vishal Birajdar
Giga Sage

Hello joao_graca,

can you try this...!!

In the mail script you can get value of  "u_notes" by using current object. then you can call errorReport function

 

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

//Get the value of notes
var status = current.u_notes;

//call the function
errorReport(status);


function errorReport(status){

if (status >= 400 && status < 499) {

template.print("<p>Client Error!</p>");

} else if (status >= 500 && status< 599) {

template.print("<p>Server Error!</p>");

} else {

template.print("<p>Error!</p>");

}

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

 

Notification  body : 

HTTP Status: ${u_notes}

${mail_script:ErrorReportNotificationScript}

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates