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

Sure , let me try

tried from background script, its inserting empty value

 

var tbl1 = new GlideRecord('u_cst_account');
tbl1.query();
if(tbl1.next())
{
var rec1=tbl1.u_account_name;
var tbl1Insert = new GlideRecord('u_cst_account');
tbl1Insert.initialize();
tbl1Insert.setValue(rec1,'test');
tbl1Insert.insert();

}

Uttkarsh S
Tera Contributor

Hi Sneha,

Since you have mentioned that you have to insert catalog item variable records on the custom table, I'm assuming that you must be using either a workflow or a flow. If that's the case, you can utilize the run script activity or code snippet in workflow or flow respectively.

Also, you'll have your current object available on it, and I'm assuming that you have a unique identifier such as RITM, on your custom table, you won't have to apply a check for the same records.

 

In the script part, collect all the variables in an array and then loop through the array to insert a record for each variable.

 

var allVariables = [];

//Can be used to push as much variables as needed
allVariables.push(current.variables.variable1);
allVariables.push(current.variables.variable2);

for(var key in allVariables){
  var variableName = allVariables[key];
  insertRecord(variableName;
}

function insertRecord(variableName){
  var createRec = new GlideRecord('u_cst_account');
  createRec.initialize();
  createRec.name = variableName;
  createRec.ritm = current.number; //This will be the field value from the RITM table.
  createRec.insert()
}

 

Please mark correct/helpful if it answers/solves your query.

Cheers,
Uttkarsh

Hi Uttkarsh 

I don't want to use workflow rather trying other methods like BR. please let me know if you have some suggestion over other options

Hi, If you don’t wanna use workflow then you can utilize the code structure (mentioned in my previous reply) in your BR as well. Check if your BR is getting invoked properly. Cheers, Uttkarsh