You need to sign in to do that
Don't have an account?

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;
}
}
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;
}
}
Let me know if this solves your problem.
All Answers
Please use this code and take care of couple of thinks
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 ;
}
}
}
Let me know if this solves your problem.