Incident: Start Microsoft Teams chat - Hide users "not on Teams"

Jan Strama1
Tera Guru

Hi Community,

 

we are looking into enabling the Teams Chat in for Records in our instance. We currently face the Challange, that not all our users are enabled in Teams but are listed in the Recommended Contacts. But we would like to hide those to avoid mistakes by the users:

JanStrama1_0-1705399325704.png

Is there a way to hide those accounts (which are already flagged as "not on Teams" in the recommendation/User picker? 

Thanks a lot for any hint on how to solve this!

1 ACCEPTED SOLUTION

Thanks again for the Hints.  Unfortunately the ChatRestrictedTaskParticipants Script Include lets you only filter the Users that are selectable over the User picket ad the top:

JanStrama1_0-1707899894261.png

But with the Help of my Colleagues I found now a way to filter the Users shown in the Recommended area by adjusting the "loadRecommended" Function in the "msteams_communication_swarm" UI Page. 

To adchive this I only needed to add the condition "*.isValidTeamsUser == true" in the Sections adding users to the Modal. Here is an extract of the Function with comments where I made the Changes:

// Called when we get a response from the 'addLoadEvent' function
function loadRecommended(response) {
    var userId = gInputData.userId;
    hideLoadingIcon();
    if (typeof response === 'string') {
        if (response.length === 0)
            return;
        var result = JSON.parse(response);
        if (result.error) {
            // Return in case of error when fetch user details
            showAlert(formatMessage(getMessage('{0}'), result.error), 'error', 10000);
            return;
        }
        addToAvailableList(result);
        
        var side = 'left';
        for (var i = 0; i < result.length; i++) {
            var item = result[i];
            if (Array.isArray(item.users)) {
                // add GroupName to Selected or add individual group members
                if (item.showGroupName) {
                    addSwarmOption(result[i], side);
                } else {
                    for (var j = 0; j < item.users.length; j++) {
                        if (userId != '' && item.users[j].id == userId){
                            addSwarmOption(item.users[j], 'right');
			}
                        else if(item.users[j].isValidTeamsUser == true){ //added the isValidTeamsUser condition
							addSwarmOption(item.users[j], side);
			}
                    }
                }
            } else {
                if (userId != '' && item.id == userId && item.isValidTeamsUser == true) { //added the isValidTeamsUser condition
                    addSwarmOption(item, 'right');
                } 
                else if(item.isValidTeamsUser == true){ //added the isValidTeamsUser condition
                    addSwarmOption(item, side);
                }
            }
        }
    }
    // get and save the selected values on the right of the slush bucket
    var array = chatSwarmSlush.getValues(chatSwarmSlush.getRightSelect());
    var r = array.join(",");
    chatSwarmSlush.saveRightValues(r);
    setModalParentHeight();
}

With this I could finally achieve that the Recommended Users in the left of the Modal are only Users with a MS Teams Account

View solution in original post

4 REPLIES 4

Paul Curwen
Giga Sage

The Script Includes involved are: 

 

  • MSTeamsChatRestrictedTaskParticipants
  • ChatRestrictedTaskParticipants

 

MSTeamsChatRestrictedTaskParticipants has Table definitions that define which tables/fields should be used to Recommend participants.  string

 

This post gives an examples of how to add a Table and it's Fields.

 

https://www.servicenow.com/community/developer-forum/start-ms-teams-chat-ui-action-recommended-user-...

 

For your case, to limit Users, if they have a linked Teams account then they would have an entry in Table: provider_user_map

 

In Script Include: ChatRestrictedTaskParticipantsSNC

 

At line 96 (if not customized you will find the following function) . getRelatedUsersCondition

 

 

 //This method is used to get the condition to limit the users you want to add in the chat. Its fine to add sys_user sysId in the condition if there are limited number of users coming from different sources but its better to use a generic condition to limit the condition size. For e.g. 'active=true^internal_integration_user=false
    getRelatedUsersCondition: function(table, sysId) {
        var values = [];
        var sourceGr = new GlideRecord(table);
        if (sourceGr.get(sysId)) {
            values = this.chatUtil.getUsersFromSourceRecord(sourceGr);
        }
        values = values.concat(this.chatUtil.getApprovers(table, sysId));
        values = this.chatUtil.filterDuplicates(values);
        values = this.filterUsersWithValidHRProfiles(table, values);
        //This is the encoded condition to query on sys_user table
        var encodedCond = 'active=true^sys_idIN' + values;
        return encodedCond;
    },

 

You should be able to amend that and filter out the users you do not want to appear by amending the encodedCond string.

 

 

 

***If Correct/Helpful please take time mark as Correct/Helpful. It is much appreciated.***

Regards

Paul

Hi Paul,

 

thabnks a lot for this. I hade a look and from my understanding and the ServiceNow Documentation I should be able to override the  getRelatedUsersCondition frunction from the ChatRestrictedTaskParticipantsSNC script include by adding the adjusted version (with edited encoded Query) in the ChatRestrictedTaskParticipants script include as it only extends ChatRestrictedTaskParticipantsSNC. But onfortunetaly this is not working as expected. Do you have an Idea why this is the Case?
Here is the Code I am using in ChatRestrictedTaskParticipants but the query is not taken from here and the gs.info message on the beginning of the Function is also not fired:

var ChatRestrictedTaskParticipants = Class.create();
ChatRestrictedTaskParticipants.prototype = Object.extendsObject(ChatRestrictedTaskParticipantsSNC, {

	getRelatedUsersCondition: function(table, sysId) {
		gs.info("DEBUG-JS: override Function is uses");
        var values = [];
        var sourceGr = new GlideRecord(table);
        if (sourceGr.get(sysId)) {
            values = this.chatUtil.getUsersFromSourceRecord(sourceGr);
        }
        values = values.concat(this.chatUtil.getApprovers(table, sysId));
        values = this.chatUtil.filterDuplicates(values);
        values = this.filterUsersWithValidHRProfiles(table, values);
        //This is the encoded condition to query on sys_user table
        var encodedCond = 'active=true^u_samaccountnameISNOTEMPTY^sys_idIN' + values;
        return encodedCond;
    },

    type: 'ChatRestrictedTaskParticipants'
});

 

Thanks again for the Hints.  Unfortunately the ChatRestrictedTaskParticipants Script Include lets you only filter the Users that are selectable over the User picket ad the top:

JanStrama1_0-1707899894261.png

But with the Help of my Colleagues I found now a way to filter the Users shown in the Recommended area by adjusting the "loadRecommended" Function in the "msteams_communication_swarm" UI Page. 

To adchive this I only needed to add the condition "*.isValidTeamsUser == true" in the Sections adding users to the Modal. Here is an extract of the Function with comments where I made the Changes:

// Called when we get a response from the 'addLoadEvent' function
function loadRecommended(response) {
    var userId = gInputData.userId;
    hideLoadingIcon();
    if (typeof response === 'string') {
        if (response.length === 0)
            return;
        var result = JSON.parse(response);
        if (result.error) {
            // Return in case of error when fetch user details
            showAlert(formatMessage(getMessage('{0}'), result.error), 'error', 10000);
            return;
        }
        addToAvailableList(result);
        
        var side = 'left';
        for (var i = 0; i < result.length; i++) {
            var item = result[i];
            if (Array.isArray(item.users)) {
                // add GroupName to Selected or add individual group members
                if (item.showGroupName) {
                    addSwarmOption(result[i], side);
                } else {
                    for (var j = 0; j < item.users.length; j++) {
                        if (userId != '' && item.users[j].id == userId){
                            addSwarmOption(item.users[j], 'right');
			}
                        else if(item.users[j].isValidTeamsUser == true){ //added the isValidTeamsUser condition
							addSwarmOption(item.users[j], side);
			}
                    }
                }
            } else {
                if (userId != '' && item.id == userId && item.isValidTeamsUser == true) { //added the isValidTeamsUser condition
                    addSwarmOption(item, 'right');
                } 
                else if(item.isValidTeamsUser == true){ //added the isValidTeamsUser condition
                    addSwarmOption(item, side);
                }
            }
        }
    }
    // get and save the selected values on the right of the slush bucket
    var array = chatSwarmSlush.getValues(chatSwarmSlush.getRightSelect());
    var r = array.join(",");
    chatSwarmSlush.saveRightValues(r);
    setModalParentHeight();
}

With this I could finally achieve that the Recommended Users in the left of the Modal are only Users with a MS Teams Account

Hi,

Please check the screenshot and i am also facing the same. But users have both valid emails in ServiceNow and MS Teams.