How to loop through a table?

zaryabro1
Giga Contributor

How do I loop through a table, I want to get specific data from a table based on a condition and put it into an array.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Something like this

var arr = [];

var gr = new GlideRecord('incident');

gr.addQuery('active', true);

gr.query();

while(gr.next()){

arr.push(gr.getValue('number'));

}

gs.info(arr);

Regards
Ankur

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

View solution in original post

9 REPLIES 9

Hi @Ankur Bawiskar,

Can you tell me the problem with this code? No record is being created in the 'Application Permission' table

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

	// Add your code here
	gs.info("RUN: Get and Populate Permissions")
	var appCategories = [];
	appCategories = getCategories(current.application_name);
	
	for(var i = 0; i < appCategories.length; i++){
		addPermissions(appCategories[i], current.employee_id);
	}
	
	
	
	
	function getCategories(app_name) {
		gs.info("RUN: getCategories()");
		var arr = [];
		var gr = new GlideRecord('x_rege_onboarding_app_category');
		
		gr.addQuery('active', true);
		gr.query();

		while(gr.next()){
		if(gr.getValue('application') == app_name){
		arr.push(gr.getValue('number'));
		}
		}
		gs.info(arr);
		return arr;
	}
	
	function addPermissions(app_cat, emp_id) {
		gs.info("RUN: addPermission()");
		var pr = new GlideRecord('x_rege_onboarding_permission');
		var apr = new GlideRecord('x_rege_onboarding_application_permission');
		var per_arr = [];
		
		pr.addQuery('active', true);
		pr.query();
		
		while (pr.next()){
			if (pr.getValue('application_category') == app_cat) {
				per_arr.push(pr.getValue('number'));
			}
		}
		
		for(var i = 0; i < per_arr.length; i++){
			apr.applicationcategoryid = app_cat;
			apr.newemployeeid = emp_id;
			apr.permission = per_arr[i];
			apr.insert();
		}
		
		
	}
})(current, previous);

Hi,

Did you try to add some debug statements?

Are you using the correct field names?

Regards
Ankur

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

Yes, I'm using the correct field names, as I wrote a dummy hello() function, which is creating records: Please Review

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

	// Add your code here
	hello();
	
	gs.info("RUN: Get and Populate Permissions")
	var appCategories = [];
	appCategories = getCategories(current.application_name);
	
	for(var i = 0; i < appCategories.length; i++){
		addPermissions(appCategories[i], current.employee_id);
	}
	
	
	function hello() {
		var gr = new GlideRecord('x_rege_onboarding_application_permission');
			gr.applicationcategoryid = "app_cat";
			gr.newemployeeid = "emp_id";
			gr.permission = "accept";
			gr.insert();
	}
	
	function getCategories(app_name) {
		gs.info("RUN: getCategories()");
		var arr = [];
		var gr = new GlideRecord('x_rege_onboarding_app_category');
		
		gr.addQuery('active', true);
		gr.query();

		while(gr.next()){
		if(gr.getValue('application') == app_name){
		arr.push(gr.getValue('number'));
		}
		}
		gs.info(arr);
		return arr;
	}
	
	function addPermissions(app_cat, emp_id) {
		gs.info("RUN: addPermission()");
		var pr = new GlideRecord('x_rege_onboarding_permission');
		var apr = new GlideRecord('x_rege_onboarding_application_permission');
		var per_arr = [];
		
		pr.addQuery('active', true);
		pr.query();
		
		while (pr.next()){
			if (pr.getValue('application_category') == app_cat) {
				per_arr.push(pr.getValue('number'));
			}
		}
		
		for(var i = 0; i < per_arr.length; i++){
			apr.applicationcategoryid = app_cat;
			apr.newemployeeid = emp_id;
			apr.permission = per_arr[i];
			apr.insert();
		}
		
		
	}
})(current, previous);(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	hello();
	
	gs.info("RUN: Get and Populate Permissions")
	var appCategories = [];
	appCategories = getCategories(current.application_name);
	
	for(var i = 0; i < appCategories.length; i++){
		addPermissions(appCategories[i], current.employee_id);
	}
	
	
	function hello() {
		var gr = new GlideRecord('x_rege_onboarding_application_permission');
			gr.applicationcategoryid = "app_cat";
			gr.newemployeeid = "emp_id";
			gr.permission = "accept";
			gr.insert();
	}
	
	function getCategories(app_name) {
		gs.info("RUN: getCategories()");
		var arr = [];
		var gr = new GlideRecord('x_rege_onboarding_app_category');
		
		gr.addQuery('active', true);
		gr.query();

		while(gr.next()){
		if(gr.getValue('application') == app_name){
		arr.push(gr.getValue('number'));
		}
		}
		gs.info(arr);
		return arr;
	}
	
	function addPermissions(app_cat, emp_id) {
		gs.info("RUN: addPermission()");
		var pr = new GlideRecord('x_rege_onboarding_permission');
		var apr = new GlideRecord('x_rege_onboarding_application_permission');
		var per_arr = [];
		
		pr.addQuery('active', true);
		pr.query();
		
		while (pr.next()){
			if (pr.getValue('application_category') == app_cat) {
				per_arr.push(pr.getValue('number'));
			}
		}
		
		for(var i = 0; i < per_arr.length; i++){
			apr.applicationcategoryid = app_cat;
			apr.newemployeeid = emp_id;
			apr.permission = per_arr[i];
			apr.insert();
		}
		
		
	}
})(current, previous);

you need to initailize() your object for the table in which you want to create records.

so for below code

var gr = new GlideRecord('x_rege_onboarding_application_permission');
gr.initialize();
Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

Hi,

you forgot to initiazlie

for(var i = 0; i < per_arr.length; i++){
apr.initialize();
			apr.applicationcategoryid = app_cat;
			apr.newemployeeid = emp_id;
			apr.permission = per_arr[i];
			apr.insert();
		}

Regards
Ankur

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