You need to sign in to do that
Don't have an account?
SFDC 2017
Need to update parent record lookup field from a child record picklist value through trigger
Hi All,
I have a problem while updating a parent record lookup field value from child record picklist value in before insert and before update trigger.Below is my code but the problem its not getting updated because its a string value in child object and parent its lookup field.so i am getting String Exception :Invalid Id:
List<Parent__c> ParentLst = new List<Parent__c>();
Map<String,Id> ParentstateMap = new Map<String,Id>();
for(ChildObj child : (List<ChildObj>) Trigger.new){
if(child.Primary__c && String.isNotBlank(child.Country_vod__c) && !ParentstateMap.containsKey(child.Account_vod__c))
ParentstateMap.put(child.State__c,child.Parent__c);
}
for(Parent__c pt:[select id,State__c from Parent__c where Id in :ParentstateMap.keySet()]){
if(ParentstateMap.containsKey(pt.id) && !ParentstateMap.get(pt.id).equals(pt.State__c)){
pt.State__c = ParentstateMap.get(pt.id);
ParentLst.add(acct);
}
}
if(ParentLst.size() >0){
try{
update ParentLst;
}catch(Exception e){
System.debug('Exception occurred'+e.getMessage());
}
}
Please help me to update lookup field value in parent from child object picklist value
Thanks !!!
I have a problem while updating a parent record lookup field value from child record picklist value in before insert and before update trigger.Below is my code but the problem its not getting updated because its a string value in child object and parent its lookup field.so i am getting String Exception :Invalid Id:
List<Parent__c> ParentLst = new List<Parent__c>();
Map<String,Id> ParentstateMap = new Map<String,Id>();
for(ChildObj child : (List<ChildObj>) Trigger.new){
if(child.Primary__c && String.isNotBlank(child.Country_vod__c) && !ParentstateMap.containsKey(child.Account_vod__c))
ParentstateMap.put(child.State__c,child.Parent__c);
}
for(Parent__c pt:[select id,State__c from Parent__c where Id in :ParentstateMap.keySet()]){
if(ParentstateMap.containsKey(pt.id) && !ParentstateMap.get(pt.id).equals(pt.State__c)){
pt.State__c = ParentstateMap.get(pt.id);
ParentLst.add(acct);
}
}
if(ParentLst.size() >0){
try{
update ParentLst;
}catch(Exception e){
System.debug('Exception occurred'+e.getMessage());
}
}
Please help me to update lookup field value in parent from child object picklist value
Thanks !!!
A rough example would be like this below
Map<string,string> mapofstate = new map<string,string>();
List<state__c> lststate = [select Id, name from state__c];
For(state__c stateobj : lststate){
Mapofstate.put(stateobj.name , stateobj.id);
}
And use this map by passing state name as picklist value in your child object.
Something like this.
Mapofstate.get(childpicklistfield);
You are using wrong map so you need to update code, Try the below code :
List<Parent__c> ParentLst = new List<Parent__c>();
Map<String,Id> ParentstateMap = new Map<String,Id>();
for(ChildObj child : (List<ChildObj>) Trigger.new){
if(child.Primary__c && String.isNotBlank(child.Country_vod__c) && !ParentstateMap.containsKey(child.Account_vod__c))
ParentstateMap.put(child.Parent__c,child.State__c);
}
for(Parent__c pt:[select id,State__c from Parent__c where Id in :ParentstateMap.keySet()]){
if(ParentstateMap.containsKey(pt.id) && !ParentstateMap.get(pt.id).equals(pt.State__c)){
pt.State__c = ParentstateMap.get(pt.id);
ParentLst.add(acct);
}
}
if(ParentLst.size() >0){
try{
update ParentLst;
}catch(Exception e){
System.debug('Exception occurred'+e.getMessage());
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi