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.

Check if record inserted or not

RudhraKAM
Tera Guru

Hello there 

I have a requirement where We are creating a record on u_object table using work flow script , how do we validated it if the record is created or not ?

One possible solution is (may be if condition) to  check the table if the record is created then move on if not create a task and loop it back for run script to create .

 

Can you please help me with the if condition script in workflow 

1 ACCEPTED SOLUTION

You should be using a workflow scratchpad for this to pass value from one activity to another.

For ex

var refSysId='';
var userGr = new GlideRecord("u_object_code");
userGr.initialize();
userGr.u_value=current.variables.clove;

userGr.u_ritm= current.sys_id;
workflow.scratchpad.refSysId=userGr.insert();

 

 

 var rec = new GlideRecord('u_object_code');
  rec.addQuery('sys_id', workflow.scratchpad.refSysId);
  rec.query();
  if (rec.next()) {

true

} else {

false

}

 

But , if you are creating a record, it will get created. You dont need to verify it.


Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

8 REPLIES 8

Allen Andreas
Tera Patron

Hi,

Is there a reason why it wouldn't be created the first time through? Do you have questions about your code?

Anyways, you can use the sys_id from the script you have that's creating the record and then run a glide against that table to see if the record is found. Pretty simple.

So set your insert(); action to a variable like... sysId = variable.insert();

Then, go for glidrecord query (sys_id, sysId); and then

if (variable.next()) {

do x

} else {

do y

}

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Thanks Allen 

Can you please correct me if i did any thing wrong 

var refSysId=0;
var userGr = new GlideRecord("u_object_code");
userGr.initialize();
userGr.u_value=current.variables.clove;

userGr.u_ritm= current.sys_id;
refSysId=userGr.insert();

 

 

after this run script activity i can use the if activity in work flow with following code ?

How will the refSysId value transfer from run script to if activity ? 

 var rec = new GlideRecord('u_object_code');
  rec.addQuery('sys_id', refSysId);
  rec.query();
  if (variable.next()) {

true

} else {

false

}

This should work. If you're using the refSysId in a later activity in the workflow you'll have to pass /call it using the scratchpad object. You would use something like: 

workflow.scratchpad.refSysId = userGr.insert();

 

You should be using a workflow scratchpad for this to pass value from one activity to another.

For ex

var refSysId='';
var userGr = new GlideRecord("u_object_code");
userGr.initialize();
userGr.u_value=current.variables.clove;

userGr.u_ritm= current.sys_id;
workflow.scratchpad.refSysId=userGr.insert();

 

 

 var rec = new GlideRecord('u_object_code');
  rec.addQuery('sys_id', workflow.scratchpad.refSysId);
  rec.query();
  if (rec.next()) {

true

} else {

false

}

 

But , if you are creating a record, it will get created. You dont need to verify it.


Please mark this response as correct or helpful if it assisted you with your question.