Narsing1
Mega Sage

How to make Notification/Email Links dynamic when accessing via Desktop & Mobile

This Article provides you an idea on how you can make a link that acts dynamically based on the Device Type that you are accessing the link.

Use Case: 

  • I want to send a notification/email to the Fulfiller when the Requestor submits a catalog item request.
  • I want to provide a link within that Email to direct him to take to the RITM to see what Requestor submitted
  • If the Fulfiller opens the email using his Mobile, the RITM should opens using the Servicenow Agent Mobile App & if the Fulfiller opens the email using desktop, it should redirect him to the resolver view.

 

Servicenow API: MobileDeepLinkGenerator

There is an OOTB API called MobileDeepLinkGenerator where you can utilize this to construct the URL dynamically.

Reference: Click here

 

Step by Step Configuration

  • Create a Portal Page and name it as “devicetype”
  • Create a Script Include called “LinktoOpen” and add the below code
  • var LinktoOpen = Class.create();
    LinktoOpen.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    	getLink: function() {
    		
    		var mb = this.getParameter('sysparm_mobile');
    		var tbl = this.getParameter('sysparm_tbl');
    		var sysid = this.getParameter('sysparm_sysid');
    		var link = 'sc_req_item.do?sys_id=' + sysid;
    		if(mb) {
    			var deepLinkGenerator = new global.MobileDeepLinkGenerator("agent");
    			link = deepLinkGenerator.getScreenLink("119ed9cf2f2460102dab2aa62799b6dd"); //SYS_ID OF YOUR APPLET
    		}
    		return link;
    	},
        type: 'LinktoOpen'
    });
  • Create a Widget called “dtype” and add the below code
  • Server Script
    
    (function() {
      /* populate the 'data' object */
      /* e.g., data.table = $sp.getValue('table'); */
    	data.tbl = $sp.getParameter('tbl');
    	data.sysid = $sp.getParameter('sysid');
    	
    })();
    
    Client Controller
    
    function me($scope, $window) {
        var c = this;
    	var isMobile = window.orientation > -1;
    	var ga = new GlideAjax('LinktoOpen');
    	ga.addParam('sysparm_name','getLink');
    	ga.addParam('sysparm_mobile',isMobile);
    	ga.addParam('sysparm_tbl',c.data.tbl);
    	ga.addParam('sysparm_sysid',c.data.sysid);
    	ga.getXML(getlnk);
    	function getlnk(response) {
    		var answer = response.responseXML.documentElement.getAttribute("answer");
    		$window.location.href = answer;
    	}
    }
    
  • Create a Notification Script and name it as “mobile_deeplink”
  • (function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
        /* Optional EmailOutbound */
        email, /* Optional GlideRecord */ email_action,
        /* Optional GlideRecord */
        event) {
    
    	var link = 'https://dev23541.service-now.com/sp?id=devicetype&tbl=sc_req_item&sysid=' + current.getValue('sys_id');
        try {
            var hr = '<a href="' + link + '"' + ' target="_blank">Go to the RITM</a>';
    		template.print(hr);
        } catch (e) {
    		
        }
    
    })(current, template, email, email_action, event);
  • Create a Notification and configure 
    • “When to send” to On insert of RITM
    • Recipients as the Resolver (You may include a default Resolver within the workflow of your catalog item just to see whether he is getting the email)
    • Also, make sure you configure Email properties to send the email & have the email field on sys_user filled with a proper email.

Notification “What it will contain” Tab can be like this

Message HTML

Number: ${number}

Click below to open the Ticket.  It opens with your NOW Mobile App when you are using your Mobile & Opens the SP Portal Status page when you are in Desktop.

${mail_script:mobile_deeplink}



Now, Submit a request and see what happens on both Desktop & Mobile when you receive the email

Mobile View


find_real_file.png


find_real_file.png

Desktop View

find_real_file.png

When you click from desktop, it opens like this

find_real_file.png



When you click from Mobile, it opens like this

find_real_file.png

 

Please bookmark & mark it as helpful.  Let me know in case of any further information required.

Thanks,

Narsing

Comments
apjohn2
Mega Sage

Hello. I have a couple of questions.

  1. Where you say "Create a Portal Page" is that in the "Pages" table (under Service Portal menu), i.e., the sp_page table?
  2. Can you help me with finding the Applet sys_id for the Script Include, or point me in a direction to help me figure this out?
Narsing1
Mega Sage

Hi,

Applets can be found from System Mobile ==> Applets Table

Portal page can be created via Service Portal ==> Pages

Thanks,

Narsing

AdamCelli
Tera Contributor

On my fresh PDI the module is System Mobile > Screens (sys_sg_screen table)

DHarris1
Tera Contributor

Hello Narsing,

The steps you provided to set up dynamic deep links for an email keep directing me to Okta to sign on.

Do you know of any additional steps that can be taken to avoid authenticating in OKTA? 

Thanking you in advance.

 

Narsing1
Mega Sage

Hey,

Can you make the page/Widget as "Public" and see whether it works

Thanks,

Narsing

Nutan
Tera Contributor

Hi,

We have a similar requirement and I performed the steps exactly as you mentioned. It is opening the link in mobile but with all the details are blank. Checked with ServiceNow but as its a customization, didn't get any support. 

Could you please check once and help.

Thanks.

Narsing1
Mega Sage

Hi,

Have you configured your Agent Mobile App with your Instance?

Have you tried with your PDI and is working?

If it is working with the PDI and not with your Company Instance, then it could be some security restriction that needs to be worked with them

 

Can you check the Mobile Deep link with your Applet in the background script and see whether you are really getting the response.

var deepLinkGenerator = new global.MobileDeepLinkGenerator("agent");
link = deepLinkGenerator.getScreenLink("119ed9cf2f2460102dab2aa62799b6dd"); 
gs.print(link);

 

Thanks,

Narsing

Nutan
Tera Contributor

Hello Narsing,

I created another link using "MobileDeepLinkGenerator" and that is working fine. But, this one is bringing blank fields in mobile. I ran this code and this created the link but that seems to be different than that of the one which gets generated with function "getformscreenlink".

Don't think there would be any restriction from instance as other links are working.

Thanks,

Nitu

dp11
Tera Guru

Hello:

Any success with this? I am adding 2 links in the email script, one for browser and one for mobile. 

 

I have modified the below line in mobile_deeplink notification script 

var link = 'https://dev23541.service-now.com/sp?id=devicetype&tbl=sc_req_item&sysid=' + current.getValue('sys_id');

 to

var link = 'https://dev23541.service-now.com/nav_to.do?uri=sc_req_item?sysid=' + current.getValue('sys_id');

 var hr1 = '<a href="' + link1 + '"' + ' target="_blank">Go to the RITM (browser)</a>';
template.print(hr1);
 

And I am not even creating the widget. Just want to see the RITM in browser...instead of portal. But this only displays a new RITM record. 

 

For mobile App, I have added the following

    var deepLinkGenerator = new global.MobileDeepLinkGenerator("request");
    link2 = deepLinkGenerator.getScreenLink("7516f9d787232300e0ef0cf888cb0b4d"); //SYS_ID OF APPLET
    var hr2 = '<a href="' + link2 + '"' + ' target="_blank">Go to the RITM (mobile)</a>';
    template.print(hr2);
 
Does not open the RITM in mobile.. Instead shows "no data available" 
 
Any idea?
Thanks,
Deb
dp11
Tera Guru

I have figured out how to make it work with 2 separate links from the email. Corrections to my above post if anyone is interested:

 

For browser (not portal)

var link = 'https://<instance name>/nav_to.do?uri=sc_req_item%3Fsysid%3D' + current.getValue('sys_id');

var hr1 = '<a href="' + link1 + '"' + ' target="_blank">Go to the RITM (browser)</a>';
template.print(hr1);

 

For mobile App, I have added the following  

var deepLinkGenerator = new global.MobileDeepLinkGenerator("request");
var link2 = deepLinkGenerator.getFormScreenLink("<applet/screen sys_id>", current.getTableName(), current.getValue("sys_id"));
var hr2 = '<a href="' + link2 + '"' + ' target="_blank">Go to the RITM (mobile)</a>';
template.print(hr2);
 
However, would really like a dynamic solution where the device type gets detected automatically and the routing to browser or mobile app takes place seamlessly like it used to be with Classic Mobile. I would like to understand how that happened in Classic Mobile.
 
Thanks
Deb
 
Blair5
Tera Guru

What's the purpose of creating the portal page and widget? Need to understand why.

Version history
Last update:
‎06-20-2021 01:37 AM
Updated by: