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.

Preventing Duplicate CIs

eddiemorales
Tera Contributor

We have users that are constantly adding New CIs/Phone Numbers and having an issue with duplicate phone #s being added because those are simply not being checked consistenly to see if it already exists. What is the best way to prevent this creation of duplicate phone#s? Business rule, etc? We are looking more in the lines of having a flag or something where once the # is saved, it prompts that "this # already exists" and denies the creation of the new #.

Thanks!

1 ACCEPTED SOLUTION

Hi Eddie,



Sorry, it is supposed to be gs.addErrorMessage() and not current.addErrorMessage().



Here is the complete script:


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


  var gr = new GlideRecord('u_phone_numbers');


  gr.addQuery('name',current.name);


  gr.addQuery('u_switch',current.u_switch);


  gr.query();


  if(gr.next()) {


      gs.addErrorMessage("The phone number on the selected switch is already in use");


      current.setAbortAction(true);


  }


})(current, previous);



I hope this gets you what you need.



/Lasse


View solution in original post

16 REPLIES 16

No problem. Let me know if you run into any issues 🙂


Hi Lasse,



I entered the new code but I am still having a couple errors (excuse my "rookie" skills☺). I also forgot one criteria of this that makes it a little more difficult. Let me explain:



So below is the form for our Phone Numbers. The Phone number itself is not unique because we have several 3 digit extensions that are the same. Although, these 3s are the same, the unique 1-1 is extensions with the respective switch. For example, there could be a 3 digit extension "222". We copuld have two extensions: 222 @ Switch "El Paso Avaya CM" & 222 @ Switch "Lubbock Avaya CM". So in this case, we need to have a query that looks at the "phone #" with its respective "switch".





Here is the "phone number" label:





Here is the "switch" label:





Here is the error I am getting with the code I have so far:





Please advise…



Thanks Lasse!


Eddie


Hi Eddie,



The script error is my bad. The "New" should have been "new". Sorry about that.



Based on your screenshots I modified the script so that it will take both the switch (u_switch) and the number (name) into consideration:



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


  var gr = new GlideRecord('u_phone_numbers');


  gr.addQuery('name',current.name);


  gr.addQuery('u_switch',current.u_switch);


  gr.query();


  if(gr.next()) {


  current.setAbortAction(true);


  current.addErrorMessage("The phone number on the selected switch is already in use");


  }


})(current, previous);



I hope this helps.



/Lasse


Awesome...I will give it a go and see what happens. I really appreciate you lasse!



Thanks!


Eddie





Sent from my Sprint Samsung Galaxy S ® 6 edge.


Hi Lasse,



I tried the new code and the duplicate restriction seems to be working by giving me an "invalid insert" flag when I tried to test a dupe # with the same switch. I did not get the error message though. Just to validate, is the "invalid insert" flag what is expected?







Thanks,


Eddie