0

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

1 Answers1

0

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.

ContactTriggerHandler.IsPricingLetter(trigger.new[0]);

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".

eyescream
  • 18,088
  • 2
  • 34
  • 46
  • 1
    Your fix worked. Used Trigger.new[0] and I'm actually getting the intended action now. I was thinking that since it's being triggered on the record, that the Trigger.new would only pull in that specific record and use the "new" data to check against the trigger handler, then throw the error before update (save). I do understand your point in doing the mass change. If our users learn how to do that, I'll revisit the code. Thanks again! – RobIsNotATechGuy Jan 20 '23 at 21:55