On Insert Check if Record ID exists to copy field value

brown9394
Tera Expert

Hello Experts,

I have a before BR that generates a GUID on a field on Insert. 

current.u_guid = gs.generateGUID();

What I need for to happen is that I want to check if every new entry's 'u_record_id' already exists in the table, If it does then copy 'u_guid' from the existing record to new record's 'u_guid' field, and if it doesn't, then generate a new one. 

How can I achieve this? 

Thanks in advance! 

1 ACCEPTED SOLUTION

SanjivMeher
Kilo Patron
Kilo Patron

You need an onBefore Insert Business Rule for this.

 

And in the script you need to write

var t = new GlideRecord('<Your table Name>');

t.addQuery('u_record_id',current.u_record_id);

t.query();



if (t.next())

{

current.u_guid = t.u_guid;

}

else

{

current.u_guid = gs.generateGUID();

}

 


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

View solution in original post

6 REPLIES 6

Yogi3
Kilo Guru

I am not quite sure what you ask is. If you want to know the sys_id of record before insert. Try using newRecord() method to find out sys_id of new record before actually committing to DB.

Hi Yogi, 

I'm not talking about checking for new sys_id, I'm wanting to check if NEW entry's u_record_id field has the same value in existing records, and if so, then copy the existing Record ID's u_guid to new entry's u_guid, otherwise generate a new u_guid.  

SanjivMeher
Kilo Patron
Kilo Patron

You need an onBefore Insert Business Rule for this.

 

And in the script you need to write

var t = new GlideRecord('<Your table Name>');

t.addQuery('u_record_id',current.u_record_id);

t.query();



if (t.next())

{

current.u_guid = t.u_guid;

}

else

{

current.u_guid = gs.generateGUID();

}

 


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

Hi Sanjiv, thanks for your reply. 

I tried this out but whether or not there is a match, it's not setting the u_guid at all.