The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Get email of Requested for user

Kasia5
Tera Contributor

Hi All,

 

I have to variables in catalog item:

One is 'Requested for' which is a List collector to choose more than one user

Then I see also user id, email and so on

But there are some security restrictions for some users and they don't see user id, email

So I've created the second variable "users info" which is a Multi Line Text and also I've created Client Script to push user id, email of 'Requested for" to this second variable.

But currently I see only user ID, what should I change/add to see also email?

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var user_id = g_form.getValue("requested_for_info");
    var userIDArray = user_id.split(',');
    var userinfo = '';

    function processUserInfo(i) {
        var ga = new GlideAjax('getCatalogItemsInfo');
        ga.addParam('sysparm_name', 'getUserInfo');
        ga.addParam('sysparm_userID', userIDArray[i]);
        ga.getXMLAnswer(function(response) {
            userinfo += response;
            g_form.setValue("users_info", userinfo);
        });
    }

    for (var i = 0; i < userIDArray.length; i++) {
        processUserInfo(i);
    }
}

 

Kasia5_0-1709201687050.png

 

Thanks in advance for help!

 

4 REPLIES 4

Malte_K
Tera Expert

Hello @Kasia5 ,

 

this depends on the Function "getUnserInfo" in your Script-Include "getCatalogsItemsInfo".
If the script include is using GlideRecord to fetch user data, then you could return the additional fields.

However if it is using GlideRecordSecure, which is recommended for client callable script includes, the access controls will not allow to return this sensitive information.

I recommend that you don't try to get sensitive information. There must be some reason why the access controls are in place.

Malte 

Malte_K
Tera Expert

Another suggestion to get the information via ServiceNow api instead of the custom script include could be the following code. However the security constraints are still in place.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var userGr = new GlideRecord('sys_user');
    userGr.addQuery('sys_id', 'IN', g_form.getValue("requested_for_info"));
    userGr.query(function(userGr) {
        var userInfos = [];

        while (userGr.next()) {
            var userName = userGr.name;
            var userId = userGr.user_name;
            var email = userGr.email;

            userInfos.push(userName + ', ' + userId + ', ' + email);
        }

        g_form.setValue("users_info", userInfos.join('\n'));
    });

}

piyushsain
Tera Guru
Tera Guru

Please share getCatalogItemsInfo script

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,
Piyush Sain

swathisarang98
Giga Sage
Giga Sage

Hi @Kasia5 ,

 

You can try the below SI and Client script,

 

 

var getUserEmail = Class.create();
getUserEmail.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getEmail: function() {
		var table = this.getParameter('sysparm_query_table');
        var sysid = this.getParameter('sysparm_id');
        gs.info('line number 8 ' + sysid);

        var grEmails = new GlideRecord(table);
        grEmails.addEncodedQuery(sysid);
        grEmails.query();
        var arr = [];
        while(grEmails.next()) {
			gs.info('line number 14 inside while');
            arr.push(grEmails.name.toString() + ':  ' + grEmails.email);
            
        }
		gs.info('line number 15', arr.toString());
        return arr.toString();


    },

    type: 'getUserEmail'
});

 

 

swathisarang98_1-1709210953960.png

 

client script:

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var liststr = g_form.getValue('requested_for').toString();
    var ga = new GlideAjax('getUserEmail');
    ga.addParam('sysparm_name', 'getEmail');
    ga.addParam('sysparm_query_table', 'sys_user');
    ga.addParam('sysparm_id', "sys_idIN" + liststr + ",");
    ga.getXML(getResponse);

}

function getResponse(response) {
    var ans = response.responseXML.documentElement.getAttribute('answer');
    alert('answer ' + ans);
    g_form.setValue('users_info', ans.replaceAll(",","\n"));
	g_form.setReadOnly('users_info', true);





}

 

 

swathisarang98_0-1709210923382.png

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang