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
vinni.focevinni.foce 

how to avoid soql and dml

how to avoid soql and dml in this trigger....

 

  1. trigger TriggerWithEntryCriteria on Account (after update)   
  2.     {  
  3.         Integer index = 0;         
  4.         for(Account acc : Trigger.New)  
  5.             {  
  6.               
  7.                 if(acc.Fax != Trigger.Old[index].Fax)                  
  8.                     {  
  9.                        
  10.                          List<Contact> listCon = [Select Fax from Contact where AccountId =: acc.id];        
  11.                          for(Contact con : listCon)  
  12.                              {  
  13.                                  con.Fax = acc.Fax;  
  14.                              }  
  15.                          update listCon;      
  16.                     }  
  17.                 index++;  
  18.             }  
  19.     }
vishal@forcevishal@force

You can avoid the dml and soql here by using the standard Trigger.oldMap and Trigger.newMap

 

Try the below code:

 

  1. trigger TriggerWithEntryCriteria on Account (after update)   
  2.     {  
  3.             Set<Id> setUpdatedIds = new Set<Id>(); // 1
  4.             List<Contact> lstContactsToBeUpdated = new List<Contact>(); // 2
  5.             for(Account acc : Trigger.New)  
  6.             {  
  7.                if(acc.Fax != Trigger.OldMap.get(acc.Id).Fax)       // 3          
  8.                     {  
  9.                         setUpdatedIds.add(acc.Id);
  10.                     }  
  11.              }
  12.              for(Contact c : [Select AccountId, Fax From Contact Where AccountID IN : setUpdatedIds]) // 4
  13.              {
  14.                   c.Fax = Trigger.newMap.get(c.AccountId).Fax;
  15.                   lstContactsToBeUpdated.add(c);
  16.              }
  17.             
  18.              if(lstContactsToBeUpdated.size() > 0) // 5
  19.                   update lstContactsToBeUpdated;
  20.  
  21.     }

Here,

1 = new set to store those Account Id's whose Fax has been changed.

2 = new list to store contacts that are to be updated (this is to avoid updating each Contact inside the for loop)

3 = checking if the account's fax has been changed. If yes, store it's id in the new set.

4 = Querying all Contacts whose Account's fax has been changed. Assigning the new fax number to each of the associated Contacts

5 = updating the Contacts.

 

Let me know if you have any doubts.

Yoganand GadekarYoganand Gadekar

You are trying to update contacts on account update.

I think u cannot avoid both of them(SOQl and DML).

As u will definately require soql to retrieve contacts,there's no other way.

Also u want to updat those contacts and DML will be must.

Even workflow cannot meet your requirement.

 

Mark this as answer if this helps you.

 

_Yoganand Gadekar.