Business Rule to Validate data of three Fields(Type Choice) to avoid Duplicate Entries

Tahzeeb
Giga Expert

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');

  1. newrec.addQuery('choicefile_one', current.choicefile_one);
  2. newrec.addQuery('choicefile_two', current.choicefile_two);
  3. 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 

  1. 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

1 ACCEPTED SOLUTION

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

Regards,
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.

View solution in original post

7 REPLIES 7

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

Regards,
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.

Mohit Kaushik
Mega Sage
Mega Sage

Hi @Tahzeeb ,

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

 

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

Vishnu Prasad K
Giga Guru

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 ?