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
Javier MaldonadoJavier Maldonado 

Query all child records contained in to an opportunity

I'm trying to query all related record in a child list from opportunity object, but I've not achieved this:

Parent Object: Opportunity

Child Object: Opportunity_Product__c (Custom Object)
Fields: Product_Description1__c and Product_Dimension__c
Master-Detail(Opportunity): Opportunity__c

So far, I have:
Select Id, Name, 
(
Select Id, Opportunity__r.Opportunity__c, 
Opportunity__r.Product_Description1__c, 
Opportunity__r.Product_Dimesion__c 

from Opportunity_Product__c

from Opportunity where id =  '0061b000002DjSCAA0'

Any HELP, will appreciated... Thanks
Best Answer chosen by Javier Maldonado
Sumit Kumar Singh 9Sumit Kumar Singh 9
Sorry Bro, 
You need to read the Relationship Queries - 

Try this one - 

Select Id, Name, (Select Id, Product_Description1__c, Product_Dimesion__c from Opportunity_Products__r) from Opportunity where id =  '0061b000002DjSCAA0'

Pls, let me know, if it solves your problem.

Thanks, 
Sumit Kumar Singh
 

All Answers

Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Javier,
You can try this -
You can go to "Opportunity__c" field and look for "child Relationship Name" ; whatever the name is append "__r" in the last. The nested SOQL is accessed with the Child relationship name.

Select Id, Name, 
(
Select Id, Opportunity__r.Opportunity__c, 
Opportunity__r.Product_Description1__c, 
Opportunity__r.Product_Dimesion__c 

from Opportunity_Product__r

from Opportunity where id =  '0061b000002DjSCAA0'

Hope it will help you.

Thanks,
Sumit Kuamr Singh
 
Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri
Hi first we have check the name of the child relationship for that we have to go to childobject -> fields -> Masterobject lookup, here you can find the 'child relationship name' usually child relationship name would be plural for eg:- Opportunity_Products__c.

So just give a try using below code.

[Select Id, Name, 
(
Select Id, Opportunity__r.Opportunity__c, 
Opportunity__r.Product_Description1__c, 
Opportunity__r.Product_Dimesion__c 

from Opportunity_Products__r

from Opportunity where id =  '0061b000002DjSCAA0']

 

but I don't know why you are query values in the Master Object Opportunity from child object ?

You can query directly 

[Select Id, Name, Opportunity__c,Product_Description1__c, Product_Dimesion__c,
(
Select Id
from Opportunity_Products__c

from Opportunity where id =  '0061b000002DjSCAA0']
Javier MaldonadoJavier Maldonado
Thanks for your recomendations...

But I'm still getting the same error...  

From Sumit code: 
from Opportunity_Product__r
     ^
ERROR at Row:8:Column:6
Didn't understand relationship 'Opportunity_Product__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.

From Shaswanthnaga code:
Opportunity__r.Opportunity__c, 
^
ERROR at Row:4:Column:1
No such column 'Opportunity__c' on entity 'Opportunity'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.

Answering your question...
I'm trying to create a RollUp Text field from all Opportunity_Product__c object records, and update RollUp_Product_Description field (located in Opportunity) and separated by ";", the only way to achieve this is doing a Trigger from the child object (Opportunity_Product__c)...

In other words, trying to create a RollUp Text field

Any hand, will be appriciated...

Javier
 
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello,
Did you check the relationship name?
Javier MaldonadoJavier Maldonado
Thanks Sumit,

This is from Opportunity_Product__c object

User-added image
Sumit Kumar Singh 9Sumit Kumar Singh 9
Yes, click on "opportunity", there you will see "child Relationship Name", copy the value and apppend "__r" in the last.
Javier MaldonadoJavier Maldonado
Thanks... nothing... still not working...

User-added image
Sumit Kumar Singh 9Sumit Kumar Singh 9
Bro, 
here, the relationship name is "Opportunity_Products", so the relationship name would be "Opportunity_Products__r",

try this one - 

Select Id, Name, 
(
Select Id, Opportunity__r.Opportunity__c, 
Opportunity__r.Product_Description1__c, 
Opportunity__r.Product_Dimesion__c 

from Opportunity_Products__r

from Opportunity where id =  '0061b000002DjSCAA0'


 
Javier MaldonadoJavier Maldonado
Thanks Sumit,

Nothing!!!

Opportunity.Opportunity__c, 
^
ERROR at Row:4:Column:1
Didn't understand relationship 'Opportunity' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.
Sumit Kumar Singh 9Sumit Kumar Singh 9
Sorry Bro, 
You need to read the Relationship Queries - 

Try this one - 

Select Id, Name, (Select Id, Product_Description1__c, Product_Dimesion__c from Opportunity_Products__r) from Opportunity where id =  '0061b000002DjSCAA0'

Pls, let me know, if it solves your problem.

Thanks, 
Sumit Kumar Singh
 
This was selected as the best answer
Javier MaldonadoJavier Maldonado
No Problem Sumit and Thanks ... I am...

It's working now...