+ Start a Discussion
d3developerd3developer 

getSObject works inconsistently

 

 

I've been debugging an issue where I need to use getSObject to get the inner object on a query.

 

Debug code:

 

 

SObject sox = Database.query('select id, Contact.FirstName from CampaignMember limit 1'); 
System.debug (sox.getSObject('Contact') == null) );

 

 

I have four testMethods with this, three return true on the second line, the fourth returns false.

 

It appears always to be false in a managed package.

 

Which means that, in my experience, getSObject works 20% or less of the time.

 

 

 

 

d3developerd3developer

Further update:

 

When I set all testmethods to run with the System Administrator profile instead of the Standard User profile all four worked (getSObject('Contact')  returned a contact.

 

However, when I tried to deploy as a managed package all four failed. Note, the managed package uploads fine, but fails on install. 

 

 

 

SuperfellSuperfell

Is your actual problem with contact, or with a custom object that's in your package? IIRC getSObject will return null in the case where there's no contact (or you don't have access to the contact record), so if contactId was null, or if the contactId was for a contact the calling user doesn't have access to, getSObject is going to return null.

hemmhemm

 


SimonF wrote:

Is your actual problem with contact, or with a custom object that's in your package? IIRC getSObject will return null in the case where there's no contact (or you don't have access to the contact record), so if contactId was null, or if the contactId was for a contact the calling user doesn't have access to, getSObject is going to return null.


 

Agree.  If you don't filter for ContactId != null, your test could pull a record tied to a Lead.  Make sure and filter for a Campaign Member that's tied to a Contact.

 

Also, your test should handle a situation where there are no Campaign Members in their system. Unless you are creating them as part of your test.  If so, you can count on them being there.

 

d3developerd3developer

The problem was all on my end. In the first org all of the CampaignMembers were connected to contacts in the second all were connected to Leads, which explains everything.

 

::blushes::