URI_REF for ServicePortal in same email notification

bhuvan
Kilo Expert

Hi There,

I have an email notification, where I need to differentiate the hyper link of the Incident Number(URI_REF) to whom it is sending. For example, if the recipient user has any roles it needs to send to ServiceNow View, else to Service Portal View. I had created the below email notification script, but every time(User with roles or without roles) it send the same notification URL. Can any one give me some help on this please.

// Add your code here
function createLinkForObject(strTableName, strSysID){
return '<a href="' + gs.getProperty('glide.servlet.uri') + gs.generateURL(strTableName, strSysID) + '">Click here</a>';
}

var user = gs.getUser().hasRoles();
if(gs.getUser().hasRoles()){
//var html = '<a href="' + gs.getProperty('glide.servlet.uri') + 'sp?id=ticket&table=incident&sys_id=' + current.sys_id + '">Click here</a>';
//print the html variable
template.print('${URI_REF}');
}
else {
gs.log("Entered else");
var tbl = current.getTableName();
var sysID = current.sys_id;
var link = createLinkForObject(tbl,sysID);
template.print(link +" to see full details.");
}

1 ACCEPTED SOLUTION

Hi Ryan,

 

Thanks for the reply. If a notification is  sent to a group that has users both with roles and without roles not sure how to achieve this.

 

Thanks,

Bhuvan B.

View solution in original post

6 REPLIES 6

ryan_pope
Mega Guru

Using gs.getUser() is looking at the current session's logged in user (you), so you're always going to be directed to the backend view. I would suggest altering your email script to look at a field from the record triggering the notification (caller_id, opened_by, requested_for, etc.) instead.

Hi Ryan,

 

Thanks for the reply. If a notification is  sent to a group that has users both with roles and without roles not sure how to achieve this.

 

Thanks,

Bhuvan B.

I've accomplished this in the past using a mail script, a processor page, and a number of system properties to control certain elements a little more easily.

Here's an example of the mail script:

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {
	
	//name of the table extended off of task that houses that record
	var taskType = current.sys_class_name + '';
	//sys id of the task/task extension record
	var taskSys = current.sys_id + '';
	//get display value of the table for the button
	var taskName = current.sys_class_name.getDisplayValue();
	
	//url creation
	var base = gs.getProperty('glide.servlet.uri');
	var processor = 'ITSMEmailRedirect.do?';
	var tableParm = 'sysparm_table=' + taskType;
	var recordParm = '&sysparm_record=' + taskSys;
	var url = base + processor + tableParm + recordParm;
	
	//build button/hyperlink
	template.print('<a href="' + url + '"');
	template.print('>');
	template.print(gs.getMessage('Take me to the ') + taskName);
	template.print('</a>');
	template.print('<br><br>');
	
	
})(current, template, email, email_action, event);

And here's an example of the processor page:

(function process(g_request, g_response, g_processor) {
	var tableName = g_request.getParameter('sysparm_table');
	var recordId = g_request.getParameter('sysparm_record');
	var base = gs.getProperty('glide.servlet.uri');
	
        //spView controls whether they go to the form view or ticket view in portal
        var spView = gs.getProperty('redirect.ticket_or_form');
        //portalSuffix tells us what the suffix is for the portal (usually "sp")
	var portalSuffix = gs.getProperty('redirect.portal.suffix');	
	
	
	var frameSetUrl = base + 'nav_to.do?uri=%2F' + tableName + '.do?sys_id=' + recordId;
	var portalUrl = base + portalSuffix + '?id=' + spView + '&table=' + tableName + '&sys_id=' + recordId;
	
	var roledUsers = new GlideRecord('sys_user_has_role');
	roledUsers.addQuery('role.name', 'NOT IN', gs.getProperty('redirect.exclude.roles'));
//redirect.exclude.roles is a property containing roles that should be excluded from considering whether they should navigate to portal or backend view.
//ex. people with just the approval role should probably navigate to the portal, so that role should be added to this exclude property.
	roledUsers.addQuery('user', gs.getUserID());
	roledUsers.query();
		
	if(!roledUsers.next())
		g_processor.redirect(portalUrl);
	else 
		g_processor.redirect(frameSetUrl);
	
})(g_request, g_response, g_processor);

Hi Ryan,

 

Thanks for your reply. Can you please let me know what should be the properties as well.

gs.getProperty('redirect.portal.suffix');
gs.getProperty('redirect.exclude.roles')
gs.getProperty('redirect.ticket_or_form');

Thanks,

Bhuvan B.