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
prasanth kumarprasanth kumar 

update account description when contact homephone is inserted or changed , not wokring please help

 
trigger updateacc on contact(after insert,after update)
{
map<id,contact> mymap=new map<id,contact>();

for(contact c:trigger.new)
{
mymap.put(c.id,c);
}

list<account> acc=[select id,name,description from account where id in:mymap.keyset()];

for(account a1:acc)
{
a1.description=mymap.get(a1.id).HomePhone;
update acc;
}
}

 
Dutta SouravDutta Sourav
Hi Prasanth,
Try with this code:
trigger updateacc on contact(after insert,after update)
{
    map<id,contact> mymap=new map<id,contact>();

    for(contact c:trigger.new)
    {
    mymap.put(c.Accountid,c);
    }

 list<account> acc=[select id,name,description from account where id in:mymap.keyset()];

for(account a1:acc)
{
a1.description=mymap.get(a1.id).HomePhone;

}
update acc;
}

Warm Regards,
Sourav
Arun KumarArun Kumar
Hi Prasanth,

Try with this bulkified Trigger code
trigger updateacc on contact(after insert,after update)
{
map<id,contact> mymap=new map<id,contact>();
List<Account> accListToUpdate = new List<Account>();

for(contact c:trigger.new)
{
mymap.put(c.id,c);
}

list<account> acc=[select id,name,description from account where id in:mymap.keyset()];

for(account a1:acc)
{
a1.description=mymap.get(a1.id).HomePhone;
update acc;
}
}


trigger updateacc on contact(after insert,after update)
{
    map<id,contact> mymap=new map<id,contact>();

    for(contact c:trigger.new)
    {
    mymap.put(c.Accountid,c);
    }

 list<account> acc=[select id,name,description from account where id in:mymap.keyset()];

for(account a1:acc)
{
a1.description=mymap.get(a1.id).HomePhone;
accListToUpdate.add(a1);
}
if(!accListToUpdate.isEmpty()) update accListToUpdate;
}

Please let me know if the helps.


As a common practice, if your question is answered, please choose 1 best answer.
Additionaly you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Arun