For below query:

System.debug([SELECT Id, Account_lookup__r.Account_Number__c FROM Case]);

I'm getting:

12:05:52:125 USER_DEBUG [1]|DEBUG|(Case:{Id=5003C000001YKoSQAW}, Case:{Id=5003C000001YKoNQAW}, Case:{Id=5003C000001YKoIQAW}, Case:{Id=5003C000001YKoDQAW}, Case:{Id=5003C000001YKo8QAG}, Case:{Id=5003C000001YKo3QAG}, Case:{Id=5003C000001YKnyQAG}, Case:{Id=5003C000001YKntQAG}, Case:{Id=5003C000001YHQLQA4}, Case:{Id=5003C000001YH0RQAW}, ...)

Now how to access that.

  • Hi, and welcome to SFSE! As written, we don't have enough information to help you. Please read more about How to Ask, then edit your question to include additional details. Details like any error messages, framework error messages, etc would be useful, as well as the code you've written so far. – Amit Singh Aug 1 '18 at 6:47
  • Hi Amit, you post this comment on all my questions please use the screenshot provided to you in previous question it is the same. Thanks – Mobidoubts Aug 1 '18 at 7:10
  • Hi Amit please use this link to see the details: salesforce.stackexchange.com/questions/223691/… – Mobidoubts Aug 1 '18 at 7:25

The Account_Number__c field that you want to access is on the record related through the Account_lookup__r relation, assuming it is the standard Account object, you would want to do:

list<Case> Cases = [SELECT Id, Account_lookup__r.Account_Number__c FROM Case];
for ( Case c : Cases ){
    System.debug( c );
    Account a = c.Account_lookup__r;
    System.debug( a );

If you want to assign to a variable the value of the Name field of the record being referenced by the lookup, you would need to declare a String type variable (since the Name field is a text string), and then use the assignment operator (=) to give it a value.

list<Case> Cases = [SELECT Id, Account_lookup__r.Name FROM Case];
for ( Case c : Cases ){
    System.debug( c );
    Account a = c.Account_lookup__r;
    if ( a != NULL ){
        String AccountName = a.Name;
        System.debug( AccountName );
  • Possibly need to use getsobject here but maybe not – Eric Aug 1 '18 at 7:05
  • Thanks! martin I can access the value now, Although It is giving null value. – Mobidoubts Aug 1 '18 at 7:07
  • Alright @Eric let me try with the getsobject. – Mobidoubts Aug 1 '18 at 7:11
  • @Mobidoubts since the case records in your question don't have the Account_lookup__c field populated, I would expect them to produce null in the debug statement here. – martin Aug 1 '18 at 7:12
  • I voted up for this answer. \m/ – Mobidoubts Aug 1 '18 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.