0

I have created one trigger which checking if account owner and associated contact owner is same or not.Its giving me output correct.but when i am updating account using dataloader then its looping in contact loop many time. Suppose i have updated 4 accounts and each account having 2 contact then in my trigger for each account -8 times contact loop is running.

trigger TimepassTrigger on Account (before insert,before update,before delete,after insert,after update) {
    If(trigger.isbefore  && (trigger.isinsert || trigger.isupdate)){
        Map<id,account> accountMap=new map<id,account>();
        for(account acc:trigger.new){
            accountmap.put(acc.id,acc);
        }
        List<contact> conlist=new list<contact>([select id,AccountId,ownerid,lastname from contact where accountid in:accountmap.keySet()]);
        for(account ac:accountmap.values()){
            for(contact con:conlist){
                if(accountmap.containsKey(con.AccountId) && con.OwnerId==accountmap.get(ac.id).ownerid){          
                    con.Description='Yes the Owner of the account and contact are same';
                }
                else
                    con.Description='No the Owner of the account and contact are not same';
            }
        }
        update conlist;
    }
}

Please suggest. See logs

enter image description here

0

First, you only need to do this after update or after undelete. There will never be a contact available in an insert trigger, as it is a child record, and no child can exist before its parent.

if(Trigger.isAfter && (Trigger.isUpdate || Trigger.isUndelete)) {

Second, what you really want is a map that holds lists of contacts:

Map<Id, List<Contact>> contacts = new Map<Id, List<Contact>>();
Contact[] updates = new Contact[0];
for(Account record: Trigger.new) {
    contacts.put(record.Id, new Contact[0]);
}
for(Contact record: [SELECT AccountId, OwnerId FROM Contact WHERE AccountId = :Trigger.new]) {
    contacts.get(record.AccountId).add(record);
    updates.add(record);
}
for(Account accRecord: Trigger.new) {
    for(Contact conRecord: contacts.get(accRecord.Id)) {
        if(conRecord.OwnerId==accRecord.OwnerId){          
            conRecord.Description='Yes the Owner of the account and contact are same';
        } else {
            con.Description='No the Owner of the account and contact are not same';
        }
    }
}
update updates;
  • Thanks for very quick response.Its working perfectly..Can you please explain me Contact[] updates = new Contact[0]; why we took 0 here. – Aditi May 26 '17 at 4:44
  • Please also let me know where i can find the trigger examples to learn the trigger in advance.thanks – Aditi May 26 '17 at 4:50
  • @sam new Contact[0] is the same as new List<Contact>(), but less typing. It says "create a new List of Contact type with 0 elements". You'll find examples of triggers like this here on SFSE and all over the web. Try going to the Trailhead to learn more. – sfdcfox May 26 '17 at 12:32

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.