How to set a combination of 2 fields unique?

JLeong
Mega Sage

Hi Guys,

I have a table for Web Accounts. The entries should be unique based on the URL and Account ID entered. How to set a combination of 2 fields to be unique.

Thanks.

 

 

1 ACCEPTED SOLUTION

Anil Lande
Kilo Patron

Hi,

You cannot have the table level configurations to create Unique field as combination of two fields. However, you can have a Before insert/update business rule on your table to validate your new entry.

In the script section you can glide to current table and check if the record already exits with current records value i.e URL and Account ID. If record exist abort the operation. 

The sample code would be like:

var web_acc = new GlideRecord("web_account_table");
web_acc.addQuery('u_url',current.u_url);
web_acc.addQuery('u_account_id',current.u_account_id);
web_acc.query();
if(web_acc.next())
{
current.setAbortAction(true);
}

 

Thanks,

Anil Lande

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

8 REPLIES 8

Anil Lande
Kilo Patron

Hi,

You cannot have the table level configurations to create Unique field as combination of two fields. However, you can have a Before insert/update business rule on your table to validate your new entry.

In the script section you can glide to current table and check if the record already exits with current records value i.e URL and Account ID. If record exist abort the operation. 

The sample code would be like:

var web_acc = new GlideRecord("web_account_table");
web_acc.addQuery('u_url',current.u_url);
web_acc.addQuery('u_account_id',current.u_account_id);
web_acc.query();
if(web_acc.next())
{
current.setAbortAction(true);
}

 

Thanks,

Anil Lande

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Thanks Anil!!

This may be an ancient post but the above answer is not true, or is no longer true. You can create a unique index based on multiple fields. If the current data violates the index you want to create you will have to either delete the offending record or delete all records. So this is best done when creating the table.

  1. Go to table list or form
  2. Right click header and choose Configure > Table
  3. scroll down to Database Indexes related list
  4. If there is an existing index on one of the fields you want to use you can optionally click it and drop the index. However when you create the new index the system will amend that index. 
  5. Click New
  6. Move the fields you want to the selected box 
  7. Check Unique Index
  8. Click Create Index

    TonyV_1-1738943578643.png

     


     

 

 

bdubz7
Giga Expert

you can create field values based on a calculated value.

 

On the Dictionary Entry for the attribute, you can make it a Calculated Value to combine 2 fields or something similar.