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
DoondiDoondi 

Query to get opportunities from custom object?

Hi,
I have 3 objects:
Opportunity
Pro__c
Unc__c 
(Pro__c and Unc__c has lookup to opportunities)

To get unc__c values from Opportunities i am using this query,
select name from opportunity where unc__r.id='idvalue'


can I query from Unc__c to get opportunities??
If so, How?
Best Answer chosen by Doondi
FARSANA PSFARSANA PS
hai,

Try it...
   
<apex:pageBlockTable   value="{!records}" var="index" >    
               <apex:column  value="{!index.name}"/>
              <apex:column value="{!index.Phone__c}"/>
               <apex:column>
                 <apex:pageBlockTable value="{!index.Opportunities__r}" var="c" > 
                 <apex:column   value="{!c.name}"/>
               </apex:pageBlockTable>
                   </apex:column>
            </apex:pageBlockTable> 
 </apex:pageblock>

hope this works....

All Answers

Meenu MathewMeenu Mathew
Hi Doondi,

Yes you can query Unc__c to get Opportunity.
Here is an example of the query:-

select Id, Opportunity__r.name from unc__c 

Thanks,
Meenu Mathew
FARSANA PSFARSANA PS
Hi Doondi,
Use this query,  

  Querying from child record:

       select id,name,opportunity__r.name,opportunity__r.id from unc__c

  Querying from master record:

      select id,name,(select id,name,opportunity__c from uncs__r) from opportunity
   

Hope this  helps..
DoondiDoondi
Hi,
for both the answers I am getting this error 

Didn't understand relationship 'opportunities__r' in field path.
FARSANA PSFARSANA PS
Hi,
   Use your relationship field's API name, replace '__c' with ' __r' ,that will be your relationship name. For eg : If lookup field name is 'opportunity__c', then use 'opportunity__r' in  query.
 
Meenu MathewMeenu Mathew
Hi,

'opportunities__r'  is used for retrieving field values from the unc__c's related Opportunity object when these objects have relationship via Lookup field.
 If Opprtunity is your related object, then for retriving Opportunity's fields we use 'opportunities__r' in query,

Thanks
DoondiDoondi
Hi,
I tried all of the above options but the Error didn't go.

while I was checking how they gave relations, I found this:
If I am trying to create a new Opp record.
> Pro__c has lookUp
AND
Unc__c is a depends on Pro__c.

by any chance this is creating error?
FARSANA PSFARSANA PS
Hi,
Try this,

 select id,name,(select id,name,unc__c from Opportunities__r) from unc__c

 Let me know if its works!
DoondiDoondi
@Farsana Ps 
This is giving required result when I do query from Developer console,
but when I add this query to class it just giving pro__c Name.
any idea where I may be going wrong?
FARSANA PSFARSANA PS
Hi,
 Can you share your apex code, so that I can help.Also please specify your context...
Ajay K DubediAjay K Dubedi
Hi Doondi,

Please have a look at the following code. Hope this will help you.

public class UncCls {

    public static void getOpportunities()
    {
        List<Unc__c> uncList = new List<Unc__c>();
        List<Opportunity> oppList = new List<Opportunity>();
        set<Id> oppIdSet = new set<Id>();
        uncList = [SELECT id,Name,Opportunity__c FROM Unc__c LIMIT 1000];
        for(Unc__c uncObj : uncList)
        {
            oppIdSet.add(uncObj.Opportunity__c);            
        } 
        if(oppIdSet != null)
        {
            oppList = [SELECT Id, Name FROM Opportunity WHERE id IN:oppIdSet];
            
            if(oppList.size() > 0)
            {
                for(Opportunity oppObj : oppList)
                    system.debug('Opp Name :' + oppObj.Name);
            }
        }       
    }
}

Thank You
Ajay Dubedi
DoondiDoondi
This is my simple Page and class, 
It's works as required 
But I want to add Opportunities as column.
public class UnisPageController 
{
    public List<Unc__c> Records{get;set;}
    public Pro__c prop {get;set;}
    
    public UnitsPageController(ApexPages.StandardController controller) 
    { 
        prop = (Pro__c)controller.getRecord();      
        Records = [SELECT id, Name,phone_c,Flag__c,Unit__c,(select name from Opportunities__r) 
          FROM Unc__c 
          WHERE Flag__c!=True AND Unit__c = :Prop.Id];
        }
    
}


Visualforce page:

<apex:pageblock >
                        <apex:pageBlockTable value="{!Records}" var="index">
                            <apex:column value="{!index.Name}"/>
                            <apex:column value="{!index.Phone__c}"/>
                            <!--<apex:column value="{!index.Opportunities__r.Name}"/> -->
                        </apex:pageBlockTable>
                    </apex:pageblock>

 
FARSANA PSFARSANA PS
hai,

Try it...
   
<apex:pageBlockTable   value="{!records}" var="index" >    
               <apex:column  value="{!index.name}"/>
              <apex:column value="{!index.Phone__c}"/>
               <apex:column>
                 <apex:pageBlockTable value="{!index.Opportunities__r}" var="c" > 
                 <apex:column   value="{!c.name}"/>
               </apex:pageBlockTable>
                   </apex:column>
            </apex:pageBlockTable> 
 </apex:pageblock>

hope this works....
This was selected as the best answer
DoondiDoondi
@FARSANA PS
Thankyou So much!!
you saved 100 hairs on my head!!

There was little bit of Css work needed but am able to achieve the requirement.

Thanks a ton!!