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
Avinash Dhanke 32Avinash Dhanke 32 

populated the lookup filed in text field

i Have created the handler for that in the text filed  i want the name but populated the id  following is my code

public class Populatedlookup {


    Public void updateField(List<Lead> leadList){
        /*
        for(lead l : leadList){
            leadId.add(l.Id);
            System.debug('leadid'+ leadId.add(l.Id));
        }
        lead leadOne = [select id, name, Channel_Partner__c from lead where Id IN: leadId];
        if(leadOne.Channel_Partner__c!=null){
        Distributor__c dis = [select id, name from Distributor__c where Id=:leadOne.Channel_Partner__c];
        DistributorName = dis.Name;
*/
        for(Lead leadObj:LeadList){
            if(leadObj.Channel_Partner__c!=null){
                leadObj.Channel_Partner_Text__c=leadObj.Channel_Partner__r.Name;
            }
            else{
                leadObj.Channel_Partner_Text__c='';
            }
            
        }
      
    }
}
Best Answer chosen by Avinash Dhanke 32
ManojjenaManojjena
HI Avinash,

Hope you  are calling this method from trigger ,if so then you need to do a additional query to parent object field details . 
Try with below code ,if the parent object name is different then plz change .

public class Populatedlookup {
    Public void updateField(List<Lead> leadList){
        Set<Id> chanelIdSet= new Set<Id>();
        Map<Id,Channel_Partner__c> chanelMap;// Change Object Name if diffrent 
        for(Lead leadObj:LeadList){
            if(leadObj.Channel_Partner__c!=null){
                chanelIdSet.add(leadObj.Channel_Partner__c);
            }
        }
        if(!chanelIdSet.isEmpty()){
           chanelMap=  new Map<Id,Channel_Partner__c>([SELECT Id,Name FROM Channel_Partner__c WHERE Id IN : chanelIdSet ]);
        }
        for(Lead leadObj:LeadList){
            if(leadObj.Channel_Partner__c!=null){
                if(!chanelMap.isEmpty() && chanelMap.get(leadObj.Channel_Partner__c) != null){
                    leadObj.Channel_Partner_Text__c= chanelMap.get(leadObj.Channel_Partner__c).Name;
                }
            }else{
                leadObj.Channel_Partner_Text__c='';
            }
        }
    }
}

Let me know if it helps !!

Thanks 
Manoj
 

All Answers

ManojjenaManojjena
HI Avinash,

Hope you  are calling this method from trigger ,if so then you need to do a additional query to parent object field details . 
Try with below code ,if the parent object name is different then plz change .

public class Populatedlookup {
    Public void updateField(List<Lead> leadList){
        Set<Id> chanelIdSet= new Set<Id>();
        Map<Id,Channel_Partner__c> chanelMap;// Change Object Name if diffrent 
        for(Lead leadObj:LeadList){
            if(leadObj.Channel_Partner__c!=null){
                chanelIdSet.add(leadObj.Channel_Partner__c);
            }
        }
        if(!chanelIdSet.isEmpty()){
           chanelMap=  new Map<Id,Channel_Partner__c>([SELECT Id,Name FROM Channel_Partner__c WHERE Id IN : chanelIdSet ]);
        }
        for(Lead leadObj:LeadList){
            if(leadObj.Channel_Partner__c!=null){
                if(!chanelMap.isEmpty() && chanelMap.get(leadObj.Channel_Partner__c) != null){
                    leadObj.Channel_Partner_Text__c= chanelMap.get(leadObj.Channel_Partner__c).Name;
                }
            }else{
                leadObj.Channel_Partner_Text__c='';
            }
        }
    }
}

Let me know if it helps !!

Thanks 
Manoj
 
This was selected as the best answer
Avinash Dhanke 32Avinash Dhanke 32
Thanks Manoj,its working