- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-28-2022 04:05 AM
Hello Experts,
We have a table which has three Choice FIelds > I need to write a Business Rule to validate if the data is already present to avoid duplicate entries.but somehow I am not able to retrieve the value of choice Fields.
Below is the code that I have tried
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var newrec = new GlideRecord('custom_table');
- newrec.addQuery('choicefile_one', current.choicefile_one);
- newrec.addQuery('choicefile_two', current.choicefile_two);
- newrec.addQuery(' choicefile_three', current.choicefile_three);
newrec.query();
while (newrec.next()) {
gs.addErrorMessage("Duplicte Entry ");
current.setAbortAction(true);
}
})(current, previous);
I also tried to modify Lines 1,2,3 as
- newrec.addQuery('choicefile_one', current.choicefile_one.getDisplayValue());
But its not giving any Value
Tried to run the above script in background script but the value is showing as null.
Please suggest how this can be done
Regards,
TM
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-28-2022 04:59 AM
Try this:
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var newrec = new GlideRecord('custom_table');
newrec.addQuery('choicefile_one', current.choicefile_one);
newrec.addQuery('choicefile_two', current.choicefile_two);
newrec.addQuery(' choicefile_three', current.choicefile_three);
newrec.query();
if (newrec.getRowCount() >= 1) {
gs.addErrorMessage("Duplicte Entry ");
current.setAbortAction(true);
}
})(current, previous);
Regards,
Michael
Michael
Please mark the suggestion as helpful/like, if you find it useful to you or others who wants to refer similar content.
Please mark the solution as correct, if the answer provided has resolved your query.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-28-2022 04:59 AM
Try this:
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var newrec = new GlideRecord('custom_table');
newrec.addQuery('choicefile_one', current.choicefile_one);
newrec.addQuery('choicefile_two', current.choicefile_two);
newrec.addQuery(' choicefile_three', current.choicefile_three);
newrec.query();
if (newrec.getRowCount() >= 1) {
gs.addErrorMessage("Duplicte Entry ");
current.setAbortAction(true);
}
})(current, previous);
Regards,
Michael
Michael
Please mark the suggestion as helpful/like, if you find it useful to you or others who wants to refer similar content.
Please mark the solution as correct, if the answer provided has resolved your query.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-28-2022 05:16 AM
Hi
you can use below script to check your criteria of delicacy and revoke the user to insert/update record with duplicate values.
And make sure the field names are correct that is very important.
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var rec = new GlideRecord('u_test_table');
// where u_choice1, u_choice2 and u_choice3 are the backend names of your choice fields.
rec.addQuery('u_choice1',current.u_choice1);
rec.addQuery('u_choice2',current.u_choice2);
rec.addQuery('u_choice3',current.u_choice3);
rec.query();
if(rec.next()){
gs.addErrorMessage('Duplicate entry');
current.setAbortAction(true);
}
})(current, previous);
I have tested this code in my personal instance and it is working fine.
Please mark this as correct and helpful if it resolved the query or lead you in right direction.
Thanks,
Mohit Kaushik
Community Rising Star 2022
Mohit Kaushik
ServiceNow MVP (2023-2025)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-28-2022 05:27 AM
Hi Tahzeeb,
Use it in before update BR. And what does that 1,2 & 3 means, and is it an OR condition or AND condition ?