+ Start a Discussion
aKallNVaKallNV 

problem with SOQL Bind

Need help with the following SOQL statement:

 

 

List<Contact> existingCons = [select ID, FirstName, LastName, Email, Department, Unit__c, StartDate__c, EndDate__c, Title, Phone from Contact where userID__c != null AND userID__c IN :idToUser.keySet()];

 

 

Here's the deal. This query isn't returning any records when it should be returning one. I know that it should be returning one because I know there is currently only one record because I am using the interface to trigger this code. I have used system.debug to confirm this. So the following 

 

system.debug(idToUser.keySet());

 returns the expected value, which is just an SF ID for the User object.

 

when I query directly for the id...like this

 

 

List<Contact> existingCons = [select ID, FirstName, LastName, Email, Department, Unit__c, StartDate__c, EndDate__c, Title, Phone from Contact where userID__c != null AND userID__c ='005Q0000000W6xbIAC'];

 The record is found as expected. That ID is the exact Id that is returned by the system.debug.

 

Does anybody see what I'm doing wrong?

 

Thanks!

 

Best Answer chosen by Admin (Salesforce Developers) 
aKallNVaKallNV

FALSE ALARM! I figured it out

 

Here is the solution:

 

 

List<User> theNewUs = [select ID, FirstName, LastName, Email, Department__c, Unit__c, DateofHire__c, TerminationDate__c, Title, Phone from User where ID IN: existingUs];
		Map<String,User> idToUser = new Map<String,User>();		
		List<Contact> consUpdate = new List<Contact>();
		
		for(User aUser : theNewUs) {
			idToUser.put(aUser.ID,aUser);			
		}
		
		List<Contact> existingCons = [select ID, FirstName, LastName, Email, Department, Unit__c, StartDate__c, EndDate__c, Title, Phone from Contact where userID__c IN :idToUser.keySet()];

  Here is the one that was causing problems:

 

 

List<User> theNewUs = [select ID, FirstName, LastName, Email, Department__c, Unit__c, DateofHire__c, TerminationDate__c, Title, Phone from User where ID IN: existingUs];
		Map<String,User> idToUser = new Map<String,User>();		
		List<User> theNewUs = [select ID, FirstName, LastName, Email, Department__c, Unit__c, DateofHire__c, TerminationDate__c, Title, Phone from User where ID IN: existingUs];
		Map<String,User> idToUser = new Map<String,User>();		
		List<Contact> consUpdate = new List<Contact>();
		
		for(User aUser : theNewUs) {
			idToUser.put(aUser.ID,aUser);			
		}

 

 

The difference between the one that works and the one that doesn't work is the position of the SOQL statement.  In the one that works I put the SOQL statement beneath the for Loop that populates the idToUser map.  I should have warned that I'm beginner!

 

Thanks for the help, though!

 

All Answers

Ritesh AswaneyRitesh Aswaney

The queries are identical, the only thing therefore that can be suspected is the keySet(). Puzzling.

 

What is the Map declared as ? Map<Id,...?

 

Is this part of a trigger or a cotrolller - maybe the context is making a difference of some sort?

aKallNVaKallNV

This is part of a trigger, and the map is declared like so

 

 

Map<String,User> idToUser = new Map<String,User>();

 

 

and userID__c is a Text field set to 18 charaters.

 

 

aKallNVaKallNV

Here is another variation of the query that I thought might help me debug, but doesn't maybe it will help somebody else...

 

 

List<Contact> existingCons = [select ID, FirstName, LastName, Email, Department, Unit__c, StartDate__c, EndDate__c, Title, Phone from Contact where userID__c IN ('005400000017Qg4AAE', '005Q0000000W6xbIAC')];

 

 

 

I tried this to see if had something to do with the 'IN' operator or because it was iterating through a list, but this query works fine...it finds the record.

aKallNVaKallNV

FALSE ALARM! I figured it out

 

Here is the solution:

 

 

List<User> theNewUs = [select ID, FirstName, LastName, Email, Department__c, Unit__c, DateofHire__c, TerminationDate__c, Title, Phone from User where ID IN: existingUs];
		Map<String,User> idToUser = new Map<String,User>();		
		List<Contact> consUpdate = new List<Contact>();
		
		for(User aUser : theNewUs) {
			idToUser.put(aUser.ID,aUser);			
		}
		
		List<Contact> existingCons = [select ID, FirstName, LastName, Email, Department, Unit__c, StartDate__c, EndDate__c, Title, Phone from Contact where userID__c IN :idToUser.keySet()];

  Here is the one that was causing problems:

 

 

List<User> theNewUs = [select ID, FirstName, LastName, Email, Department__c, Unit__c, DateofHire__c, TerminationDate__c, Title, Phone from User where ID IN: existingUs];
		Map<String,User> idToUser = new Map<String,User>();		
		List<User> theNewUs = [select ID, FirstName, LastName, Email, Department__c, Unit__c, DateofHire__c, TerminationDate__c, Title, Phone from User where ID IN: existingUs];
		Map<String,User> idToUser = new Map<String,User>();		
		List<Contact> consUpdate = new List<Contact>();
		
		for(User aUser : theNewUs) {
			idToUser.put(aUser.ID,aUser);			
		}

 

 

The difference between the one that works and the one that doesn't work is the position of the SOQL statement.  In the one that works I put the SOQL statement beneath the for Loop that populates the idToUser map.  I should have warned that I'm beginner!

 

Thanks for the help, though!

 

This was selected as the best answer