1

'm trying to update fields in child and grandchildren from parent object. Everything seems good but while testin I'm hitting Apex CPU time limit exceeded error. my code structure is like below which I think causing the apex limit exceeded. Please anyone help structuring it properly

for (Parent__c p : parents) {
    // Squared - problem
    for (Child__c c : children) {
        // Cubed - big problem
        for (GrandChild__c bc : grandChildren) {
            // ...
        }
    }
}

My actual code is below

trigger UpdateAccount on Contract__c (before update) {
Contract__c[] contract = Trigger.new;
try
{
    if(contract[0].Id != null)
    {
        if(contract[0].Total_Applicable_Accounts__c > 0)
        {
                Set<Id> accountNames = new Set<Id>(); 
                List<Applicable_Accounts__c> ApplicableAccounts = [Select Id,Account_Name__c from Applicable_Accounts__c where Contract__c =: contract[0].Id ];
                System.debug(ApplicableAccounts.size());
                if(ApplicableAccounts != null && ApplicableAccounts.size() > 0)
                {
                    for(Applicable_Accounts__c appaccount : ApplicableAccounts) 
                    { 
                        if( appaccount.Account_Name__c != null)
                        {
                            accountNames.add(appaccount.Account_Name__c);
                        }
                    }
               }
                    List<Account> accounts = [select Name from Account where Id IN: accountNames]; 
                    List<Opportunity> ops = [Select Id, Name, AccountId From Opportunity WHERE AccountId IN : accountNames];
                     System.debug(accounts.size());
                    if(accounts!=null && accounts.size() > 0)
                    {   List<Account> updatedacc = new List<Account>();
                        List<Opportunity> updateop = new List<Opportunity>();
                        for(Account objaccount : accounts)
                        {
                            if(contract[0].Active__c != null)
                            {
                                objaccount.Active_Hidden__c = contract[0].Active__c;        
                                if(contract[0].Active__c == 'Yes')
                                {
                                   objaccount.Contract_Hidden__c = contract[0].Id; 
                                   objaccount.Contract_Details_Hidden__c = contract[0].Contract_Details__c;                           
                                   objaccount.Special_Quoting_Instructions_Hidden__c = contract[0].Special_Quoting_Instructions__c;                          
                                   objaccount.Special_Report_Links_Hidden__c = contract[0].Special_Report_Links__c;                          
                                   objaccount.Contract_Manager_Hidden__c = contract[0].Contract_Manager__c;                            
                                   objaccount.Pricebook_Hidden__c = contract[0].Pricebook__c;                    
                                }
                                else
                                {
                                     objaccount.Contract_Hidden__c = null;
                                     objaccount.Contract_Manager_Hidden__c = null;
                                     objaccount.Contract_Details_Hidden__c =null;
                                     objaccount.Special_Quoting_Instructions_Hidden__c=null;
                                     objaccount.Special_Report_Links_Hidden__c=null;
                                     objaccount.Pricebook_Hidden__c = null;

                                }  
                            updatedacc.add(objaccount);                             
                            //update objaccount;
                          }  
                        } 
                        update updatedacc;

                        for(Opportunity objOpportunity : ops)
                        {
                             if(contract[0].Total_Applicable_Accounts__c != null)
                             {
                                 objOpportunity.Special_Quoting_Instructions2__c = accounts[0].Special_Quoting_Instructions_Hidden__c;
                              }
                              else
                              {
                              objOpportunity.Special_Quoting_Instructions2__c = null;
                              }
                        updateop.add(objOpportunity);
                       }
                       update updateop;
                    }                   
            }
  • oof yeah I'm not surprised, you should be using Maps, not Lists – EricSSH Jul 19 '16 at 19:20
  • @EricSSH ... I'm just beginner ... Not familiar with Maps topics yet .. could you please help me out ... please!!!! – Gidy Jul 19 '16 at 19:29
3

Disclaimer, this is pretty rough and I eyeballed it. Some work will still need to be done. But List, Sets and Maps is one of thee most important things you can learn Google around but this is a good one to learn from http://blog.jeffdouglas.com/2011/01/06/fun-with-salesforce-collections/, also if those values really need to be null you will need to move the Active == Yes out of the qualifier

trigger UpdateAccount on Contract__c (before update) 
{

    Map<ID, Contract__c> contractMap = new Map<ID, Contract__c>();
    List<Account> updatedacc = new List<Account>();
    List<Opportunity> updateop = new List<Opportunity>();
    Set<Id> acctIDS = new Set<Id>(); 

    for(Contract__c c: Trigger.new)
    {
        if(c.Total_Applicable_Accounts__c > 0 && c.Active__c == 'Yes')
        {
            contractMap.put(c.ID, c);
        }     
    }

    for(Applicable_Accounts__c appAccts :  [Select Id, Account_Name__c from Applicable_Accounts__c where Contract__c =: contractMap.keySet() AND Account_Name__c != null])
    {
        acctIDS.add(appaccount.Account_Name__c);
    }

    if(!acctIDS.IsEmpty())
    {
        Map<ID, Account> accountMap = new Map<ID, Account>([select Name from Account where Id IN: acctIDS]); 
        Map<ID, Opportunity> opMap = new Map<ID, Opportunity>([Select Id, Name, AccountId From Opportunity WHERE AccountId IN : accountNames]);

        for(ID aid : accountMap.keySet())
        {
            if(contractMap.containsKey(accountMap.get(aid).ID).Account_Name__c)
            {
                accountMap.get(aid).Contract_Hidden__c = contractMap.get(accountMap.get(aid).ID).Account_Name__c; 
                accountMap.get(aid).Contract_Details_Hidden__c = contractMap.get(accountMap.get(aid).ID).Contract_Details__c;                           
                accountMap.get(aid).Special_Quoting_Instructions_Hidden__c = contractMap.get(accountMap.get(aid).ID).Special_Quoting_Instructions__c;                          
                accountMap.get(aid).Special_Report_Links_Hidden__c = contractMap.get(accountMap.get(aid).ID).Special_Report_Links__c;                          
                accountMap.get(aid).Contract_Manager_Hidden__c = contractMap.get(accountMap.get(aid).ID).Contract_Manager__c;                            
                accountMap.get(aid).Pricebook_Hidden__c = contractMap.get(accountMap.get(aid).ID).Pricebook__c;   
                updatedAcc.add(accountMap.get(aid));   
            }    
        }
    }

    update updatedacc;
    //Use your account map and Ops map to do the same    
    /*    for(Opportunity objOpportunity : ops)
    {
        if(contract[0].Total_Applicable_Accounts__c != null)
        {
            objOpportunity.Special_Quoting_Instructions2__c = accounts[0].Special_Quoting_Instructions_Hidden__c;
        }
        else
        {
            objOpportunity.Special_Quoting_Instructions2__c = null;
        }
            updateop.add(objOpportunity);
       }
       update updateop;
    }*/                   
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.