
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2021 03:08 AM
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.
Solved! Go to Solution.
- Labels:
-
Delegated Development
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2021 03:17 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2021 04:29 AM
Hi
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2021 04:54 AM
Hi,
Did you try to add some debug statements?
Are you using the correct field names?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2021 05:00 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2021 05:28 AM
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();
Mohit Kaushik
ServiceNow MVP (2023-2025)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2021 05:41 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader