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
GONZALO PIZARRO RIVERA 6GONZALO PIZARRO RIVERA 6 

Map Error: System.StringException: Invalid id:

Hello, I am trying to get the User->Id field using a custom field (Id_SAP__c) that i have in the user object. 
To do this i have created a map with those two fields in order to get the id of the user but i am receiving an error and i dont know how to solve it.
Please, can anyone help me?

This my code:

public static void fillOwnerField(List<Promoci_n__c> lstPromociones){
        Set<String> setResources = new Set<String>();
        for(Promoci_n__c p :lstPromociones){
            if(p.Comercial_Id_SAP__c != null){
                setResources.add(p.Comercial_Id_SAP__c);
            }   
        }
        System.debug('***setResources = '+setResources);
        if(setResources.size() > 0){
            Map<Id, User> mapSR = new Map<Id, User>([Select Id, Id_SAP__c From User where Id_SAP__c in: setResources]);
            System.debug('***mapSR = '+mapSR);
            for(Promoci_n__c p :lstPromociones){
                System.debug('***p.Comercial_Id_SAP__c = '+p.Comercial_Id_SAP__c);
                System.debug('***Comercial_Asignado__c = '+mapSR.values());
                System.debug('***Comercial_Asignado__c = '+mapSR.get(p.Comercial_Id_SAP__c).Id_SAP__c);
                p.Comercial_Asignado__c = mapSR.get(p.Comercial_Id_SAP__c).Id_SAP__c;
            }

        }
    }

I get the error in line

System.debug('***Comercial_Asignado__c = '+mapSR.get(p.Comercial_Id_SAP__c).Id_SAP__c);

thank you in advance
Best Answer chosen by GONZALO PIZARRO RIVERA 6
Maharajan CMaharajan C
Hi Gonzalo,

Please try the below code:
 
public static void fillOwnerField(List<Promoci_n__c> lstPromociones){
Set<String> setResources = new Set<String>();
for(Promoci_n__c p :lstPromociones){
	if(p.Comercial_Id_SAP__c != null){
		setResources.add(p.Comercial_Id_SAP__c);
	}   
}
System.debug('***setResources = '+setResources);
if(setResources.size() > 0){
	//Map<Id, User> mapSR = new Map<Id, User>([Select Id, Id_SAP__c From User where Id_SAP__c in: setResources]);
	Map<String, Id> mapSR = new Map<String, Id> userMap();
	for(user u : [Select Id, Id_SAP__c From User where Id_SAP__c in: setResources]){
		mapSR.put(u.Id_SAP__c, u.Id);
	}
	
	System.debug('***mapSR = '+mapSR);
	for(Promoci_n__c p :lstPromociones){
		if(p.Comercial_Id_SAP__c != null && mapSR.containsKey(p.Comercial_Id_SAP__c)){
			System.debug('***p.Comercial_Id_SAP__c = '+p.Comercial_Id_SAP__c);
			System.debug('***Comercial_Asignado__c = '+mapSR.values());
			System.debug('***Comercial_Asignado__c = '+mapSR.get(p.Comercial_Id_SAP__c));
			p.Comercial_Asignado__c = mapSR.get(p.Comercial_Id_SAP__c);
		}
	}
}
}

Thanks,
Maharajan.C

 

All Answers

Maharajan CMaharajan C
Hi Gonzalo,

Please try the below code:
 
public static void fillOwnerField(List<Promoci_n__c> lstPromociones){
Set<String> setResources = new Set<String>();
for(Promoci_n__c p :lstPromociones){
	if(p.Comercial_Id_SAP__c != null){
		setResources.add(p.Comercial_Id_SAP__c);
	}   
}
System.debug('***setResources = '+setResources);
if(setResources.size() > 0){
	//Map<Id, User> mapSR = new Map<Id, User>([Select Id, Id_SAP__c From User where Id_SAP__c in: setResources]);
	Map<String, Id> mapSR = new Map<String, Id> userMap();
	for(user u : [Select Id, Id_SAP__c From User where Id_SAP__c in: setResources]){
		mapSR.put(u.Id_SAP__c, u.Id);
	}
	
	System.debug('***mapSR = '+mapSR);
	for(Promoci_n__c p :lstPromociones){
		if(p.Comercial_Id_SAP__c != null && mapSR.containsKey(p.Comercial_Id_SAP__c)){
			System.debug('***p.Comercial_Id_SAP__c = '+p.Comercial_Id_SAP__c);
			System.debug('***Comercial_Asignado__c = '+mapSR.values());
			System.debug('***Comercial_Asignado__c = '+mapSR.get(p.Comercial_Id_SAP__c));
			p.Comercial_Asignado__c = mapSR.get(p.Comercial_Id_SAP__c);
		}
	}
}
}

Thanks,
Maharajan.C

 
This was selected as the best answer
GONZALO PIZARRO RIVERA 6GONZALO PIZARRO RIVERA 6
Thanks