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
gv rameshgv ramesh 

getting error while updating contact and account using trigger...

hi everyone...could u rectify the error.....
trigger conchangecity on Contact (after update) {
if(trigger.isafter){
if(trigger.isupdate){
set<id>conids=new set<id>();
list<account> acclst=new list<account>();
for(contact con:trigger.new){
if(con.mailingcity!=trigger.oldmap.get(con.accountid).mailingcity && con.mailingcity!=null)
conids.add(con.accountid);
}
contact con=new contact();
for(account acc:[select id,billingcity from account where id in:conids]){
acc.billingcity=trigger.newmap.get(con.accountid).mailingcity;
acclst.add(acc);
}
update acclst;
}
}

}

thanks inadvance...
Shrikant BagalShrikant Bagal
Hello [gv ramesh] gv ramesh,

What is Error?


 
SaranSaran
Hi ,

Below is the code which updates the accounts mailing city when the related contacts mailing city is updated.
 
trigger conchangecity on Contact (after update) {
    if(trigger.isafter && trigger.isUpdate){
        map<id, string> conValueMap =new map<id, string>();
        list<account> acclst=new list<account>();

        for(contact con:trigger.new){
            if(con.mailingcity!=trigger.oldmap.get(con.accountid).mailingcity && con.mailingcity!=null)
                accIds.put(con.accountid, con.mailingcity);
        }
        
        for(account acc:[select id,billingcity from account where id in:conids]){
            acc.billingcity = conValueMap.get(con.accountid);
            acclst.add(acc);
        }
        
        update acclst;
    }
}

Hope this might help you

Thanks
gv rameshgv ramesh
still raising error..... Variable does not exist: con.accountid
gv rameshgv ramesh
hi srikanth bagal...error like....:Apex trigger conchangecity caused an unexpected exception, contact your administrator: conchangecity: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object
SaranSaran
Hi,

Just replace the line no 7 as 
 
if(con.mailingcity!=trigger.oldmap.get(con.id).mailingcity && con.mailingcity!=null)

Hope this solves.

Thanks