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
narensfdcnarensfdc 

Trigger on account for after update event

Hi,

      

     I have a scenario with trigger..........    like when i am updating billing address fields in account  the same should be 

     reflected in contact object fields.

 

     I used after trigger event  but i am getting an error .

 

     help me out in this senario.....

 

thanks

Jeff MayJeff May

What error are you getting?

raseshtcsraseshtcs

Please post the code and the error for better understanding of the error

narensfdcnarensfdc

this is the code,its working while i am inserting a new record

 

the requirement is when i am updating account fields, that should get reflected the same in contact as well

---------------------------------------------------------------------------------

trigger insertcononacctrg on Account (after insert) {
 
     account acc = trigger.new[0];
       
     contact con = new contact();
 
     con.lastname = acc.name;
     con.mobilephone = acc.phone;
     con.accountid = acc.id
 
     insert con;
}
narensfdcnarensfdc
this is the code,its working while i am inserting a new record

the requirement is when i am updating account fields, that should get reflected the same in contact as well
---------------------------------------------------------------------------------
trigger insertcononacctrg on Account (after insert) {

account acc = trigger.new[0];

contact con = new contact();

con.lastname = acc.name;
con.mobilephone = acc.phone;
con.accountid = acc.id;

insert con;
}
Jeff MayJeff May

what error are you getting?

narensfdcnarensfdc
the requirement is when we update a record in account,the trigger should fire
narensfdcnarensfdc

i worked on that trigger ..... this trigger is firing for update operation.....

 

trigger insertcononacctrg1 on Account (after update) {

account a=trigger.new[0];
list<contact> conlst;
conlst=[select id,name from contact where AccountId=:a.Id limit 10];
if(conlst.size()>0){
for(contact c: conlst){
c.lastname=a.name;
c.mobilephone = a.phone;
c.accountid = a.id;
update c;
}

}
}

 

thanks

Naidu PothiniNaidu Pothini
trigger insertcononacctrg1 on Account (after update)
{
List<Contact> conList = new List<Contact>();
for(Contact con : [SELECT Id, Name, MobilePhone FROM Contact WHERE AccountId IN :Trigger.newMap.keySet()]) { Account acc = Trigger.newMap.get(con.AccountId); con.LastName = acc.Name; con.MobilePhone = acc.Phone; conList.add(con); } update conList; }

 try this.