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
Jacob Elliott 1Jacob Elliott 1 

Trigger to populate a field on contact when account is created. It's not working but not getting errors

I created a trigger to update all related Contacts when an Account is updated. It was working perfectly and then I was asked to turn it into a regular class so that it can be called by an existing trigger...now it's not working. It looks like the code is being executed, but I'm not getting an error...Any thoughts?

Trigger that calls my class below (seems to be working fine):
/**
 * Class AccountHandler
 *
 * Trigger Handler for the Account SObject. This class implements the ITrigger
 * interface to help ensure the trigger code is bulkified and all in one place.
 */
public with sharing class CCIAccountHandler implements CCITriggerBase {

    // Constructor
    public CCIAccountHandler() {
    }

    public void bulkGeneral() {}
    /**
     * bulkBefore
     *
     * This method is called prior to execution of a BEFORE trigger. Use this to cache
     * any data required into maps prior execution of the trigger.
     */
    public void bulkBefore() {}

    public void bulkAfter() {}

    public void beforeInsert(SObject so) {}

    public void beforeUpdate(SObject oldSo, SObject so) {
        Account oldAcct = (Account) oldSo;
        Account acct = (Account) so;
        if(acct.V12C__Market__c != null && acct.V12C__Market__c.equals('MHFN')) {
            //SI-2: Send Email to the Account Owner and Quality Specialist
            CCISendEmail.sendDataIntegrationEmail(oldAcct, acct);
        }
        
    }

    /**
     * beforeDelete
     *
     * This method is called iteratively for each record to be deleted during a BEFORE
     * trigger.
     */
    public void beforeDelete(SObject so) {}

    public void afterInsert(SObject so) {
        Account acct = (Account) so;
    }

    public void afterUpdate(SObject oldSo, SObject so) {
    Account oldAcct = (Account) oldSo;
    Account acct = (Account) so;
    CCIProviderChangeAccountTracking.resaveContact(oldAcct,acct);

    }

    public void afterDelete(SObject so) {}

    /**
     * andFinally
     *
     * This method is called once all records have been processed by the trigger. Use this
     * method to accomplish any final operations such as creation or updates of other records.
     */
    public void andFinally() {}

}

My class that will run when certain fields on account is changed and will populate a datetime on contact called last changed:
public with sharing class CCIProviderChangeAccountTracking {

    public static void resaveContact(Account oldAcct, Account newAcct) {
         
        if (oldAcct.Phone != newAcct.Phone ||
                oldAcct.ShippingAddress != newAcct.ShippingAddress ||
                oldAcct.V12C__PR_Practice_Tax_ID_Number__c != newAcct.V12C__PR_Practice_Tax_ID_Number__c ||
                oldAcct.BillingAddress != newAcct.BillingAddress) {
            
            List<Contact> toSave = new List <Contact>();
            for (Contact con : [SELECT Id, Scorecard_Modified_Date__c FROM Contact WHERE Primary_Practicing_Location_Lookup__r.ParentId =:newAcct.Id]) {
                con.Last_Changed_Date__c = Datetime.NOW();
                toSave.add(con);    
            }
            system.debug('-------------' + toSave.size());
            
            try {
            update toSave;}
            catch(exception e){system.debug(e.getMessage());}
        }
    }
}

In the debug log I can see where it calls the class but isn't showing the system.debug
Best Answer chosen by Jacob Elliott 1
Rohit Kumar SainiRohit Kumar Saini
Hi Jacob,

Couple of things to look for:
  1. Your class is 'with sharing' so sharing rules are applicable. Can you please make sure user updating account has access to contacts. You can update class to 'Without sharing' and see if it changes anything?
  2. Can you please check if one of the multiple account conditions are being met. You can put a debug log at line #9 and see if we are getting there in debug logs.
  3. Debug logs also show ROWS for query results. You can check if the contact query is returning any results or not. 
Your code doesn't have any issues so you will have to debug why your code is not reaching to contact update statement. There can be multiple reasons for that as I listed above. Thanks.