How to use for loop in Script Include

Rekha Tiwari
Kilo Guru

Hi All,

 

I need to populate assignment group with the group to which Subject person belongs. I need to validate for below groups if Subject person is member or not-

1) Supplier Onboarding - CCS
2) Supplier Onboarding - Client Services
3) Supplier Onboarding - Implementations

 

I have written below SI-

 

find_real_file.png

var SupplierOnboardingCustomUtils1 = Class.create();
SupplierOnboardingCustomUtils1.prototype = {
initialize: function() {},
populateGroups: function(subjectPerson) {

 

var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', subjectPerson);
gr.addQuery('group', '515e3a461b644110050ddc68b04bcbea').addOrCondition('group', 'cd1d36061b644110050ddc68b04bcbf4').addOrCondition('group', '77fd76061b644110050ddc68b04bcb00'); //Supplier Onboarding - CCS OR Supplier Onboarding - Client Services OR Supplier Onboarding - Implementations
gr.query();
if (gr.next()) {
return gr.group.toString();
//return "sys_id="+gr.group.toString();
}
},
type: 'SupplierOnboardingCustomUtils1'
};

 

it is giving correct result but as per feedback I need to use system property to store group sys id. and need run a for loop to add orCondition. So I have created system properties named- sn_hr_le.supplier.onboarding.groups

 

find_real_file.png

This is the BR which is calling SI-

 

current.assignment_group = new SupplierOnboardingCustomUtils1().populateGroups(current.subject_person);
current.assigned_to = current.opened_by;
current.update();

 

Any one can please help me to modify script to call system property and running for loop?

 

Thanks,

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

update script as this

var SupplierOnboardingCustomUtils1 = Class.create();
SupplierOnboardingCustomUtils1.prototype = {

	initialize: function() {},

	populateGroups: function(subjectPerson) {

		var groups = gs.getProperty('sn_hr_le.supplier.onboarding.groups'); // get comma separated list of group sys_id's
		var queryString = 'group.sys_idIN' + groups;
		var gr = new GlideRecord('sys_user_grmember');
		gr.addQuery('user', subjectPerson);
		gr.addEncodedQuery(queryString);
		gr.setLimit(1);
		gr.query();
		if (gr.next()) {
			gs.info("User is Member of Group!");
			gs.info('returned group is:' + gr.group.toString());
			return gr.group.toString();
		}
		else {
			gs.info("User is Not a Member of Group!");
			return '';
		}

	},
	type: 'SupplierOnboardingCustomUtils1'
};

Script

var group = new SupplierOnboardingCustomUtils1().populateGroups(current.subject_person);
if(group){
	current.assignment_group = group;
}
current.assigned_to = current.opened_by;
current.update();

Regards
Ankur

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

View solution in original post

7 REPLIES 7

You have used both approaches.

Please update your script like below:

var SupplierOnboardingCustomUtils1 = Class.create();
SupplierOnboardingCustomUtils1.prototype = {
initialize: function() {},
populateGroups: function(subjectPerson) {

// var gr = new GlideRecord('sys_user_grmember');
// gr.addQuery('user', subjectPerson);
// gr.addQuery('group', '515e3a461b644110050ddc68b04bcbea').addOrCondition('group', 'cd1d36061b644110050ddc68b04bcbf4').addOrCondition('group', '77fd76061b644110050ddc68b04bcb00'); //Supplier Onboarding - CCS OR Supplier Onboarding - Client Services OR Supplier Onboarding - Implementations
// gr.query();
// if (gr.next()) {
var groups = gs.getProperty('sn_hr_le.supplier.onboarding.groups'); // get comma separated list of group sys_id's
//var queryString = 'group.sys_idIN' + groupString;
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', subjectPerson);
gr.addEncodedQuery('groupIN' + groups.toString());
//gr.addEncodedQuery('group', queryString);
if (gr.next()) {
gs.info("User is Member of Group!");
gs.info('returned group is:' + gr.group.toString());
return gr.group.toString();
}
//return "sys_id="+gr.group.toString();
else {
gs.info("User is Not a Member of Group!");
}


},
type: 'SupplierOnboardingCustomUtils1'
};

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Hi @Mike Reading 

 

This is not working-

 

find_real_file.png

 

 

var SupplierOnboardingCustomUtils1 = Class.create();
SupplierOnboardingCustomUtils1.prototype = {
initialize: function() {},
populateGroups: function(subjectPerson) {

// var gr = new GlideRecord('sys_user_grmember');
// gr.addQuery('user', subjectPerson);
// gr.addQuery('group', '515e3a461b644110050ddc68b04bcbea').addOrCondition('group', 'cd1d36061b644110050ddc68b04bcbf4').addOrCondition('group', '77fd76061b644110050ddc68b04bcb00'); //Supplier Onboarding - CCS OR Supplier Onboarding - Client Services OR Supplier Onboarding - Implementations
// gr.query();
// if (gr.next()) {
var groups = gs.getProperty('sn_hr_le.supplier.onboarding.groups'); // get comma separated list of group sys_id's
var queryString = 'group.sys_idIN' + groupString;
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', subjectPerson);
//gr.addEncodedQuery('groupIN' + groups.toString());
gr.addEncodedQuery('group', queryString);
if (gr.next()) {
gs.info("User is Member of Group!");
gs.info('returned group is:' + gr.group.toString());
return gr.group.toString();
}
//return "sys_id="+gr.group.toString();
else {
gs.info("User is Not a Member of Group!");
}


},
type: 'SupplierOnboardingCustomUtils1'
};

 

  FYI-  Subject person can be either one of the mentioned group so we are considering OR. will this encoded query will work?

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

update script as this

var SupplierOnboardingCustomUtils1 = Class.create();
SupplierOnboardingCustomUtils1.prototype = {

	initialize: function() {},

	populateGroups: function(subjectPerson) {

		var groups = gs.getProperty('sn_hr_le.supplier.onboarding.groups'); // get comma separated list of group sys_id's
		var queryString = 'group.sys_idIN' + groups;
		var gr = new GlideRecord('sys_user_grmember');
		gr.addQuery('user', subjectPerson);
		gr.addEncodedQuery(queryString);
		gr.setLimit(1);
		gr.query();
		if (gr.next()) {
			gs.info("User is Member of Group!");
			gs.info('returned group is:' + gr.group.toString());
			return gr.group.toString();
		}
		else {
			gs.info("User is Not a Member of Group!");
			return '';
		}

	},
	type: 'SupplierOnboardingCustomUtils1'
};

Script

var group = new SupplierOnboardingCustomUtils1().populateGroups(current.subject_person);
if(group){
	current.assignment_group = group;
}
current.assigned_to = current.opened_by;
current.update();

Regards
Ankur

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