How to create a record from business Rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2018 09:55 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2018 09:44 AM
its going into infinite loop because everytime it inserts a record its calling the business rule and this is happening again and again.
Instead of doing this in a business rule, why don't you add this code in the UI Action"Submit" on table X, that way it runs only once when a user submits a record, it checks if field A already exists in the table and then creates a new record.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2018 09:44 AM
Or you can also write this in a script include and call the script include from the "Submit" action.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2018 09:50 AM
But if you see I gave condition to skip this
by adding one query
gr.addEncodedQuery('fieldBISEMPTY');
if this condition is met then only it must throw an error and set abort action and create a record when creating a record.
If this Field is not empty then this business rule will be false as we have addQuery condition
Am I correct ??

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2018 10:04 AM
where exactly did you give this condition? in your question it states field B is reference to table X, so its not on table X right? How did you give the condition without it being on table?
Also the business rule will not really be false if you give the condition in the script, it will not run only if give your conditions in the conditions section, else it will enter the script no matter what.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2018 11:47 AM
Hi Niteesh,
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
" I should abort the Action of New Record as Field A value is same in Both Existing Record and New Record which I am able to do by using "Before" BR on "Insert and Update" as Below.
var gr = new GlideRecord('sample');
gr.addQuery('u_field_a',current.u_field_a);//It is checking for field value in Field A ==>This is true
gr.addEncodedQuery('u_field_bISEMPTY');//it is checking whether the Field B is empty or not ==> This is True
gr.addQuery('sys_id','!=',current.sys_id);//It is checking if the record is new record or not. ==> This is True
gr.query();
if(gr.next()){ //If all the 3 Conditions are True then it will enter this Loop as per my UNderstanding.
gs.addInfoMessage('Allready record with Same Field A exist in another record '+gr.number);
current.u_field_a= '';
current.setAbortAction(true);
}
the Above Code is working As per my Expectations.
Now after throwing an error. I am trying to create a New Record with the Values of the user that he already filled. and also I am trying to Fill the Field B with the Number of Feild A
The New Record should Look like.
Field A Value = Test
Field B Value = 123.
Now before Inserting again my BR will trigger.
var gr = new GlideRecord('sample');
gr.addQuery('u_field_a',current.u_field_a);//It is checking for field value in Field A ==>This is true
gr.addEncodedQuery('u_field_bISEMPTY');//it is checking whether the Field B is empty or not ==> This is false
gr.addQuery('sys_id','!=',current.sys_id);//It is checking if the record is new record or not. ==> This is false
gr.query();
if(gr.next()){ //If all the 3 Conditions are True then it will enter this Loop as per my UNderstanding.
gs.addInfoMessage('Allready record with Same Field A exist in another record '+gr.number);
current.u_field_a= '';
current.setAbortAction(true);
}
Now this Br Should not Throw an error as per my Understanding. Am I Correct as Field B is Not Empty