scripted rest api to get all incidents assigned to user groups?

Kiddy
Tera Guru

Hi ,

I have created a scripted rest api to get all the incidents assigned to user groups which he is member of below is the code which i was trying i am stuck how to get the assignment groups of user and put it in the query 

i get the user id in params need to check all the groups the user is part of and return the incidents assigned to those groups in last 30 days

var userId=request.pathParams.user_id;

var grProj = new GlideRecord('incident');
grProj.addEncodedQuery('sys_class_name=incident^sys_created_onONLast 30 days@javascript:gs.beginningOfLast30Days()@javascript:gs.endOfLast30Days()^active=true');
//grProj.addQuery('assigned_to.user_name',userId);
grProj.addEncodedQuery('assignment_groupDYNAMICd6435e965f510100a9ad2572f2b47744');
grProj.query();
while(grProj.next())
{
var body={};
body.number=grProj.number;
body.short_description =grProj.short_description;
body.state=grProj.getDisplayValue('incident_state');
body.assignment_group=grProj.getDisplayValue('assignment_group');
body.assigned_to=grProj.getDisplayValue('assigned_to');
body.due_date=grProj.getDisplayValue('due_date');
//body.assigned_to=getInc.getDisplayValue('assigned_to');
//body.assigned_time=grProj.sys_created_on;
//body.short_description=getInc.short_description;
bodyList.push(body);

}
response.setBody(bodyList);

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

like this

1) query group member table for that user and get all the groups to which this user belongs

2) then use that group array in query

var userId = request.pathParams.user_id;

// get the user group then use in query

var groupArr = [];
var rec = new GlideRecord('sys_user_grmember');
rec.addQuery('user.user_name', userId);
rec.query();
while(rec.next()){
	groupArr.push(rec.getValue('group'));
}

var grProj = new GlideRecord('incident');
grProj.addEncodedQuery('sys_class_name=incident^sys_created_onONLast 30 days@javascript:gs.beginningOfLast30Days()@javascript:gs.endOfLast30Days()^active=true');

grProj.addEncodedQuery('assignment_group', 'IN', groupArr);

grProj.query();
while(grProj.next())
{
	var body={};
	body.number=grProj.number;
	body.short_description =grProj.short_description;
	body.state=grProj.getDisplayValue('incident_state');
	body.assignment_group=grProj.getDisplayValue('assignment_group');
	body.assigned_to=grProj.getDisplayValue('assigned_to');
	body.due_date=grProj.getDisplayValue('due_date');
	//body.assigned_to=getInc.getDisplayValue('assigned_to');
	//body.assigned_time=grProj.sys_created_on;
	//body.short_description=getInc.short_description;
	bodyList.push(body);

}
response.setBody(bodyList);

Regards
Ankur

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

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

like this

1) query group member table for that user and get all the groups to which this user belongs

2) then use that group array in query

var userId = request.pathParams.user_id;

// get the user group then use in query

var groupArr = [];
var rec = new GlideRecord('sys_user_grmember');
rec.addQuery('user.user_name', userId);
rec.query();
while(rec.next()){
	groupArr.push(rec.getValue('group'));
}

var grProj = new GlideRecord('incident');
grProj.addEncodedQuery('sys_class_name=incident^sys_created_onONLast 30 days@javascript:gs.beginningOfLast30Days()@javascript:gs.endOfLast30Days()^active=true');

grProj.addEncodedQuery('assignment_group', 'IN', groupArr);

grProj.query();
while(grProj.next())
{
	var body={};
	body.number=grProj.number;
	body.short_description =grProj.short_description;
	body.state=grProj.getDisplayValue('incident_state');
	body.assignment_group=grProj.getDisplayValue('assignment_group');
	body.assigned_to=grProj.getDisplayValue('assigned_to');
	body.due_date=grProj.getDisplayValue('due_date');
	//body.assigned_to=getInc.getDisplayValue('assigned_to');
	//body.assigned_time=grProj.sys_created_on;
	//body.short_description=getInc.short_description;
	bodyList.push(body);

}
response.setBody(bodyList);

Regards
Ankur

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

Hi Ankur,

 

I tried this i get incidents from groups which he is not part of as well in result

Hi,

Please check with some other user

Script will get only the groups to which user belongs to

Did you check for active groups only?

Regards
Ankur

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