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

Nitesh Balusu
Giga Guru

var gr = new GlideRecord('x);

var tkt = new GlideRecord('x');

 

are you gliding to the same table twice?

yeah initially i tried that and later I tried commenting out var tkt = new GlideRecord('x')

But still the same. creating multiple records

Since you want your record to be created when that error message is thrown, do this.

 

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.');

var tkt = new GlideRecord('x');

tkt.initialize();

tkt.fieldb = gr.number;

tkt.description = current.description;

tkt.u_name = current.u_name;

tkt.insert();

current,setabortAction(true);

}


 

I did the same but still it going to Infinite loop.

Even I kept current.setworkflow(false).

 

I know why this is happening because I am running before insert each time a record is created it will execute the BR and it will create a new Record, so its going to Infinite Loop. 

To Stop this Loop I need to add one more condition intially like 

If FieldB is Blank I need to run this if it is not blank then there is no need to throw an error.

so I addedd

gr.addEncodedQuery('fieldB=NULL');

 

but inorder to make this true I need to populate the Ticket number initially which is not happening. If i fix that then this loop will break. But that assignment is not working for some reason.