+ Start a Discussion
System Admin 949System Admin 949 

Picklists update using Trigger

Hi All,
I have two objects Account and Custom Object.Account is the parent and Custom object is child.Account has the picklist field called country and it has some  values like Ind,US,UK,Europe,..etc.And Custom object also has one more picklist called Transport,it has some values like By Road,By Air,By Sea..etc.
while the user selects the value in Account picklist field as "Ind",the value in child object picklist field as "By Road".If the value in Account has Other than "Ind",the value in child object picklist field as "By Air".I did this with ProcessBuilder and created seperate process for two conditions.
I include these two conditions with Process at the time of update record actions using formulas it won't work.which formula used in this??
How to write the trigger for this updating records.can any one provide sample code or any ideas.
thanks in advance.
 
NitishNitish
Trigger childPickListUpdate on Account(After update){
	if(Trigger.isUpdate && Trigger.isAfter){
		List<Contact> childList=new List<Contact>();
		List<Contact> childListWithInd=new List<Contact>(); 
		List<Contact> childListWithoutInd=new List<Contact>(); 
		List<Account> accList=[SELECT Id,Country,(SELECT Id,Transport__c FROM Contacts) FROM Account WHERE Id in :Trigger.new];
		
		for(Account acc:accList){
			childList.addAll(acc.contacts);
		}
		
		for(Contact con:childList){
			if(Trigger.newMap.get(con.accountId).Country == 'IND'){
				con.Transport__c='By Road';
				childListWithInd.add(con);
			}else{
				con.Transport__c='By Air';
				childListWithoutInd.add(con);
			}
		}
		
		if(childListWithInd.size()>0){
			update childListWithInd
		}
		if(childListWithoutInd.size()>0){
			update childListWithoutInd
		}
		
	}
}

 
sowmya Inturi 9sowmya Inturi 9
Hi,
You can do this with process builder. I tried and is working fine for me.
Please check the below screen shots:
Create a process builder on Account Object.
User-added image

Check for India.
User-added image
Update Transport in Child Object
User-added image

Similarly check for other countries and Update.
User-added image
User-added image


If you still want to use the trigger. Please try the below code.

trigger ChildTransportUpdate on Account (after update) {
    List<Line_Items__c> lilist = new List<Line_Items__c>();
    Set<Id> ids=new Set<Id>();
    Map<Id,List<Line_Items__c>> liMap=new Map<Id,List<Line_Items__c>>();
    List<Line_Items__c> indList= new LIst<Line_Items__c>();
    List<Line_Items__c> otherList= new LIst<Line_Items__c>();
    for(Account a:trigger.new)
    {
        ids.add(a.id);
    }
    List<Line_Items__c> lList= [SELECT Name,Id,Account__c,
                                Transport__c from Line_Items__c where Account__c in : ids];
    System.debug('lList'+lList);
    for(Line_Items__c li:lList)
    {
        if(liMap.containsKey(li.Account__c)) {
            List<Line_Items__c> liids = liMap.get(li.Account__c);
            liids.add(li);
            liMap.put(li.Account__c, liids);
        } else {
            liMap.put(li.Account__c, new List<Line_Items__c> { li });
        }    
    }  
    for(Account a:trigger.new){
        System.debug('liMap.get(a.id)'+liMap.get(a.id));
        if(liMap.keySet().size()>0){
            if(a.Country__c=='Ind'){
                indList.addall(liMap.get(a.id));
            }
            else{
                otherList.addAll(liMap.get(a.id));
            }
        }
    }
    System.debug('indList'+indList);
    System.debug('otherList'+otherList);
    for(Line_Items__c l:indList){
        l.Transport__c='By Road';
        liList.add(l);
        
    }
    for(Line_Items__c l:otherList){
        l.Transport__c='By Air';
        liList.add(l);
        
    }
    update liList;
}


Let me know if you have any other queries.

Thanks,
Sowmya.
 
Ajay K DubediAjay K Dubedi
Hi System Admin,

Here is a solution to your Problem.
    
    --------------------Trigger--------------------
    
trigger q6trigger on Account (before update) {
    TravelTriggerHandler.selectvalue(trigger.new);  

    
    --------------------Trigger_Handler------------
    
public with sharing class TravelTriggerHandler {
    public static void selectvalue(List<Account> lstAccount){
        try{   
               map<Id,Travel__c> mapOfTravel = new map<Id,Travel__c>();
            for(Travel__c tv : [SELECT Id,Name,Transport__c,Account__c FROM Travel__c WHERE Account__c IN:lstAccount]){
                mapOfTravel.put(tv.Account__c,tv);
            } 
            System.debug('map of Travel :::::::::::::::::'+ mapOfTravel);
            
            for(Account acc : lstAccount){
               if(acc.Country__c == 'IND')
                    mapOfTravel.get(acc.Id).Transport__c = 'By Road'; 
                else
                    mapOfTravel.get(acc.Id).Transport__c = 'By Air';
            }
            if(mapOfTravel.Size()>0)
                update mapOfTravel.Values(); 
        }
        catch(Exception e){
            System.debug('Exception:::::'+ e);
        }
    }    
}
    
I hope it will help you.
Please select this as Best Answer so that other's also get help from this.
 
Thank You
Ajay Dubedi