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
Carsten BONOMOCarsten BONOMO 

Master-detail SOQL query

Hello,

I have a Master-detail relationship between Train-Ticket.
Using a SOQL query on Ticket I need to obtain a field value of the associated train.
This is the query I'm using : 
 public static void ticketInfo(ID ticketId){
        Ticket__c ticket = [SELECT ID,Name,PAX_Type__c, Train__r.FPU_Train_Category__c
                            FROM Ticket__c 
                            WHERE ID =: ticketID];
        system.debug(ticket);
    }

The issue is that instead of the desired field (FPU_Train_Category__c) it returns the Train's ID. I tried Train__r.Name and it still returned the ID.

Thanks a lot for explaining this to me!
Have a nice day

 
Best Answer chosen by Carsten BONOMO
Aman MalikAman Malik
HI Carsten,

Your query seems correct. However there may be chances that, you are debugging them in wrong way.
Kindly confirm you are using below debug to show train name:
system.debug(ticket.Train__r.FPU_Train_Category__c);

Thanks,
Aman
 

All Answers

v varaprasadv varaprasad
HI Carsten

Could you please confirm FPU_Train_Category__c is lookup or text field which datatype?

If it is look up field then write below query.

SELECT ID,Name,PAX_Type__c, Train__r.FPU_Train_Category__r.name
                            FROM Ticket__c 
                            WHERE ID =: ticketID


Thanks
Varaprasad
Carsten BONOMOCarsten BONOMO
Thanks for your quick answer.

It's a text field so your query doesn't work.

Regards
Aman MalikAman Malik
HI Carsten,

Your query seems correct. However there may be chances that, you are debugging them in wrong way.
Kindly confirm you are using below debug to show train name:
system.debug(ticket.Train__r.FPU_Train_Category__c);

Thanks,
Aman
 
This was selected as the best answer
Carsten BONOMOCarsten BONOMO

Hi Aman,

That debugging actaully worked! Glad the query was right.
Any idea why my way of debugging doesn't work?

Thanks a lot

Aman MalikAman Malik
HI Carsten,
Is your issue resolved?

Please like the answer and mark it as best if this resolve your issue.

Thanks,
Áman