In a table How to restrict/alert the creation of duplicate records based on several fields ?

Hari95
Kilo Contributor

Hello Everyone,

I had a use case to restrict the creation of duplicate records on a table based on 3 fields.

1. Account 

2. Assignment Grp

3. Trouble Type 

If any records with these 3 field values existing on the table, system needs to alert the user ex. ''Record with similar combination existed''.

Can anyone help me to achieve this requirement?

Thanks,

Hari

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you should write before insert business rule on the custom table and then query the table with these 3 fields

Note: Ensure you give correct field names here

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

	// Add your code here
	var gr = new GlideRecord(current.getTableName());
	gr.addQuery("u_account", current.u_account); // give correct field name
	gr.addQuery("u_trouble_type", current.u_trouble_type); // give correct field name
	gr.addQuery("u_assignment_group", current.u_assignment_group); // give correct field name
	gr.setLimit(1);
	gr.query();
	if (gr.hasNext()) {
		gs.addErrorMessage("This is a duplicate record");
		current.setAbortAction(true);
	}

})(current, previous);

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

9 REPLIES 9

Hari95
Kilo Contributor

Hi suvro,

On which table should i need toi write that BR ?

I need to restrict on one of the custom created table

You should write this business rule on your custom table and in the script also glide on the same table

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you should write before insert business rule on the custom table and then query the table with these 3 fields

Note: Ensure you give correct field names here

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

	// Add your code here
	var gr = new GlideRecord(current.getTableName());
	gr.addQuery("u_account", current.u_account); // give correct field name
	gr.addQuery("u_trouble_type", current.u_trouble_type); // give correct field name
	gr.addQuery("u_assignment_group", current.u_assignment_group); // give correct field name
	gr.setLimit(1);
	gr.query();
	if (gr.hasNext()) {
		gs.addErrorMessage("This is a duplicate record");
		current.setAbortAction(true);
	}

})(current, previous);

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,

It's working. Thank you 

Regards,

Hari

Hey Ankur,
 I have also same requirement but i want to restrict and notify the user on catalog item form that the request for this item already exist in an open state. I am using 2 fields i.e

asset tags and replacement checkbox field.

Could you please provide some assistance