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
David MatusowDavid Matusow 

whoid in select from lead results not found

I have an odd event happening.  I am doing a simple SELECT in an event, but the whoId is not correct.  I have a method that determines the element type (lead, account, opportunity, etc) and I see the whoid in there for the lead.  I immediately do a select (below) but the select fails because the whoid shows an event instead of the lead I'm working with.  I can't see why this should be.
 
List<Lead> leadList = [select Id, Name, PostalCode, Street, State, City, Latitude, Longitude
from Lead where Id = :whoId];
I can run the EXACT same query and get a response.  I can see the whoid and it shows the correct ID for the lead.  In the code, it fails:
11:17:25.0 (111774981)|EXCEPTION_THROWN|[126]|System.QueryException:
Event WHERE id = '00U15-------'

Above this, the whoid shows it to be a lead (00Q150-----).

What could I be doing wrong??
 
Mike ArthurMike Arthur
Can you post the method that determines the type and sets the whoId?
David MatusowDavid Matusow
private String getParentType (String whoId, String whatId) {
    
        if (whatId != null) {
        
        system.debug('substring is ' + whatId.substring(0,3));
        
            if (whatId.substring(0,3) == '006') {
                return OPPORTUNITY;
            } else if (whatId.substring(0,3) == '001') {
                return ACCOUNT;         
            } else if (whatId.substring(0,3) == '500') {
                return CSE;
            }
        }
        if (whoId != null) {        
            if (whoId.substring(0,3) == '003') {
                return CONTACT;
            } else if (whoId.substring(0,3) == '00Q') {
                return LEAD;
            }
        }
        return NONE;        
    }

nothing fancy!  It only sends back a setting that is easier to use than having to do the above.  It doesn't SET whoid!
David MatusowDavid Matusow
I went a step further and did the following in the code to ENSURE that I'm looking at a Lead
if (whoId.substring(0,3) == '00Q') {
	List<Lead> leadList = [select Id, Name, PostalCode, Street, State, City, Latitude, Longitude
                from Lead where Id = :whoId ];
    
	if (leadList.size() > 0) {
		leadhold = leadList[0];
        }
} else {
	system.debug('whoid is not Lead ' + whoid);
}
It flows into the query, as expected (as the whoid IS a Lead!), but fails the query with the same issue!

I was thinking that maybe it's tied into the polymorphic nature, but none of the other related queries fail (account, opportunity, case, and contact (which is also using whoid)).  Very perpexed!!