1

Main Object : Contract Child Object1: Account Child Object2: Account's Opps

I'm trying to copy some field from Contract to Account, and one special field to both Account and Account's Opps [Special_Quoting_Instructions__c]. I could able to copy field values from Contract to Account but couldn't able to push that one special field to Account's Opps. I know cannot be done using formula field because it's a Long Text Area field and don't want to do workflow or process builder, as I want to push that field value to children upon changing the value at parent object. Below is the code that I've written. Again, I'm trying to push the Special_Quoting_Instructions__c [Contract] to Special_Quoting_Instructions_Hidden__c [Account] & Special_Quoting_Instructions__c [Account's Opp level].

    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]; 
                     System.debug(accounts.size());
                    if(accounts!=null && accounts.size() > 0)
                    {   List<Account> updatedacc = new List<Account>();
                        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;
                    }                   
            }


        List<Account> DeletedApplicableAccounts=[ Select Id,Name,Total_Applicable_Accounts_Hidden__c from Account where Contract_Hidden__c =:contract[0].Id and Total_Applicable_Accounts_Hidden__c=0 ];
         System.debug(DeletedApplicableAccounts.size());
          if(DeletedApplicableAccounts != null && DeletedApplicableAccounts.size() > 0)
            {
                List<Account> deletedacc = new List<Account>();
             for(Account deletedappaccount : DeletedApplicableAccounts)
               {
                if( deletedappaccount.Id != null && deletedappaccount.Total_Applicable_Accounts_Hidden__c == 0 )
                   {
                            deletedappaccount.Active_Hidden__c = 'No';
                            deletedappaccount.Contract_Hidden__c = null;
                            deletedappaccount.Contract_Manager_Hidden__c = null;
                            deletedappaccount.Contract_Details_Hidden__c =null;
                            deletedappaccount.Special_Quoting_Instructions_Hidden__c=null;
                            deletedappaccount.Special_Report_Links_Hidden__c=null;
                            deletedappaccount.Pricebook_Hidden__c = null;
                            deletedacc.add(deletedappaccount);
                           // update deletedappaccount;
                   }
                }
                update deletedacc;
            }
    }               


}
 catch(Exception ex)
    {
        System.debug('The following exception has occurred: ' + ex.getMessage());
        contract[0].addError('Exception has occured updating Account on update of Contract object');
    }   

}
1
  • @cropredy Could you please help me ... this is the actual scenario
    – Gidy
    Commented Jul 19, 2016 at 15:41

0

You must log in to answer this question.

Browse other questions tagged .