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

A little clean up on the script. It would be more efficient to use a "get" vs a query because it will increase performance.

rec.get(<sys_id>)

 

Also in the ".next()". If you just need to know if the record exists, you can use ".hasNext()" it will return true/false so you are not using up memory by holding the contents of that record while processing the rest of the script.

Hi, yea, as stated, if you're needing to use it elsewhere, then you'd use a workflow scratchpad variable:

https://docs.servicenow.com/bundle/helsinki-servicenow-platform/page/administer/using-workflows/conc...

Please mark reply (my original reply) as Correct if it helped answer your question.

Thanks!


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

Hello,

I see you've marked a response as Correct. What did you end up doing? If you ended up querying using the sys_id of the newly created record, as was mentioned by me in the first reply to your post, then let us know? If you didn't do any of that, let us know too?

Currently, we're unsure and others reading this post may not know since you marked another post as Correct, which was only a follow-up question you had to my post...which explained what to do and could be the Correct answer. In that post you marked Correct, there's a last line of text saying you didn't even need to do any of this...so confusion is left for us.

Thanks!


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

A reason for needing to check if it successfully inserted: If there is a business rule or a flow that might block the insert.