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.

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

Hi Tolga,



I have already marked as correct answer and helpful.



Please find my another issue here. I have raised one more discussion.



Please help me on this.



Malaisamy


Tolga,



This is my new topic Need script to add days (taking from dropdown field)



Help me on this