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

I think again you are missing the point.

We already have the Record in Sample Table.

BR Runs on Insert and It is checking for Record with Same value in Field A correct, that means Already record Exist with Same value in Field A. I am getting the sys_id of the Existing record from "gr.sys_id"

 

Again I am pasting the Example I gave.

 

OK Let me frame my requirement again.

 

I have a table 'sample' where in I have 

Field A which is String field.

Field B which is a reference field and refers to Table 'sample'.

 

Existing Record:

Number: 123

Field A value = Test

Field B value = empty

 

 

New Record:

Number: 456

Field A value = Test

Field B Value = Empty

 

Just think, If I am able to Abort the Action that means I already have a Record with same value in Field A Correct.

I am trying to get that record sys_id and Populate it in the New record that I am trying to Creating.

 

give log statements and see what is printing for gr.sys_id, see if you're getting the value or its saying undefined.

I am getting the Value and I am able to set the Field B value with the number of existing Record in Field B, But when trying to Create a Record its not setting the Value.

Even I am throwing the Error with the Existing Record number.

 

Here are the Scree shots 

Exsiting Record :

 

 

As I said I tried to check whether I am able to Set the Field B value to the Found Record and it worked and I was able to set the value of Field B to the Found record.

 

 

Now I am trying to TO the same by getting the Value of the record that User is trying to Submit and Create a new Record which is going to Infinite Loop.

 

Why does that have to be a reference field, can't you just make it a string field and copy the number directly using gr.number.getDisplayValue()

A field referring to the same table is very strange to me.