+ Start a Discussion
kundlik Yewalekundlik Yewale 

how to retrieve record id from map using custom filed

Hello All,
how to retrieve record id from map using custom filed.
List<String> lststringid =  new List<String>();
                for(Servicing_Error__c se : lstservicingerror)
                {
                    if(se.Fastrack_Work_Order__c != null)
                    {
                         lststringid.add(se.Fastrack_Work_Order__c) ;  
                    }   
                }
                Map<id,workOrder> mapWorkOrder = new Map<id,workOrder>([Select id,FAStrackIdentifier__c from WorkOrder where FAStrackIdentifier__c IN:lststringid]); 
                
                System.debug('Keyset'+mapWorkOrder);
                
                for(Servicing_Error__c se2:lstservicingerror)
                {
                    if(Se2.Fastrack_Work_Order__c != null)
                    {
                        System.debug('mapWorkOrder.get(se2.Fastrack_Work_Order__c).id'+mapWorkOrder.get(se2.Fastrack_Work_Order__c).id);
                         se2.Fastrack_Work_Order__c = mapWorkOrder.get(se2.Fastrack_Work_Order__c).id; 
                    }
                }


I am getting below error:
System.StringException: Invalid id: 23982178: External entry point
Best Answer chosen by kundlik Yewale
Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi Kundlik 

Try this piece of code : 

List<String> lststringid =  new List<String>();
for(Servicing_Error__c se : lstservicingerror)
{
    if(se.Fastrack_Work_Order__c != null)
    {
         lststringid.add(se.Fastrack_Work_Order__c) ;  
    }   
}
Map<String,Id> mapWorkOrder = newMap<String,Id>();
for( WorkOrder objWorkOrder : [Select id,FAStrackIdentifier__c from WorkOrder where FAStrackIdentifier__c IN:lststringid]) {
    mapWorkOrder.put(objWorkOrder.FAStrackIdentifier__c,objWorkOrder.Id);
}

for(Servicing_Error__c se2:lstservicingerror)
{
    if(Se2.Fastrack_Work_Order__c != null && mapWorkOrder.containsKey(se2.Fastrack_Work_Order__c))
    {
         se2.Fastrack_Work_Order__c = mapWorkOrder.get(se2.Fastrack_Work_Order__c).id; 
    }
}

In your code your data in the field was of text type but your map had 'ID' as dataype due to which this type conversion error arised.

Cheers!!!