+ Start a Discussion
Gabriel DiasGabriel Dias 

Update record not working when I addError in a trigger

I have a trigger that checks if exists a duplicate record before insert a Lead and when it find a duplicate record I need to update two fields on this record with the date of the try to insert a duplicate record and a boolean that shows if you hear an attempt to register for the lead, but when I use AddError the other record is not saved in the database.

Does anyone have a workaround to this problem? Future methods also don't work.
Malni Chandrasekaran 2Malni Chandrasekaran 2
Hi Gabriel,
Try using the variable instead of trigger context directly to addError.  Eg: Instead of using "  trigger.new[0].addError('Some error')  "" bring the record to a variable like

for (<object name> a : Trigger.new) {
<your logic>
a.addError(error message);
}

Hope this helps!
Malni Chandrasekaran 2Malni Chandrasekaran 2
And also try this syntax "addError(<fieldName>, errorMsg)" rather "addError(ErrMsg)"
Gabriel DiasGabriel Dias
Hi Malni, I'm using the for loop with the trigger.new to addError.