Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to search records with script

Shiva41
Tera Contributor

i want to create multiple records, but we need to look either record already exist or not.

2 REPLIES 2

Community Alums
Not applicable

Not sure what exactly you want to achieve & where you want to implement the logic. However the following might give you some starting point.

 

var array = ; // define your array with the values you want to pass
for (var i = 0; i < array.length; i++)
{

var gr = new GlideRecord("your table name");
gr.addQuery("name", "value");//Your condtions based on table and how you fetch record. You can passs the paramter from array.
gr.query();
if (gr.next()) {
//logic to execute the record is found
}
else
{
//insert logic if the record is not found
}
}

Sai Kumar B
Mega Sage

Hi Shiva,

You can query the existing records in the table and create records if there are not available

var array=6; //You can change the number accordingly as per your requirement
var Inc = new GlideRecord('table_name');
Inc.addQuery('field_name', 'value');
Inc.addQuery('field_name', 'value');
Inc.query();
if(!Inc.hasNext()) {
//It comes inside if there are no records with same data as per the above query you can insert new record here
for(var i=0; i<array; i++){ //Use this for loop if you want to create multiple records, You can change the number from 6 to anything as per your requirement
var gr = new GlideRecord('table_name');
gr.intialize();
gr.setValue('field_name','value');
gr.setValue('field_name','value');
gr.insert();
}
}