0

If I do this:

return [select id,
                 price__c,
                 original_price__c,
                 title__c,
                 tolabel(Shipping_Option__c),
                 Listing_Type__r.Name,
                 Item_Image__c,
                 Seller_Nickname__c,
                 Catalog_Product_Id__c,
                 Permalink__c,
                 Sold_Quantity__c,
                 Available_Quantity__c,
                 Sold_Qty_ofThisItem__c,
                 ml_id__c,
                 brand__c,
                 model__c,
                 Producto__r.Name,
                 catalog_item_relation__c,
                 catalog_variation_relation__c,
                 catalog_listing__c,
                 Catalog_Eligibility__c,
                 Discount_Type__c,
                 Free_Shipping__c,
                 Promo_Finish_Date__c,
                 Promo_Prime_Percentage__c,
                 Promo_Prime_Price__c,
                 Promo_Start_Date__c,
                 tolabel(Promo_Status__c),
                 (SELECT id,
                         Title__c,
                         Available_Quantity__c,
                         Producto__r.name
                     FROM Item_Variations__r)  
                 from Item__c where id in: itemIds];

How do the values from item_variations__r arrive? I have no way to tell because none of the test records available have item variations. For example, this is what arrives from one of the records, as shown in the browser console:

Object
    available_quantity: 30
    catalog_listing: false
    free_shipping: false
    id: "MLA512373197"
    image: "/resource/mlsales__ML_App_Resources/images/icons/sinfoto.jpg"
    listing_type: "bronze"
    listing_type_long: "a0EE0000003Z3bOMAS"
    permalink: "http://articulo.mercadolibre.com.ar/MLA-512373197-articulo-de-prueba-no-ofertar123- 
_JM"
    price: 90
    producto: {name: "Sintonizadora Tv Externa Mygica PT230 Digital Tda Smartphone Tablet Padtv", 
 sf_id: "a1kE0000000A2lZ", sf_id_long: "a1kE0000000A2lZIAS"}
    producto_long: "a1kE0000000A2lZIAS"
    promo_prime_percentage: 0
    seller: {nickname: "TT914358"}
    seller_nickname: "TT914358"
    sf_id: "a0DE0000009X8cl"
    sf_id_long: "a0DE0000009X8clMAC"
    shipping_mode: "not_specified"
    sold: 0
    sold_with_parent: 0
    title: "Articulo De Prueba-  No Ofertar__123"

What I want is to render values from those fields from item_variations in my html.

3 Answers 3

1

This problem has been already answered here. Some additional context for understanding the SOQL query results from this ref is given below:

Query results are returned as nested objects. The primary or “driving” object of the main SELECT statement in a SOQL query contains query results of subqueries.

The SOQL query (& subquery) result is of type List<SObject> and a single primary object record could contain zero to many child records in the subquery result. Apex and Force.com platform is strictly governed by limits and hence, the platform doesn't support accessing the parent and child records directly in a single result collection of some special type. This makes sense considering the efficient querying and performance optimization measures. So, your only option is to iterate the query result collection and access the subquery results as List<SObject> (as mentioned by others already).

If you had the result fetched into List<Item__c> items, then you can determine the number of child records using the List collection's size() method in the FOR loop code (as shown in the snippet below):

for(Item__c item: items){
   Integer itemVariationsCount = item.Item_Variations__r.size();
   // Write additional code as per your requirement based on the presence or absence of child records
}
1
  • Thanks a lot! I didn't really know how nested soql queries worked, given its the first time I'm working with it. Thanks again! Commented Dec 31, 2020 at 14:40
2

I suspect you are debugging YourReturnedList. Try accessing YourReturnedList[0].Item_Variations__r

1

Inner query in SOQL works where you have Lookups or Master-Detail relationships.

In your query, you might not have any data in Item_Variations__r object with a lookup or masterdetail data of Item__c with Ids as itemIds.

You need to check Item_Variations__r record in your org, if they have any lookup or master detail data as your itemIds

You must log in to answer this question.

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