You need to sign in to do that
Don't have an account?
TheRealist
How to avoid duplication of records based on 3 fields (data type:Text)
Hi
i have a scnario:3 fields (data type:Text) First Name,Middle Name,Last Name, if any Duplicate record is going to be created with same values in those three fields, that operation should fail by throwing an error,Is there any possible way to achieve this,through (UI configuration or trigger),
I wonder..how to compare text fields that too 3 Text fields.
i have a scnario:3 fields (data type:Text) First Name,Middle Name,Last Name, if any Duplicate record is going to be created with same values in those three fields, that operation should fail by throwing an error,Is there any possible way to achieve this,through (UI configuration or trigger),
I wonder..how to compare text fields that too 3 Text fields.
Then add a validation rule to prvent the duplication.
ie. fomula field: concatenatingField.
in validationg rule, check condition: PRIORVALUE( concatenatingField__c ) = concatenatingField__c to prevent the duplication.
Can you try the below code through trigger.
String errorMsg ='A Contact with these values already exists';
Map<String, Contact> ContactMap1 = new Map<String, Contact>();
Map<String, Contact> ContactMap2 = new Map<String, Contact>();
Map<String, Contact> ContactMap3 = new Map<String, Contact>();
for (Contact Contact : System.Trigger.new)
{
if (System.Trigger.isInsert || (System.Trigger.isUpdate &&
(Contact.FirstName != System.Trigger.oldMap.get(Contact.Id).FirstName || Contact.LastName != System.Trigger.oldMap.get(Contact.Id).LastName || Contact.MiddleName != System.Trigger.oldMap.get(Contact.Id).MiddleName)))
{
// Make sure another new Contact isn't also a duplicate
if (ContactMap1.containsKey(Contact.FirstName) && ContactMap2.containsKey(Contact.LastName) && ContactMap3.containsKey(Contact.MiddleName) )
{
Contact.addError(errMsg);
}
else
{
ContactMap1.put(Contact.FirstName,Contact);
ContactMap2.put(Contact.LastName,Contact);
ContactMap3.put(Contact.MiddleName,Contact);
}
}
}
// Using a single database query, find all the Contacts in
// the database that are having same FirstName,LastName and MiddleName as any
// of the Contacts being inserted or updated.
for (Contact Contact : [SELECT FirstName,LastName,MiddleName FROM Contact where FirstName IN:ContactMap1.KeySet() and LastName IN:ContactMap2.keySet() and MiddleName IN:ContactMap3.keySet()])
{
Contact newContact = ContactMap1.get(Contact.FirstName);
newContact.company.addError(errorMsgMsg);
}
Regards,
Sridhar Bonagiri
Here are the steps:
1.Create a unique field of type text.
2. While creatig check don't allow duplicate on the field creation definition.
3. Write a workflow rule to put the concatenated value of the three fields that you don't want to have duplicates.
By doing above process, whenever a new records is going to be created unique field that you will create will not allow user to insert the data.
Hope this helps, let me know if you have further issues
mark this as best answers if this helps, for helping others