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
SFDC 2017SFDC 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 !!!
Dushyant SonwarDushyant Sonwar
As mentioned above that you have a custom object state__c , so you need to create a state name and state id map .

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);
Ajay K DubediAjay K Dubedi
Hi,
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