0

How to mass-update all other contact records on the same account When a single contact record updated?

Trigger code:

trigger syncContact on Contact (after update) {
    map<id,string> conMap = new map<id,string>();
    set<id> accId = new set<Id>();
    for(Contact c : Trigger.new)
    {
        conMap.put(c.Id,c.Title);
        accId.add(c.AccountId);
    }
    List<contact> conList = new List<contact>();
    if(recursiveCheck.runOnce() && trigger.isupdate)
    {
        for(account a : [select id,(select id,title from contacts where id not in : conMap.keySet() ) from account where id in : accId])
        {
            conList.add(a.contacts);
            for(contact c : a.contacts)
            {
             c.Title=conMap.get(id).Title;   
                conlist.add(c);
            }
        }
    }
update conlist;
}
  • you are trying to create a map of contactId vs title. You will not be able to get the title from this map in you for loop as you are excluding the contact in your query that you have put on your map. – Ajay Prakash Dubey Nov 26 '17 at 18:17
1
    trigger syncContact on Contact (after update) {
    map<id,string> conMap = new map<id,string>();
    set<id> accId = new set<Id>();
    for(Contact c : Trigger.new){
        conMap.put(c.AccountId,c.Title);
        accId.add(c.id);
    }
    List<contact> conList = new List<contact>();
    if(recursiveCheck.runOnce() && trigger.isupdate){
        for(account a : [select id,(select id,AccountId,title from contacts where id not in : accId ) from account where id in : conMap.keySet()]){
            for(contact c : a.contacts){
                c.Title=conMap.get(c.AccountId);   
                conlist.add(c);
            }
        }
    }
    update conlist;
}

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.