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.

Script to insert record in custom table

Sneha39
Mega Guru

I want to insert catalog item variable record to custom table as soon as the RITM gets created.

I am writing after BR on ritm table( its not working)  for same but looking for more suggestions.

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

// Add your code here

var tbl1 = new GlideRecord('u_cst_account');
tbl1.query();
if(tbl1.next())
{
var rec1=tbl1.u_account_names;
if(current.variables.var_type1 == 'AST1')
{
current.variables.var_name = rec1;
tbl1.setValue(name,current.variables.var_name);

}
}

})(current, previous);

1 ACCEPTED SOLUTION

Ok if its inserting of record

then you need to do 2 things

1. Make a check if same record exist

2. If Yes then ignore or If No then insert

var recinsert = new GlideRecord('u_cst_account');\
recinsert.addQuery('columnename',current.variables.var_name);//add here column name against which you want to check
recinsert.query();
if(!recinsert.next()) //If no record found
{
var recinsertnow = new GlideRecord('u_cst_account');
recinsert.initialize();
recinsert.setValue(name,current.variables.var_name);
recinsert.insert();
}

 

Hope this helps

Thank you
Prasad

View solution in original post

16 REPLIES 16

Prasad Pagar
Mega Sage

Hi Sneha,

 

So you already have existing record in u_cst_account and then just want to update it with new values coming from RITM variables?

OR

You want to insert new record all together?

Thank you
Prasad

 

I want to insert new record ( with the check that will see if same record shouldn't already exists )

Ok if its inserting of record

then you need to do 2 things

1. Make a check if same record exist

2. If Yes then ignore or If No then insert

var recinsert = new GlideRecord('u_cst_account');\
recinsert.addQuery('columnename',current.variables.var_name);//add here column name against which you want to check
recinsert.query();
if(!recinsert.next()) //If no record found
{
var recinsertnow = new GlideRecord('u_cst_account');
recinsert.initialize();
recinsert.setValue(name,current.variables.var_name);
recinsert.insert();
}

 

Hope this helps

Thank you
Prasad

its inserting empty record.