Populate the logged in user's group

Samiksha2
Mega Sage

Hi All,

 

I have created a Reference field in the Requested Item.(Requester Group- Reference to sys_group table). 

The requirement is to show the logged in user's group in that field without adding any reference qualifier in the dictionary because this field is been used in other scripts.

Samiksha2_0-1694760919710.png

Please help in this. 

 

Thanks,

Samiksha

1 ACCEPTED SOLUTION

@Samiksha2 

Please use the following BR with the script to populate the first group upon RITM creation.

 

(function executeRule(current, previous /*null when async*/ ) {
    var gr = new GlideRecord("sys_user_grmember");
    gr.addQuery("user", current.requested_for);
    gr.query();
    if (gr.next()) {
        current.u_requester_group = gr.getValue('group'); //Change field name as per your configuration
    }
})(current, previous);

 

AnveshKumarM_0-1694763427193.png

 

Thanks,
Anvesh

View solution in original post

7 REPLIES 7

Thanks @AnveshKumar M ,

 

It worked.

Ankur Bawiskar
Tera Patron
Tera Patron

@Samiksha2 

Is this a custom field? what's the use of this?

Which logged in user are you referring?

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

RAMANA MURTHY G
Mega Sage
Mega Sage

Hello @Samiksha2 

You can write onLoad client script, and call the Client callable script include using GlideAjax to query the current logged-in User's group.

 

onLoad Client Script on Requested Item table

RAMANAMURTHYG_0-1694765815167.png

 

function onLoad() {
   //Type appropriate comment here, and begin script below
   var loggedinUser = g_user.userID;    // It gives current logged in user sys_id
   var gAjax = new GlideAjax('PopulateLoggedinUserGroup');
   gAjax.addParam('sysparm_name','populateGroup');
   gAjax.addParam('sysparm_user',loggedinUser);

   gAjax.getXML(populateGroup);

   function populateGroup(response){
	var returnGroup = response.responseXML.documentElement.getAttribute('answer');
	g_form.setValue('u_requester_group',returnGroup);
   }
}

 

Client Callable Script include

RAMANAMURTHYG_1-1694765945372.png

 

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

	populateGroup: function(){
		var userId = this.getParameter('sysparm_user');
		var userGroup = new GlideRecord('sys_user_grmember');
		userGroup.addQuery('user',userId);
		userGroup.query();
		if(userGroup.next()){
			return userGroup.group;
		}
	},
    type: 'PopulateLoggedinUserGroup'
});

 

Please mark my answer as Helpful, if it helps you

Thank you

Please mark my answer helpful  & correct if it helps you
Thank you

G Ramana Murthy
ServiceNow Developer