You need to sign in to do that
Don't have an account?
vinni.foce
how to avoid soql and dml
how to avoid soql and dml in this trigger....
- trigger TriggerWithEntryCriteria on Account (after update)
- {
- Integer index = 0;
- for(Account acc : Trigger.New)
- {
-
- if(acc.Fax != Trigger.Old[index].Fax)
- {
-
- List<Contact> listCon = [Select Fax from Contact where AccountId =: acc.id];
- for(Contact con : listCon)
- {
- con.Fax = acc.Fax;
- }
- update listCon;
- }
- index++;
- }
- }
You can avoid the dml and soql here by using the standard Trigger.oldMap and Trigger.newMap
Try the below code:
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.
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.