Dynamically create URL in Email Notifications

tkh
Kilo Expert

We have some users who interact with SerivceNow via a separate web portal.   Management from the other portal has requested that the notifications for companies that use the other web portal use a URL for that portal.  So we have a check box on the companies that use the other web portal.

 

The ask is that for notifications if the "u_in_portal" is true the URL look something like https://portal.com/ticket_type/[sys_id]

the if "u_in_portal" is false use the regular ServiceNow URL to the record

Is there a way to dynamically add the URL to the email notification based on the "u_in_portal" filed on the company page?

1 ACCEPTED SOLUTION

tkh
Kilo Expert

Final solution

 

var num = current.sys_id; //quick easy grab of current record sys_id to use in building URL

var gr = new GlideRecord('change_request'); //New glideRecord on change_request table
gr.addQuery("sys_id", num); //Limits the record look up to just the current record sys_id
gr.query(); //executes the query
while (gr.next()); //while there is a next record (esentially for each record found) do the following

//If the check box is true (checked)
if (gr.u_checkbox == true){

//Start of string sentence to show in the email body
template.print("More details can be found ");

//create variable to incld the part of the URL that will remain constant + the current sys_id variable from above (num)
var pLink = "https://company.com/subsite/support/#/editChangeRequest/" + current.sys_id;

//create html anchor (<a href) to create the actual URL link using the word here as the text link
var pAnchor = "<a href='" + pLink +"'>" + "here." + "</a>";
template.print(pAnchor + "<br />");
}

//If the \check box ix false (not checked) then the URL link will be created the same way as above, but will link back to the record in ServiceNow
if (gr.u_checkbox == false){
template.print("More details can be found ");
var sLink = "https://instance.service-now.com/nav_to.do?uri=change_request.do?sys_id=" + current.sys_id;
var sAnchor = "<a href='" + sLink +"'>" + "here." + "</a>";
template.print(sAnchor + "<br />");
}

View solution in original post

5 REPLIES 5

John Ping
Mega Guru

Hi tkh,

You an do that with notification email scripts. See the documentation here: 

https://docs.servicenow.com/bundle/london-servicenow-platform/page/script/server-scripting/concept/c...

In the script, you can query your company table, build and print a URL appropriate to the situation, based on the caller in the record for example.

tkh
Kilo Expert

So I have a true/false field on the company table called u_inPortal.

This field has been added to the Change form as a reference field.

inside an email  notification script, I need to check the u_inPortal field of the current change record to see if it is true or false.

If True the email script will create one URL, if false it will create a different URL.

I have all the parts working except for the getting the true or false value of the u_inportal field it either always comes back false or as undefined.

 

Here is my script.

var num = current.sys_id;

dothis();

function dothis(){
var answer = current.company.u_inportal;
template.print("In Portal is "+ answer + "<br />"); //for testing
if (answer == true){
template.print("More portal details ");
var pLink = "https://console.test.cloud.ibm.com/managed-solutions/support/#/editChangeRequest/" + current.sys_id;
var pAnchor = "<a href='" + pLink +"'>" + "here" + "</a>";
template.print(pAnchor + "<br />");

}
if (answer == false){
template.print("More ServiceNow details ");
var sLink = "https://ibmmhasdev2.service-now.com/nav_to.do?uri=change_request.do?sys_id=" + current.sys_id;
var sAnchor = "<a href='" + sLink +"'>" + "here" + "</a>";
template.print(sAnchor + "<br />");

}
}

John Ping
Mega Guru

Does it come back as true if you check the box on one of the companies in a non-production environment? Usually on a true/false, I'll code for the true case - so:

if (answer == true) {

  // Do something

} else {

  // Do something else

}

That way I'll at least get the else case. (I feel like sometimes it will come back as undefined if the box has never been checked, but don't quote me on that.)

tkh
Kilo Expert

no matter whether the box is checked or not it comes back false.

maybe do a if not null then do this else do that.  🙂