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

Okay question, field B is a reference field. when you say field B=123 is it even taking the value?

And i still didn't your point of "Field B which is a reference field and refers to Table 'sample'."

Field B is a reference to table 'sample'. So its on another table and not on 'sample' because it can't be on 'sample' and refer to 'sample' itself?

Yes, Field B is a reference field of the Same table in the same table.

i.e 

On sample Table I have a Field B which refers to same table record.

 

If I say like

current.u_field_b = gr.sys_id.

It is getting filled with 123 

but when tried by using gr.u_field_b = gr.sys_id its not setting the value 123 in Field B

Not sure why you have a field on a table referring to the same table, anyway

 

gr.u_field_b = gr.sys_id;

 

the reason this will not work is, when you're querying the table, the query conditions could satisfy multiple records, it would not know which record's sys_id to add to field B, it will work only if only one record is found.

 

You have to figure out other conditions, your query statements look very odd to me.

Exactly, It will work only if a record is found with matching value in Field A

(There will not be multiple records with same field value in A because of the BR.)

The same condition which I am checking for Field A.

There will be only one other record (Existing Record) with the Same value as user Provided in new Record. Which I am able to Find that Record and Record Number.

Now after Finding the Record number I need to create a Record with that Matching number in Field B and all other field value that User provided in the rejected Record.

You cannot do that because Field B is a reference field, and reference fields can only be filled with records that are already existing, you cannot insert a value into field B without a record already existing in you "sample" table.