How to create a record from business Rule

ursnani
Giga Guru

I have a business rule on a table X which checks for Unique Field Value in A.

If user wants to create a record with same value in Field A, then I need to create a new record with the data that user provided and populate Field B with Field A Number.

 

Fields on Table X

Field A reference Field

Field B Reference to the table X

Business Rule:

Before Insert/Update

 

var gr = new GlideRecord('x);

gr.addQuery('fieldA', current.fieldA);

gr.addQuery('sys_id','!=',current,sys_id);

gr.query();

if(gr.next()){

gs.addInfoMessage('Record with Field A already Exists, Hence creating a new Record.')

current,setabortAction(true);

}

 

Untill Here Its working FIne and as expected 

Now my task is to create a New record in the same table if that error message is thrown. So I have added this code to the existing code

 

Business Rule:

Before Insert/Update

 

var gr = new GlideRecord('x);

gr.addQuery('fieldA', current.fieldA);

gr.addQuery('sys_id','!=',current,sys_id);

gr.query();

if(gr.next()){

gs.addInfoMessage('Record with Field A already Exists, Hence creating a new Record.');

current,setabortAction(true);

}

//New COde added to existing one

var tkt = new GlideRecord('x');

tkt.initialize();

tkt.fieldb = gr.number;

tkt.description = current.description;

tkt.u_name = current.u_name;

tkt.insert();

These Extra lines of code is causing to create Multiple Record but not Single record. and also I want to Have the Number of Existing record in Field B which is not having.

Can some please please let me know where I am going wrong Its very urgent fix i need to make


Thanks.

 

 

 

 

23 REPLIES 23

can you remove before update and just do before insert. Also remove the setabort action statement, its not necessary i guess.

If I remove the Setabort action then it will insert the record to the table with the same value in the field.

Something I am missing in this and its going to Infinite loop.

ursnani
Giga Guru

Hi All,

 

Can someone Please respond on this and help me to figure out why this is going to Infinite loop.

on what table did you write the business rule on? is it X? if it's already written on X, why are we gliding into the table again?

Okay, i think i know what to do, instead of doing this in a business rule, why don't you do it in the "Submit" UI action? That way only when a user hits submit, it gives them the error message and it runs the code and creates a new record.