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
KCLKCL 

help in writing a trigger .

I parent Object and child object 

Parent: state
Parent Field: Status(picklist Field): values are:(IN,OUT)
Child :city
Child Field: Yes(Check Box)

Need help in writing a trigger : 

Scenerio:When ever picklist values are changed ex: IN to OUt  or OUT to IN The trigger need to Check the YES filed and immediately Uncheck.

 
Ajay K DubediAjay K Dubedi
Hi,

Use below code it may helpful for you.

Trigger
trigger TriggerOnState on State__c (after update) {
    if(trigger.isAfter && trigger.isUpdate){
        TriggerOnStateApex.triggerOnStateApexMethod(trigger.old, trigger.old);
    }
      
}

Apex Controller
public class TriggerOnStateApex {
    public static void triggerOnStateApexMethod(List<State__c> newStateList, List<State__c> oldStateList) {
        try{
            if(newStateList.size()>0){
                system.debug('newStateList--->'+newStateList);
                Set<Id> stateIdSet = new Set<Id>();
                for(State__c oldstateinst : oldStateList) {
                    for(State__c newStateInst : newStateList) {
                        if(oldstateinst.Id == newStateList.Id){
                            if(oldstateinst.Status__c != null &&  newStateInst.Status__c != null){
                                if(oldstateinst.Status__c != newStateInst.Status__c){
                                    stateIdSet.add(newStateInst.Id);
                                }
                            }
                        }
                    }
                }
                if(!stateIdSet.isEmpty()){
                    List<City__c> cityList = new List<City__c>();
                    cityList = [SELECT Name, Yes__c, State__c FROM City__c WHERE State__c IN : stateIdSet];
                    
                    for (City__c cityInst : cityList)
                        if(cityInst.Yes__c == true){
                            cityInst.Yes__c == fales;
                        }
                        else{
                            cityInst.Yes__c == true;
                        }
                    }
                }
                update cityList;
            }
        }catch(Exception e){
            system.debug('Exception:'+e.getMessage()+' '+e.getLineNumber());
        }
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
Deepali KulshresthaDeepali Kulshrestha
Hi,

Use the following code. It may helpful for you
Trigger

trigger UpdateCityField on State__c (after update) {
  if(trigger.isAfter){
        if(trigger.isUpdate){
           ToUpadateCity.toUpdateCityOnUpdatingState(Trigger.New);
        }   
        
    }
}

Apex Controller

public class ToUpadateCity {
    
    public static void toUpdateCityOnUpdatingState(List<State__c> stateList){
        try{
            Set<Id> stateIds = new Set<Id>(); 
            List<State__c> newStateList = new List<State__c>();    
            List<City__c> cityToUpdate = new List<City__c>(); 
            
            for(State__c stateObject : stateList){
                State__c oldState = (State__c)Trigger.oldMap.get(stateObject.Id);   
                
                if(stateObject.Status__c != oldState.Status__c){
                    stateIds.add(stateObject.Id);  
                }
            }
            
            if(stateIds.size()>0){
                newStateList = [SELECT Id, Status__c, (SELECT Id, YES__C FROM Cities__r) FROM State__c WHERE Id IN: stateIds]; 
                
                for(State__c stateObject: newStateList){
                    for(City__c cityObject: stateObject.Cities__r){
                        
                        if(stateObject.Status__c == 'OUT'){
                            cityObject.Yes__c = true;     
                        }else{
                            cityObject.Yes__c = false;        
                        }
                        cityToUpdate.add(cityObject);
                    }
                }      
                if(cityToUpdate.size()>0){      
                    Update cityToUpdate;
                }
            }
        }catch(Exception exp){
            System.debug('Exception in code'+ exp.getCause() + 'Exception in line number'+exp.getLineNumber());
        }
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com