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
Sia Thripio 6055Sia Thripio 6055 

Write a trigger to update a field(let it be city) in all related opportunities, when same field(city) is update in account.

this code is not reflecting my UI . why?
trigger citytrigger on Account (after update) {
    
   List< Opportunity> OppListToUpdate=new  List< Opportunity > ();
    map<id,account> Accmap=new map<id,account>();
    for(Account a:trigger.new){
        if(a.accCity__c !=trigger.oldmap.get(a.id).accCity__c)
            Accmap.put(a.id,a);
    }
    
    if(!Accmap.isEmpty()){
        Set<id> AccIDs=Accmap.keyset();
         List< Opportunity>  Opplist=[Select AccountId,city__c,Account.accCity__c  from Opportunity where AccountId  in :AccIDs];
        
        if(Opplist.size()>0){
            for(Opportunity o: Opplist ){
                o.city__c=Accmap.get(o.accountId).accCity__c;
                OppListToUpdate.add(o);
            }
        }
    }
    
    if(OppListToUpdate.size()>0){
        update OppListToUpdate;
    }
}
SwethaSwetha (Salesforce Developers) 
HI Sia,
By not reflecting in UI, are you seeing any errors?

Similar: https://developer.salesforce.com/forums/?id=9060G0000005dwtQAA

https:/salesforce.stackexchange.com/questions/317923/trigger-to-update-field-in-opportunity-when-account-is-created-with-same-value-a

https://developer.salesforce.com/forums/?id=9062I000000QvzjQAC
Naveen KNNaveen KN
your code looks good. Maybe you can add system debug points to see the list size of OppListToUpdate and also before updating the opportunity level field, check what value account field has.