Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

How to set field value unique using scripts

malaisamy1
Kilo Contributor

Hi Guys,

Here i have one problem,

i want a ui action script for finding duplicate records.

My scenario is , if the field invoice_no has same value for two different record over the list,

when i click my ui action button it has to give me an error y saying already one record is there for this field invoice_no.

Is it possible. Can someone help me?

please find my below code

  1. getDuplicates();
  2. function getDuplicates() {
  3.     var dupRecords = [];
  4.     var gaDupCheck1 = new GlideAggregate('sys_user');
  5.     gaDupCheck1.addQuery('active','true');
  6.     gaDupCheck1.addAggregate('COUNT', 'user_name');
  7.     gaDupCheck1.groupBy('user_name');
  8.     gaDupCheck1.addHaving('COUNT', '>', 1);
  9.     gaDupCheck1.query();
  10.     while (gaDupCheck1.next()) {
  11.           dupRecords.push(gaDupCheck1.user_name.toString());
  12.     }
  13.     gs.print(dupRecords);
  14. }

its giving me an error 'addHaving is not allowed in scoped applications'

Is there any alternate solution for this scenario?

Thanks in Advance

Malai

1 ACCEPTED SOLUTION

tolga1
ServiceNow Employee
ServiceNow Employee

What about using a business rule instead with this code on insert/update?



(function executeRule(current, previous /*null when async*/) {


// Add your code here


if (current.invoice_no &&


(current.operation() == "insert" ||


(current.operation() == "update" && current.invoice_no.changes()))) {


var gr = new GlideRecord("table_where_invoice_no_is");


gr.addQuery("invoice_no", current.invoice_no);


gr.query();


if (gr.getRowCount() > 0) {


current.setAbortAction(true);


gs.addErrorMessage(gs.getMessage("Not allowed the duplicate invoice_no=" + current.invoice_no));


}


}


})(current, previous);


View solution in original post

6 REPLIES 6

Sanjeev Kumar1
Kilo Sage

Hi,



Your User table is not available for scoped application.


you need to contact ServiceNow.


Or Create an incident on Hi.



they will help you :0  


tolga1
ServiceNow Employee
ServiceNow Employee

What about using a business rule instead with this code on insert/update?



(function executeRule(current, previous /*null when async*/) {


// Add your code here


if (current.invoice_no &&


(current.operation() == "insert" ||


(current.operation() == "update" && current.invoice_no.changes()))) {


var gr = new GlideRecord("table_where_invoice_no_is");


gr.addQuery("invoice_no", current.invoice_no);


gr.query();


if (gr.getRowCount() > 0) {


current.setAbortAction(true);


gs.addErrorMessage(gs.getMessage("Not allowed the duplicate invoice_no=" + current.invoice_no));


}


}


})(current, previous);


Hi Tolga,



Its really amazing..super..it works perfectly like i wanted.



I Have one more requirement. Could you please help me on this,


i have one date field called invoice_date where i have one more field called day_s



where day_s has dropdown values like 10,15, 30 so on.



my result field = invoice_date + day_s


that is it has to add the days whatever there in the day_s field. and stored in my result field.



result field is a date field.



Is it possible? Do you have any idea?



Thanks in Advance,


Malaisamy Ji


tolga1
ServiceNow Employee
ServiceNow Employee

Hi Malaisamy


I'm glad, my solution helped. May I please ask you to mark my answer as "correct", if it resolved your issue?



As you already opened a new thread on your additional question, I'd propose to discuss it there and closed this thread (as it is a different topic), if okay for you.



Best Regards


Tolga