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
phanip adminphanip admin 

I have trigger seneccario i create contact automatically account will be created but i want update the contact details it is not update account record

Hi Everyone?

I have trigger scenario i create contact automatically account will be created but i want update the contact details it is not update account record?

i write a code pls check this code? (insert work fine but update is not working ple help me as well as give me code also delete)


trigger acct on Contact (after insert,after update) {
    list<account>acct=new List<account>();
    Map<string,account> accountmap=new Map<string,account>();
    if(trigger.isafter){
        if(trigger.isinsert){
            for(contact con:trigger.new){
                account acc=new account();
                acc.Name=con.LastName;
                acc.id=con.AccountId;
                acc.Phone=con.Phone;
                acct.add(acc);
            } 
            insert acct;
        }
        if(trigger.isafter && trigger.isupdate){
            for(account acc:[select id from account WHERE id IN:trigger.newMap.keySet()]){
                accountmap.put(acc.Id, acc);               
            }
            for(contact con:trigger.new){
                account acc=new account();
                if(accountmap.containsKey(acc.Id))
                   acc.Id=accountmap.get(acc.Id).id;
                   acc.Phone=con.Phone;
                   acc.Name=con.LastName;
                    acc.Id=con.AccountId;
            }
        }
        if(acct.size()>0)
        update acct;
  }  
}



 
Best Answer chosen by phanip admin
Ashish DevAshish Dev
You should not write code in trigger but in handler class, but for now I am correcting your 'update' trigger code.
 
if(trigger.isafter && trigger.isupdate){
    Map<id,id> conAccMap = new Map<id, id>();
    
    for(contact con: trigger.new){
        conAccMap.put(con.Id, con.AccountId);
    }
    for(account acc:[select id from account WHERE id IN :conAccMap.values()]){
        accountmap.put(acc.Id, acc);               
    }
    for(contact con:trigger.new){
        account acc=new account();
        if(accountmap.containsKey(conAccMap.get(con.Id)))
            acc = accountmap.get(conAccMap.get(con.Id));
        acc.Phone=con.Phone;
        acc.Name=con.LastName;
        acc.Id=con.AccountId;
    }
}

Let me know if this solves your problem.

All Answers

Raj VakatiRaj Vakati
Hi Phanip , 

Please use this code  and take care of couple of thinks 
  • Make it as bulkify code 
  • Null values check 
  • error handling.


trigger acct on Contact (after insert,after update) {
    list<Account> acct=new List<Account>();
    Map<string,account> accountmap=new Map<string,account>();
    Map<Id,account> accountmapTemp=new Map<Id,account>();
    
    
    if(trigger.isafter){
        if(trigger.isinsert){
            for(Contact con:trigger.new){
                Account acc=new Account();
                acc.Name=con.LastName;
                acc.Phone=con.Phone;
                // acct.add(acc);
                insert acc;
                // 
                Contact c =[Select Id from Contact where id=:con.id];
                c.AccountId = acc.Id ;
                update c ;
                
                // accountmapTemp.put(con.Id, acc);
            } 
        }
        
        //insert accountmapTemp.values(); 
        
        
        
        if(trigger.isafter && trigger.isupdate){
            // Set<Id> accIds = new Set<Id>();
            Map<Id,Contact> accIdMap = new Map<Id,Contact>();
            
            for(contact con:trigger.new){
                //   accIds.add(con.AccountId) ;
                accIdMap.put(con.AccountId, con);
            }
            List<account> acc = [select id from account WHERE id IN:accIdMap.keySet()];
            for(Account atemp:acc){
                Contact c =accIdMap.get(atemp.Id);  
                atemp.Phone=c.Phone;
                atemp.Name=c.LastName;
                // acc.Id=con.AccountId;
                
            }
            update acc ; 
            
        }
    }
    
}






 
Ashish DevAshish Dev
You should not write code in trigger but in handler class, but for now I am correcting your 'update' trigger code.
 
if(trigger.isafter && trigger.isupdate){
    Map<id,id> conAccMap = new Map<id, id>();
    
    for(contact con: trigger.new){
        conAccMap.put(con.Id, con.AccountId);
    }
    for(account acc:[select id from account WHERE id IN :conAccMap.values()]){
        accountmap.put(acc.Id, acc);               
    }
    for(contact con:trigger.new){
        account acc=new account();
        if(accountmap.containsKey(conAccMap.get(con.Id)))
            acc = accountmap.get(conAccMap.get(con.Id));
        acc.Phone=con.Phone;
        acc.Name=con.LastName;
        acc.Id=con.AccountId;
    }
}

Let me know if this solves your problem.
This was selected as the best answer