How can I Populate a custom field (Created by Team) with the Logged In User's Group Name

WazzaJC
Tera Expert

Hi Team,

I would really appreciate your help/guidance, I cannot work this out.

How can I configure a Business Rule or Client Script so that it populates a custom field I have created, with the Group/Group Names, that the currently logged in/opened by User belongs to, in the Incident form.

Custom Field is called: Created by Team (u_created_by_team) - this is where I want the User's Group Name or Names to populate to.

Source: Incident Form

Opened By field = Logged in User

I would like the Groups that the logged in user belongs to, to populate (onLoad) to the custom field (u_created_by_team), which I have set up as a sting field.

I assume this can be done by Business Rule or onLoad Client script but not sure how to do it.

Thanks ever so much - I have attached a screenshot showing my Incident Form with the fields on.

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@WazzaJC 

you can use display business rule on incident table and set the value

(function executeRule(current, previous /*null when async*/) {

	if(current.isNewRecord()){
		var groups = new global.ArrayUtil().convertArray(gs.getUser().getMyGroups());
		var arr = [];
		var gr = new GlideRecord("sys_user_group");
		gr.addQuery("sys_id", "IN", groups);
		gr.query();
		while (gr.next()) {
			arr.push(gr.getDisplayValue());
		}
		current.u_created_by_team = arr.toString();
	}

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

7 REPLIES 7

Prince Arora
Tera Sage
Tera Sage

@WazzaJC 

 

What would be the case if user is the part of multiple groups?

 

If my answer solved your issue, please mark my answer as  Correct & 👍Helpful based on the Impact.

Ankur Bawiskar
Tera Patron
Tera Patron

@WazzaJC 

you can use display business rule on incident table and set the value

(function executeRule(current, previous /*null when async*/) {

	if(current.isNewRecord()){
		var groups = new global.ArrayUtil().convertArray(gs.getUser().getMyGroups());
		var arr = [];
		var gr = new GlideRecord("sys_user_group");
		gr.addQuery("sys_id", "IN", groups);
		gr.query();
		while (gr.next()) {
			arr.push(gr.getDisplayValue());
		}
		current.u_created_by_team = arr.toString();
	}

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hello Ankur,

This is brilliant ! Works perfectly, I have set it up now and all working great.

Thank you ever so much, I really appreciate your help and such a quick response.

I am very grateful for this community - thank you for your guidance.

Kind Regards, Warwick

@WazzaJC 

Glad to help.

Happy learning.

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