function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
LakisLakis 

Catch error exception and send email to a user with error details

I've got a working (Before update) trigger and a test class (with 77% pass). Before I move it to production I would like to:

1. Make sure that once trigger is moved into production, an email will be sent with error details to my account anytime error occurs.

2. Improve code coverage.

No idea how to do it... Can anyone help please? 

The trigger idea is pretty simple: If one of the picklist values on the Account with the specific record type 'Exhibitor'  is changed, corresponding picklists on related Contact records (Contact record type is called 'Exhibitor') must update.

Thanks in advance,
Lakis

***
trigger Contact_Importance_Update on Account (before update) {

    Id recTypeId = Account.sObjectType.getDescribe().getRecordTypeInfosByName().get('Exhibitor').getRecordTypeId(); //using it like this you won't have problems to deploy (and it'll save you a query)
    
    List<Account> accountsToVerify = new List<Account>();

   for(account a : trigger.new){
       if(a.RecordTypeId == recTypeId  //checks if the account has the correct RecordType
          && (a.PURE_Importance__c != trigger.oldMap.get(a.Id).PURE_Importance__c
          || a.WAA_Importance__c != trigger.oldMap.get(a.Id).WAA_Importance__c
          || a.LE_Miami_Importance__c != trigger.oldMap.get(a.Id).LE_Miami_Importance__c
          || a.PURE_Agent__c != trigger.oldMap.get(a.Id).PURE_Agent__c
          || a.LE_Miami_agent__c != trigger.oldMap.get(a.Id).LE_Miami_agent__c
          || a.WAA_agent__c != trigger.oldMap.get(a.Id).WAA_agent__c)){ //checks if the picklist is changed
           accountsToVerify.add(a);
       }
   }

   if(!accountsToVerify.isEmpty()){ //will only process if there's a need to.
      List<Contact> contactsToUpdate = [SELECT Id, AccountId, FirstName FROM Contact WHERE AccountId IN :accountsToVerify];
      for(Contact c : contactsToUpdate){
          Account a = trigger.newMap.get(c.AccountId);

          c.PURE_Importance_contact__c=a.PURE_Importance__c;
          c.We_Are_Africa_Importance_contact__c=a.WAA_Importance__c;
          c.LE_Miami_Importance_contacts__c=a.LE_Miami_Importance__c;
          c.PURE_Agent__c=a.PURE_Agent__c;
          c.LE_Miami_agent__c=a.LE_Miami_agent__c;
          c.WAA_agent__c=a.WAA_agent__c;
       }
       update contactsToUpdate;
   }


}

***

@isTest
private class Contact_Update{
    static testMethod void Contact_Importance_Update(){

        //Query the record types by developername to get their Ids
        RecordType acctExhibitorRT = [Select Id From RecordType Where sObjectType = 'Account' And DeveloperName = 'Exhibitor'];

        RecordType contExhibitorRT = [Select Id From RecordType Where sObjectType = 'Contact' And DeveloperName = 'Exhibitor'];                        
                 
        Account acc = new Account(
            Name = 'testaccount'
            ,Street__c = 'teststreet'
            ,Country_area_code__c = 'Afghanistan (GMT +4:30; area code +93)'
            ,Region__c = 'Asia - South-Central (and India Sub-Cont.)'
            ,RecordTypeId = acctExhibitorRT.Id //Set this account to the exhibitor Record Type
        );
        insert acc;
                    
        Contact con = new Contact(
            AccountId = acc.id
            ,Lastname = 'testcontact'
            ,Firstname ='testdata1'
            ,Email = 'lakis@o2.pl'
            ,OwnerId = Userinfo.getUserid()
            ,Country__c = 'Afghanistan (GMT +4:30; area code +93)'
            ,Region__c = 'Asia - South-Central (and India Sub-Cont.)'
            ,RecordTypeId = contExhibitorRT.Id //Set this contact to the exhibitor record type
        );
        insert con;

        acc.PURE_Importance__c = 'Leader';
        acc.PURE_Agent__c = 'Name1';
        acc.LE_Miami_Importance__c = 'In Hand';
        acc.LE_Miami_Agent__c = 'Name2';
        acc.WAA_Importance__c = 'Suspect';
        acc.WAA_Agent__c = 'Name3';
              
        Test.startTest();
        update acc;
        Test.stopTest();

        //Requery the contact record and check to make sure it was updated.
        Contact result = 
            [Select
                PURE_Importance_Contact__c
                ,We_Are_Africa_Importance_Contact__c
                ,LE_Miami_Importance_Contacts__c
                ,PURE_Agent__c          
                ,LE_Miami_agent__c
                ,WAA_agent__c
            From
                Contact
            Where
                Id = :con.Id];

        System.assertEquals(acc.PURE_Importance__c,result.PURE_Importance_Contact__c);
        System.assertEquals(acc.WAA_Importance__c,result.We_Are_Africa_Importance_contact__c);
        System.assertEquals(acc.LE_Miami_Importance__c,result.LE_Miami_Importance_contacts__c);
        
        System.assertEquals(acc.PURE_Agent__c,result.PURE_Agent__c);
        System.assertEquals(acc.WAA_agent__c,result.WAA_Agent__c);
        System.assertEquals(acc.LE_Miami_agent__c,result.LE_Miami_agent__c);
        
    }
}
Best Answer chosen by Lakis
pconpcon
You cannot have apex code "ignore" validation rules.  You will need to ensure that your data meets the validation rules or you will need to remove them.

All Answers

pconpcon
To increase your testing you will need to add more tests that cover all of the facets of your trigger [1].  As for error emails, the Apex emails will go to the last user that edited the class.  So in the case of production, it would be whomever pushed the class last.

[1] http://blog.deadlypenguin.com/blog/testing/strategies/
LakisLakis
Thanks Patrick. I also have two validation rules on Contact object. Would you be able to help to update the trigger to ignore the validation?
pconpcon
You cannot have apex code "ignore" validation rules.  You will need to ensure that your data meets the validation rules or you will need to remove them.
This was selected as the best answer