+ Start a Discussion
Dan RogersDan Rogers 

Can access a field in SOQL, can't access it in Apex....?

Hi,

I'm new to Apex Code / SOQL.

I'm trying to write a trigger based on someone's Mailchimp Interest Lists. I can the value I need no problem if I run SOQL directly, e.g.
 
select MC4SF__MC_Subscriber__r.MC4SF__Interests__c from lead where Id = '00Q24000004bfgYEAQ'

gives me the result I need:

User-added image
But whatever I do I can't seem to get access to this value in Apex Code, e.g. this is the trigger:
 
trigger updateMCStatus on Lead (after update) {

    Map<String, Lead> leadMap = new Map<String, Lead>();

    for (Lead lead : System.Trigger.new) {

        System.Debug('lead mc interests: ' + lead.MC4SF__MC_Subscriber__r.MC4SF__Interests__c);
        System.Debug('lead mc subscriber: ' + lead.MC4SF__MC_Subscriber__c);
		
        String query = 'select MC4SF__MC_Subscriber__r.MC4SF__Interests__c from lead where Id = \'' + String.escapeSingleQuotes(lead.Id) + '\'';
        
        System.Debug(query);
        	
        List<sObject> sobjList = Database.query(query);
		
        System.Debug('object: ' + sobjList[0]);
        
    }

}

Results of those debugs:

lead.MC4SF__MC_Subscriber__r.MC4SF__Interests__c = null
lead.MC4SF__MC_Subscriber__c = a0B24000004BPMnEAO
(the SOQL query) sobjList[0] =  Lead:{MC4SF__MC_Subscriber__c=a0B24000004BPMnEAO, Id=00Q24000004bfgYEAQ}​

Is there anything I can do to get this value :) ? It's driving me crazy.

Cheers!

Dan



 
Best Answer chosen by Dan Rogers
GauravTrivediGauravTrivedi
For accessing MC4SF__Interests__c you need to get Id of lead then you need to pass in SOQL to get MC4SF__Interests__c. You need to do something like this:
trigger updateMCStatus on Lead (after update) {

    Map<String, Lead> leadMap = new Map<String, Lead>();
	Set<Id> idSet = new Set<Id>();
    for (Lead lead : System.Trigger.new) {
		idSet.add(lead.id);
    }
	List<sObject> sobjList = [select MC4SF__MC_Subscriber__r.MC4SF__Interests__c from lead where Id IN: idSet];
	System.Debug('object: ' + sobjList);

}


 

All Answers

GauravTrivediGauravTrivedi
Hi Dan,

Is "MC4SF__Interests__c" a different object?

 
Dan RogersDan Rogers
Hi Guarav,

Thanks for your reply, yes it is a different object I think (at least in the schema). This data/schema is from an itegration (mailchimp). It's nested quite heavily;

User-added image


Leads (and Contacts) are related to an MC_Subscriber object, then that MC_Subscriber object has the Custom Field Interests (which I think is managed / autopopulated somehow... as I said, fairly new to this :) ).

User-added image

THanks!

Dan
James LoghryJames Loghry
The first debug statement is null because values across relationships (lead.MC4SF__MC_Subscriber__r.MC4SF__Interests__c in your case) aren't available directly via a trigger.

However, you should still be able to get it back in a SOQL query.  I would check that you have read (Object CRUD) permission on the MCSF_MC_Subscriber object first, and also that the MCS4SF_Intersests__c field is visible in your FLS or Field Level Security settings.  Additionally, it shouldn't matter, but verify that you have visibility to the MC4SF__MC_Subscriber__r record through Org Wide Defaults / Sharing Settings too.
GauravTrivediGauravTrivedi
For accessing MC4SF__Interests__c you need to get Id of lead then you need to pass in SOQL to get MC4SF__Interests__c. You need to do something like this:
trigger updateMCStatus on Lead (after update) {

    Map<String, Lead> leadMap = new Map<String, Lead>();
	Set<Id> idSet = new Set<Id>();
    for (Lead lead : System.Trigger.new) {
		idSet.add(lead.id);
    }
	List<sObject> sobjList = [select MC4SF__MC_Subscriber__r.MC4SF__Interests__c from lead where Id IN: idSet];
	System.Debug('object: ' + sobjList);

}


 
This was selected as the best answer
Dan RogersDan Rogers
Hi Guys,

Got it thanks. You were actually both right, I had some FLS that wasn't set, then I retrieved the Interests directly from the Subscriber Object:
 
String query = 'select MC4SF__Interests__c from MC4SF__MC_Subscriber__c where Id = \'' + String.escapeSingleQuotes(lead.MC4SF__MC_Subscriber__c) + '\'';
        	
List<sObject> sobjList = Database.query(query);
		
System.Debug('object: ' + sobjList[0]);

Gives me the value I needed (a0324000003mJl8AAE).

Sorry that I can't vote both as best answer, I flipped a coin :D

Thanks for your help!!

Dan