Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Duplicate number created while creating record

harry24
Tera Contributor

Duplicate number created while creating record , even though the auto numbering feature is on

8 REPLIES 8

Hello @harry24,

 

Yes, while I was facing the same issue we created a BR(business rule) where in all the duplicate number records were deleted and it used to check for unique number while creating a new record.

can you help me with the business rule?

Hello @harry24,

 

Following script worked for me:

var testGr = new GlideAggregate('u_test');   // table on which deletion is to be performed

 

testGr.groupBy('u_test_column');   // column which you think has duplicate values i think it would be the value column for sys_choice table

 

testGr.query();

 

var testGR1;



while (testGr.next()) {  

 

    testGR1 = new GlideRecord('u_test');  

 

    testGR1.addQuery('u_test_column', testGr.u_test_column);  

 

    testGR1.query();  

 

    testGR1.next(); // Skip the first result  

 

    while (testGR1.next()) { // delete the next one

 

            testGR1.deleteRecord();  

 

    }  

 

}  



you can test this script by running it against incident table:



Steps to execute/test this script:



1) In incident table create 2 records with same short_description

 

2) Modify the code above and check whether only 1 record is remaining and 1 got deleted.



var testGr = new GlideAggregate('incident');  

 

testGr.groupBy('short_description');  

 

testGr.query();

 

var testGR1;



while (testGr.next()) {  

 

    testGR1 = new GlideRecord('incident');  

 

    testGR1.addQuery('short_description', testGr.short_description);  

 

    testGR1.query();  

 

    testGR1.next(); // Skip the first result  

 

    while (testGR1.next()) { // delete the next one

 

            testGR1.deleteRecord();  

 

    }  

 

}  

Shruti Khaire
Kilo Sage

Hello @harry24,

 

Did my below script solve your question, if not let me know so that I'll be able to help you.