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
DritterDritter 

Handled nested records from a query

I have the following method that finds an order along with it's order items (child object). 
 
public static ccrz__e_Order__c getOrderDetails (String orderId){
        
        ccrz__e_Order__c order = [SELECT id,
                                  name,
                                  ccrz__OriginatedCart__c,
                                  ccrz__Storefront__c,
                                  ccrz__OrderDate__c,
                                  Pay_In_Deposit__c,
                                  Do_not_send_confirmation_email__c,
                                  Activate_w_Balance_Due_c__c,
                                  SubTotalAmount2__c,
                                  ccrz__ShipAmount__c,
                                  ccrz__TotalAmount__c,
                                  ccrz__TotalSurcharge__c,
                                  Tuition_Fee__c,
                                  UpfrontCost__c,
                                  Amount_Charged__c,
                                  ccrz__Account__c,
                                  CreatedById,
                                  ccrz__BillTo__c,
                                  ccrz__ShipTo__c, (SELECT id,
                                                    ccrz__Product__c,
                                                    ccrz__Category__c,
                                                    Transaction_Code__c,
                                                    ccrz__StoreId__c,
                                                    ccrz__Subscription_Start_Date__c,
                                                    Flex_End_Date__c,
                                                    ccrz__ItemTotal__c,
                                                    ccrz__Price__c,
                                                    AmountPaid__c,
                                                    ccrz__SubAmount__c
                                                    FROM ccrz__e_orderitems__r)
                                  FROM ccrz__e_Order__c 
                                  WHERE id =: orderId
                                  LIMIT 1];
        return order;
    }
I'm trying to loop through the orderItems and call another method, but I'm getting the following error in the compiler. 

Error: A non-foreign key field cannot be referenced in a path expression: ccrz__E_OrderItems__r
} else if (order != NULL){
            for (ccrz__E_Orderitem__c orderItemProducts: order.ccrz__e_orderitems__r) {
                cc_kapmp_order_ProductDAO.getSpecForProductId (order.ccrz__e_orderitems__r.ccrz__Product__c, 'Free Trial Product Id');
            }
        }
Any thoughts on why is doesn't like ccrz__E_OrderItems__r?

 
jigarshahjigarshah
You may have got the relationship name for the relationship between Order and OrderItems as incorrect. Try referring the article to understand how relationship names work (https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_parent_child.htm#sforce_api_calls_soql_relationships_parent_child)and how cna they be referred within Apex code.

Please mark the thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.