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

Business rule advance Condition for script

dennisandrison
Kilo Expert

HI Developers.

 

I have the following script on a business rule:  

(function executeRule(current, previous /*null when async*/) {

//Get the Email record created against this Log
var grEmail = new GlideRecord("sys_email");
if(grEmail.get("sys_id", current.email)) {
//Check for a style associated with the Notification used
var grES = new GlideRecord("u_email_style");
var htmlStyle = "";
var strCSS = "";
var grSEA = new GlideRecord("sysevent_email_action");{
if(grSEA.get("sys_id", current.notification))

// if (grSEA.u_style == '') {
// //no style specifed, exit logic
// return;
// }
if(grES.get("sys_id", grSEA.u_style)) {
htmlStyle = grES.u_style_template;
strCSS = grES.u_css;
}
}
var strBody = grEmail.body;
//gs.log("DEBUG 1: Apply Email Style, strBody = " + strBody);
//gs.log("DEBUG 2: Apply Email Style, htmlStyle = " + htmlStyle);
strBody = strBody.replace("<html>", "");
strBody = strBody.replace("</html>", "");
strBody = strBody.replace("<body>", "");
strBody = strBody.replace("</body>", "");
htmlStyle = htmlStyle.replace("body_text", strBody);
//gs.log("DEBUG 3: Apply Email Style, htmlStyle = " + htmlStyle);

var strEmail = strCSS + "<html><head></head><body>" + htmlStyle + "</body></html>";
grEmail.body = strEmail;
grEmail.headers = grEmail.headers + "\n" + strCSS;
grEmail.autoSysFields(false);
grEmail.setWorkflow(false);
grEmail.update();
}
})(current, previous);

 

I only want this script to run when the sysevent_email_action.u_type is populated.   I have the following condition on the script current.sysevent_email_action.u_style!=""  but this is not working.  What is wrong with the condition? 

1 ACCEPTED SOLUTION

rzj
Giga Contributor

Your code is checking for sysevent_email_action.u_style but you said "I only want this script to run when the sysevent_email_action.u_type is populated." Is there a relation between u_type and u_style? What am I missing here?

View solution in original post

9 REPLIES 9

dennisandrison
Kilo Expert

sorry it should be u_style  not u_type 

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

You can dot-walk from this record to the notification record.  This said your condition should be:

current.notification.u_style!=""

rzj
Giga Contributor

try using gs.nil(grSEA.u_style) in your condition

 

dennisandrison
Kilo Expert

thanks   I got it to work with the following condition  !current.notification.nil()

 

 

current.notification will always be populated in the sys_email_log table so you really don't need this condition as it will always run.