You need to sign in to do that
Don't have an account?
System 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.
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.
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.
Check for India.
Update Transport in Child Object
Similarly check for other countries and Update.
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.
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