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
harsha vardhan vasa 9harsha vardhan vasa 9 

need help in trigger after update

Q) need to update parent record when child is updated. and need to update child when parent is updated. field city__C.
i have implemented after update which works fine in 2 individual scenarios. but when i combine the code im getting error.
code snippet:
trigger Child_update on Account (after update) {
    set<id> accid = new set<id>();
    list<account> acclist1 = new list<account>();
    list<account> upd = new list<account>();
     boolean vardhan;
    list<account> accid1 = new list<account>();
    set<id> acc =new set<id>();
    boolean harsha;
    for(account a:trigger.new){
        accid1 =[select id,name from account where id=:a.id and parentid =null ];
        if(accid1.size()>0){
            harsha =true;
        }
        else harsha=false;
        
        for(Account ab:accid1){
            acc.add(ab.id);
        }
    }
    if(harsha==true){
    list<account> acclist= [select id ,name,city__C,(select id,city__C from ChildAccounts) from account where id in :acc];
    list<account> updatelst = new list<account>();
    system.debug('list of records with child'+acclist);
    
        for(Account a :acclist){
            for(account abc:a.childaccounts){
                abc.City__c=a.City__c;
                updatelst.add(abc);
                system.debug('ayya vachindhi code');
            }
        }
        
        if(!updatelst.isEmpty()){
            update updatelst; 
        }   
 }

//second piece of code:
 if(harsha==false){
    for(account a:trigger.new){
      account[] acclst=[select id,name,parentid,city__c from account where id=:a.id and parentid !=null];
        for(account c:acclst){
        accid.add(c.ParentId);
        }
    }
    acclist1 =[select id,name,city__C from account where id =:accid];
   
    for(account r:trigger.new){
    for(account a:acclist1){
        system.debug('code vachindhi');
        a.City__c=r.city__C;
        upd.add(a);
    }
        }
        if(!upd.isempty()){
update upd;
        }
    }





help me if anyone had similar requirement
Matty-TMatty-T
What error are you getting? If you can supply the stack trace it would help, thanks!