glide record for loop help

Gary22
Tera Contributor

hi All , 

i have a list of incidents stored in a variable ( separator is comma )

var1 = 'inc1 , inc2 , inc3'

 

In my server side script i need to do a glide on the incident table and query for each incident number , one by one ( first query for inc1 , then query for inc 2 in a loop ).This is a basic scripting question i know but need some hint on how to fetch the comma separated values into the for loop one by one , , so that i can then do a glide query . 

 

thanks a lot in advance

6 REPLIES 6

BalaG
Kilo Sage

Hi @Gary22   here is the javascript  code to iterate over the incidents, I did not include call to glide.

 

var1 = 'inc1 , inc2 , inc3';   // input 
var var2 = var1.split(',');

var2.forEach ( processInc );

function processInc(inc) {
// process individual incident
 gs.info( inc.trim() ); // trim to remove spaces
}

 

hope this helps

--

Bala Guthy

 

Please mark this as a solution or helpful as you see fit

 

Sainath N
Mega Sage
Mega Sage

@Gary22 : Please use the below script. 

 

var incidentList = ['INCC0011392', 'INCC0011391', 'INCC0011390'];

incidentList.forEach(callbackFn);

function callbackFn(number) {
    gs.log('Incident Number ==' + number);
    // I am printing the number one-by-one.
    // you can add here your logic
}

 

Sample Output:

sainathnekkanti_0-1704774593219.png

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

 

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Gary22 ,

 

Try below code

 

// Your comma-separated incidents

var var1 = 'inc1,inc2,inc3';

 

// Split the values into an array

var incidentsArray = var1.split(',');

 

// Iterate through the array

for (var i = 0; i < incidentsArray.length; i++) {

    // Trim to remove any extra spaces

    var incidentNumber = incidentsArray[i].trim();

 

    // Perform Glide query for each incident number

    var incidentGR = new GlideRecord('incident');

    incidentGR.addQuery('incident_number', incidentNumber);

    incidentGR.query();

 

    // Process the result

    if (incidentGR.next()) {

        // Do something with the incident record

        gs.info('Incident ' + incidentNumber + ' found: ' + incidentGR.getValue('short_description'));

    } else {

        gs.info('Incident ' + incidentNumber + '

not found');

    }

}

 

Thanks,

Danish

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Gary22 

It's an easy one.

Rather than asking the question in community and using the exact answer you should understand how to debug it, this will help you develop your scripting skills as well.

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