How can I get current logged in user in servicenow ?

RadhikaG
Tera Contributor

A manager wants a field to be mandatory only when the logged-in user belongs to a specific group. How would you implement this?

Can we achieve this via UI policy or client script ?

2 REPLIES 2

GlideFather
Tera Patron

Hi @RadhikaG,

 

you can use User Presence there you can track what people do :))

GlideFather_0-1768925528651.png

 

GlideFather_1-1768925823640.png

 

 

And/or on the sys_user table there are fields Last login (just date) and Last login time (date and time):

GlideFather_2-1768925891229.png

 

_____
No AI was used in the writing of this post. Pure #GlideFather only

yashkamde
Tera Guru

Hello @RadhikaG ,

You can achieve this using onLoad Client script, Below I had given an implementation

client script ->

function onLoad() {
    var fieldName = 'field_name';  //Replace the field name you want to make mandatory
    var targetGroupSysId = '3d70baa9c3e2f610bbcf1275e4013114';  //Replace your group sys_id
    
    var ga = new GlideAjax('UserGroupCheck');
    ga.addParam('sysparm_name', 'isUserInGroup');
    ga.addParam('sysparm_group', targetGroupSysId);
    ga.getXMLAnswer(function(answer) {
        if (answer == 'true') {
            g_form.setMandatory(fieldName, true);
        } else {
            g_form.setMandatory(fieldName, false);
        }
    });
}

 

script include ->

var UserGroupCheck = Class.create();
UserGroupCheck.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    isUserInGroup: function() {
        var groupSysId = this.getParameter('sysparm_group');
        var userSysId = gs.getUserID();
        
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('group', groupSysId);
        gr.addQuery('user', userSysId);
        gr.query();
        
        if(gr.next()){
           return true;
        }else{
           return false;
        }
    }
});

 

If my response was helped mark as helpful and accept the solution..