Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

UI macro to redirect to teams chat window

Tapish Sharma1
Kilo Sage

Hi Folks,

I have a requirement to add a teams link besides a User reference field ('Responsible Leader'). I have written a UI macro but it is not working - Below is the script  (Also open to go with any other approach if possible)

**My script and field both are in Customer service Scope

UI Macro 

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<a><img src ="1batch.png" width = "25%" height = "25%" onClick = "userWindow('$(ref)')" tabindex="0" ></img></a>

<script>
function userWindow(reference){
var refVal = reference.split('.');
var fieldName = refVal[1];
var emailId;

var sysId= g_form.getValue(fieldName);
var user = new GlideRecord('sys_user);
if(user.get(sys_id)){

emailId = user.email.toString();
}

if(emailId){
open('sip:'+emailId, '_top');

}
else{
g_form.addErrorMessage('User Does not have email in servicenow');

}
}
</script>
</j:jelly>

1 ACCEPTED SOLUTION

Hi,

try this and share the alerts

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
	<g:evaluate var="jvar_guid" expression="gs.generateGUID(this);" />
	<j:set var="jvar_n" value="show_incidents_${jvar_guid}:${ref}"/>

	<a id="${jvar_n}"
	   onclick="userWindow('${ref}');"
	   name="${jvar_n}"
	   onmouseout="lockPopup(event)"
	   tabindex="0"
	   >
		<img border="0" src="1batch.png" width="28" height="28" title="${gs.getMessage('Open Teams chat')}" />
	</a>

	<script>
		function userWindow(reference){
		var refVal = reference.split('.');
		var fieldName = refVal[1];
		var emailId;
		alert('fieldName->' + fieldName);
		var sysId= g_form.getValue(fieldName);
		alert('sysId->' + sysId);
		var user = new GlideRecord('sys_user'); // quote was missing
		if(user.get(sysId)){ // variable name corrected
		emailId = user.email.toString();
		}

		if(emailId){
		open('sip:'+emailId, '_top');
		}
		else{
		g_form.addErrorMessage('User Does not have email in servicenow');
		}
		}
	</script>
</j:jelly>

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

14 REPLIES 14

Hi Ankur,Thanks I corrected those typos, I am not getting any values in fieldname and sysId variables. 

Hi,

try this and share the alerts

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
	<g:evaluate var="jvar_guid" expression="gs.generateGUID(this);" />
	<j:set var="jvar_n" value="show_incidents_${jvar_guid}:${ref}"/>

	<a id="${jvar_n}"
	   onclick="userWindow('${ref}');"
	   name="${jvar_n}"
	   onmouseout="lockPopup(event)"
	   tabindex="0"
	   >
		<img border="0" src="1batch.png" width="28" height="28" title="${gs.getMessage('Open Teams chat')}" />
	</a>

	<script>
		function userWindow(reference){
		var refVal = reference.split('.');
		var fieldName = refVal[1];
		var emailId;
		alert('fieldName->' + fieldName);
		var sysId= g_form.getValue(fieldName);
		alert('sysId->' + sysId);
		var user = new GlideRecord('sys_user'); // quote was missing
		if(user.get(sysId)){ // variable name corrected
		emailId = user.email.toString();
		}

		if(emailId){
		open('sip:'+emailId, '_top');
		}
		else{
		g_form.addErrorMessage('User Does not have email in servicenow');
		}
		}
	</script>
</j:jelly>

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I have this working script, I have another direct link with teams as well (so the two url options both work). 

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<g:evaluate var="jvar_guid" expression="gs.generateGUID(this);"/>
<j:set var="jvar_n" value="open_lync_${jvar_guid}:${ref}"/>

<a href="#" onclick="invokeChat('${ref}');"><img src="teams.jpg" title="Invoke Teams Chat" /></a>

<script>
function invokeChat(reference) {
var s = reference.split('.');
var tableName = s[0];
var referenceField = s[1];
var v = g_form.getValue(referenceField);
var email;
var gr = new GlideRecord('sys_user');
if (gr.get(v)) { email = gr.email;
}
var url = 'sip:'+email; //one examlpe
var url = 'https://teams.microsoft.com/l/chat/0/0?users='+email; //another example

var w = getTopWindow();
w.open(url,"_self");
}
</script>
</j:jelly>

Best regards,
Sebastian Laursen

@Sebastian Laursen  

I have added the UI macro in field attributes , it is showing in the form, any idea how can we make it visible also on portal . Same field is visible on portal as well but the attribute is not showing.

Hi Ankur , this worked for me .... thankyou so much !!