Created a Contact Trigger and a ContactTriggerHandler class to produce an error if the Contact is related to a relationship that contains 'Pricing Letters' and any item in the Address or Email Address is blank. Code compiles but receiving the error below. Any help is appreciated.
ERROR: Review the errors on this page. ContactTrigger: execution of BeforeUpdate caused by: System.FinalException: SObject row does not allow errors Class.ContactTriggerHandler.IsPricingLetter: line 9, column 1 Trigger.ContactTrigger: line 5, column 1
TRIGGER
trigger ContactTrigger on Contact (before update) {
if(trigger.isbefore && trigger.isupdate){
Contact checkcontact = [SELECT id,Email,MailingStreet,MailingCity,MailingState,MailingPostalCode,MailingCountry
FROM Contact WHERE id IN : Trigger.new];
ContactTriggerHandler.IsPricingLetter(checkcontact);
}//End If isBefore && isUpdate
}//End Class
TRIGGER HANDLER
public class ContactTriggerHandler {
public static void IsPricingLetter(Contact con){
//Get list of relationship records that contain Pricing Letters role on the Contact ID being triggered.
for(AccountContactRelation c : [SELECT ContactId FROM accountcontactrelation WHERE roles INCLUDES ('Pricing Letters') AND Contactid = :con.Id]){
//If triggering Contact ID is present, check for missing mailing address components or missing email address.
if(con.MailingStreet==null){
con.addError('Mailing street on a Pricing Letter Contact cannot be null.');
}//End If Mailing Street
if(con.MailingCity==null){
con.addError('Mailing City on a Pricing Letter Contact cannot be null.');
}//End If Mailing City
if(con.MailingPostalCode==null){
con.addError('Mailing City on a Pricing Letter Contact cannot be null.');
}//End If Mailing Postal
if(con.MailingState==null){
con.addError('Mailing State on a Pricing Letter Contact cannot be null.');
}//End If Mailing State
if(con.MailingCountry==null){
con.addError('Mailing Country on a Pricing Letter Contact cannot be null.');
}//End If Mailing Country
if(con.Email==null){
con.addError('Email Address on a Pricing Letter Contact cannot be null.');
}//End If Mailing Country
}//End For Loop
}//End IsPricingLetter Method
}//End Class
Don't query. Last time I told you that if you query in a "before update" you'll get state of database before the edit, without values changed by the user.
You can't just
addError
on something you queried, potentially totally unrelated to the edit.it's bit dangerous (you check only 1st instead of all, problem if anybody does mass edit from a listview for example) but should be enough and "errorable".