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

Adding links from 2 different tables into an email notification

slalbiharie
Kilo Explorer

I have a change workflow. It is triggered for Routine and Emergency.
Within this workflow I have this condition: If Routine fire this notification, if not fire this one. That workds great
Within these notifications - that are both within the workflow itself, I need to be able to add a link to the Change as well as the Change Task. I have no problems for the change but I am not able to add the link to the change task itself.

Because this is based off of the change request table, can this even be done?

8 REPLIES 8

brad_hicks
Giga Expert

You can query the change task table for tasks that belong to this particular change request and then build out links to each of the change tasks that it finds. Here is an example:



<mail_script>
ctaskLinks();
function ctaskLinks() {
//Check for any tasks and add links if they exist
var gr = new GlideRecord('change_task');
gr.addQuery('change_request', current.sys_id);
gr.query();
if(gr.hasNext()){
template.print('Change Tasks: \n');
while (gr.next()) {
var link = createLinkForTask(gr.getTableName(), gr.sys_id, gr.number);
template.print(link + '\n');
}
template.print('<hr/>');
}
}

function createLinkForTask(strTableName, strSysID, strTaskNumber) {
return "<a href=\"" + gs.getProperty("glide.servlet.uri") + gs.generateURL(strTableName, strSysID) + "\">" + strTaskNumber + "</a>";
}
</mail_script>


Thank you for your prompt response, however, I am still a bit at a loss on how to accomplish this. Sorry


Thank you I was able to figure this out 🙂


MB26
ServiceNow Employee
ServiceNow Employee

I was just a little late. Brad beat me too it with a better example.

You would have to use a mail script (http://wiki.service-now.com/index.php?title=Mail_Scripts) to do a query for those change task(s), and add in a link into your email.

If you look under "System Policy --> Email Notifications" you should see one called "request.general" In there, they use a simple example of querying in a mail script.



<mail_script>
template.print("<p></p>Requested items:\n");

var gr = new GlideRecord("sc_req_item");
gr.addQuery("request", current.sys_id);
gr.query();
while(gr.next()) {
template.print(gr.number + ": " + gr.cat_item.getDisplayValue() + ", Stage: " + gr.stage.getDisplayValue() + "\n");
}
</mail_script>


You could query the Change task table (using a GlideRecord query, as above) where the parent = current.sys_id (which would be the Change Request). And use the "template.print" command to output what you want. If you want a URL you may need to build up and output an HTML link.


<a href="incident.do?sys_id=the-sys_id">gr.number</a>

using gr.sys_id to replace "the-sys_id".