+ Start a Discussion
frasuyetfrasuyet 

Contract Trigger - How to Bulkify

The trigger below is working as it should, two field values from the contract are updating the reciporating fields on the related account record. The trigger is not bulfied and I am trying to figure the struture so it could support bulk updates to the contract object. 

 

Any comments on how this trigger could be bulkified?  Thanks in advance for the guidence.

 

 

trigger UpdateAccountTxnsArc1 on Contract (after update) {
    public Id aId;
    public Id cId;
    for(Contract c : Trigger.New){
        cId = c.Id;
        if(c.Air_Transactions__c != Null 
           && c.Est_Annual_Air_Spend__c != Null 
           && c.AccountId != Null 
           && c.Status == 'Active'
           && c.RecordTypeId == '012600000000zk') { 
            aId = c.AccountId;
        }
    }
    if(aId!=Null){
        Account acct = [Select Contracted_Air_Transactions_Acct__c From Account Where Id = :aId];
        for(Contract con : Trigger.New){
           acct.Contracted_Air_Transactions_Acct__c = con.Air_Transactions__c;
           acct.ARC__c = con.Est_Annual_Air_Spend__c;
        }
    update acct;    
    } 
}

 

 

 

 

 

gm_sfdc_powerdegm_sfdc_powerde

1) In the first for loop, you should collect the accounts in a list.  You probably need a map since you need to track the contract associated with each account.

2) Your query to fetch accounts should use an IN clause so you fetch all accounts in the map rather than a single account.

3) The second "for" loop could be for the map entries and for each map entry, you could update the corresponding account record with the appropriate information from contract.

4) Update all account records in one shot.

 

Hope this helps!

 

Rahul S.ax961Rahul S.ax961

Hello frasuyet,

I got the requirement, had optimized the code for you.
you can still bulkify it furthur using a batch class, if you are having large data.
It seems there will be a problem if we update Multiple Contracts of same Account, At that time which contract needs to be considered?

Trigger:
------------------------------------------------------------------------------------------------------------------------------------------------------

trigger UpdateAccountTxnsArc1 on Contract (after update)
{
    List<Account> lstAccount =new List<Account>();
    for(Account a : [Select ARC__c, Contracted_Air_Transactions_Acct__c,
                            (select id, name, Air_Transactions__c, Est_Annual_Air_Spend__c from contract
                                where id in: Trigger.new and Air_Transactions__c != Null and
                                AccountId != Null and Status = 'Active' and RecordTypeId = '012600000000zk')
                            From Account)
    {
        if(!a.Contracts.isEmpty())
        {
            lstAccount.add(
                        Account acct = new Account(Contracted_Air_Transactions_Acct__c = a.Contracts[0].Air_Transactions__c,
                        ARC__c = a.Contracts[0].Est_Annual_Air_Spend__c)
                        );
        }
    }
    if(!lstAccount.isEmpty)
        update lstAccount;    
}
------------------------------------------------------------------------------------------------------------------------------------------------------
Hope it helps,

frasuyetfrasuyet

@Rahul

 

When compiling your code I get the follow message: 

 

"Error: Compile Error: Didn't understand relationship 'Contract' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 4 column 21"

 

I paired down the query to a simple example and got the same message when running in the IDE salesforce.schema

Select Arc__c,
  (select id  From Contract where Air_Transactions__c != Null)
From Account

 Any thoughts of why I might be receiving this error?

 

Rahul S.ax961Rahul S.ax961

Hi frasuyet,

 

Try this:

 

 

Select Arc__c,
(select id From Contracts where Air_Transactions__c != Null)
From Account

 

Let me know if u face furthur issues.