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
K C 43K C 43 

write a apex code to update a feild(saycity) in account whenever a same feild(saycity) is updated on Order.

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you check the below trigger logic on Order object. Replace the billingcity field with your field.
 
trigger OrderTriggerCity on Order (after update) {
    Map<id,Order> ordmap= new Map<id,Order>();
    for(Order ord:Trigger.new){
        
        Order oldorder= Trigger.oldmap.get(ord.id);
        if(ord.BillingCity!=oldorder.BillingCity ){
            ordmap.put(ord.AccountId,ord);
        }
        
    }
    
    List<Account> acclist=[select id,BillingCity from Account where id in :ordmap.keySet() ];
    List<Account> tobeupdate= new List<Account>();
    For(Account acc:acclist){
        order ordervalue=ordmap.get(acc.id);
        acc.billingcity= ordervalue.BillingCity;
        tobeupdate.add(acc);
    }
update tobeupdate;
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
CharuDuttCharuDutt
Hii KC
Try Below Code
trigger OrderTriggerCity on Order (after Insert,after update) {

	if(trigger.IsAfter){
		if(trigger.IsInsert){
			for(Order ord:Trigger.new){   
				if(ord.AccountId!= null && ord.BillingCity!= null){
					ordmap.put(ord.AccountId,ord);
				}
        
			}
		}else if(trigger.IsUpdate){
		
			for(Order ord:Trigger.new){   
				if(ord.AccountId!= null && (ord.BillingCity!=Trigger.oldmap.get(ord.id).BillingCity || ord.AccountId != Trigger.oldmap.get(ord.id).AccountId)){
					ordmap.put(ord.AccountId,ord);
				}
        
			}
		}
	}
    
    
    List<Account> acclist=[select id,BillingCity from Account where id in :ordmap.keySet() ];
    For(Account acc:acclist){
        order ordervalue=ordmap.get(acc.id);
        acc.billingcity= ordervalue.BillingCity;
    }
    update acclist;
}
Please Mark It As Best Asnwer If It Helps
Thank You!