You need to sign in to do that
Don't have an account?
Sreelakshmi Asokan
trigger to prevent duplicate contact based on Email
Hi,
I was trying to practice writing trigger to prevent duplicates on contact based on the email. But I am getting an error "System.FinalException: SObject row does not allow errors". Adding my code below, please help me understanding the issue.
public class preventDuplicate {
public static void duplicatePreventMthd(List<Contact> conList){
Map<String,Contact> updatedMap = new Map<String,Contact>();
if(!conList.isEmpty()){
for(Contact con:conList){
updatedMap.put(con.Email,con);
}
}
List<Contact> fetchedContacts =[SELECT Id,Email FROM Contact WHERE Email IN:updatedMap.keySet()];
if(!fetchedContacts.isEmpty()){
for(Contact conn:fetchedContacts){
if(updatedMap.containsKey(conn.Email)){
conn.Email.addError('Duplicate Contact not allowed');
}
}
}
}
}
I was trying to practice writing trigger to prevent duplicates on contact based on the email. But I am getting an error "System.FinalException: SObject row does not allow errors". Adding my code below, please help me understanding the issue.
public class preventDuplicate {
public static void duplicatePreventMthd(List<Contact> conList){
Map<String,Contact> updatedMap = new Map<String,Contact>();
if(!conList.isEmpty()){
for(Contact con:conList){
updatedMap.put(con.Email,con);
}
}
List<Contact> fetchedContacts =[SELECT Id,Email FROM Contact WHERE Email IN:updatedMap.keySet()];
if(!fetchedContacts.isEmpty()){
for(Contact conn:fetchedContacts){
if(updatedMap.containsKey(conn.Email)){
conn.Email.addError('Duplicate Contact not allowed');
}
}
}
}
}
The error "System.FinalException: SObject row does not allow errors" occurs when you try to add an error to a record that has already been committed to the database. In your code, you are trying to add an error to a fetched contact record that was queried from the database.
Please try with below code :
'
If it helps please mark this as Best Answer.
Thank you.