0

I have a SOQL to retrieve values for a custom object which has reference to another custom object(Ticket__c). I am gettign the following error-

System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Payment__c.Ticket__r for the line - String uname=getPayments.get(i).Ticket__r.User__c;

List<Payment__c> getPayments = [Select Transaction_Number__c,
                                       TransactionDate__c,
                                       Type__c,
                                       Mode__c,
                                       Amount__c,
                                       Ticket__c 
                                  from Payment__c];
for(Integer i=0;i<getPayments.size();i++)
{ 
String uname=getPayments.get(i).Ticket__r.User__c;
}

1 Answer 1

5

You need to query the parent field that you reference in the for loop:

List<Payment__c> getPayments = [Select Transaction_Number__c,
                                       TransactionDate__c,
                                       Type__c, 
                                       Mode__c, 
                                       Amount__c, 
                                       Ticket__r.user__c 
                                  from Payment__c];

Ticket_c is simply the ID field of the related Ticket__c object. You need the actual field in the related object, hence ticket__r.user__c

As an aside, I wouldn't name the list of Payments getPayments as that sounds like a method name; better would be payments or paymentsList

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .