+ Start a Discussion
louisa barrett 7louisa barrett 7 

apex code to get child opportuities

HI,

I'm attempting to create my first Visual Force page displaying results from an Apex class.
Could someone help me please with the code on how to retrieve all the opportunities from child accounts where the parent account is the one being viewed.

Apex Code:
public with sharing class ChildOpportunities {
    Public id Parent_Acc_Id;
          public ChildOpportunities(ApexPages.StandardController controller) {
    Parent_Acc_Id = ApexPages.currentPage().getparameters().get('id');
    }
         public List<Opportunity> getRelatedOpportunities(){
        List <opportunity> childOpps = New List<Opportunity>();
        for(Opportunity opp:[select Name, Total_Opportunity_Amount__c  
                             from opportunity where account.parent.id=: Parent_Acc_Id]){
                        childOpps.add(opp);    
        }
        return childOpps;
    }
}

VF Page code

<apex:page sidebar="false" showHeader="false" standardController="Account" extensions="ChildOpportunities">
    <apex:pageBlock title="Child Opportunities">
        <apex:pageBlockSection title =" {! Opportunity.Account.Name}" columns="5">
            Opp Name: <apex:outputField  value="{! Opportunity.Name}"/>  
            Opp Value: <apex:outputField  value="{! Opportunity.Total_Opportunity_Amount__c}"/> 
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>

I'll apologise in advance, as I say this is my first attempt so I'm sure there are a hundred things wrong.....
I currently have the two following errors, but I'm not sure the principal is even correctUser-added image

Thanks
Best Answer chosen by louisa barrett 7
Pankaj_GanwaniPankaj_Ganwani
Hi,

You gave the braces while calling function from <apex:pageblocktable>. Just use below mentioned code. I made some edits in your code.

<apex:page sidebar="false" showHeader="false" standardController="Account" extensions="ChildOpportunitiesAP">
   <apex:form>
    <apex:pageBlock title="Child Opportunities">
          <apex:pageBlockTable value="{! RelatedOpportunities}" var = "opp">
            Opp Name: <apex:outputField  value="{! opp.Name}"/>  
            Opp Value: <apex:outputField  value="{! opp.Total_Opportunity_Amount__c}"/> 
        </apex:pageBlockTable>
    </apex:pageBlock>
  </apex:form>
</apex:page>

All Answers

Pankaj_GanwaniPankaj_Ganwani
Apex Code:
public with sharing class ChildOpportunities {
    private id Parent_Acc_Id;
          public ChildOpportunities(ApexPages.StandardController controller) {
    Parent_Acc_Id = ApexPages.currentPage().getparameters().get('id');
    }
         public List<Opportunity> getRelatedOpportunities(){
        List <opportunity> childOpps = New List<Opportunity>();
        for(Opportunity opp:[select Name, Total_Opportunity_Amount__c  
                             from opportunity where OpportunityId=: Parent_Acc_Id]){
                        childOpps.add(opp);    
        }
        return childOpps;
    }
}

VF Page code

<apex:page sidebar="false" showHeader="false" standardController="Account" extensions="ChildOpportunities">
   <apex:form>
    <apex:pageBlock title="Child Opportunities">
          <apex:pageBlockTable value="{!RelatedOpportunities()}" var = "opp">
            Opp Name: <apex:outputField  value="{! opp.Name}"/>  
            Opp Value: <apex:outputField  value="{! opp.Total_Opportunity_Amount__c}"/> 
        </apex:pageBlockTable>
    </apex:pageBlock>
  </apex:form>
</apex:page>

Hi,

I made some edits in your code. Please refer the same.
louisa barrett 7louisa barrett 7
Hi,

Thank you very much for your assistance.
I've edited the code as you suiggested but it's returning the following error.
I have renamed by ChildOpportunituies Apex Class just for clarity for myself.


User-added image
This is my code:

Apex class:

public with sharing class ChildOpportunitiesAP {
    private id Parent_Acc_Id;
          public ChildOpportunitiesAP(ApexPages.StandardController controller) {
    Parent_Acc_Id = ApexPages.currentPage().getparameters().get('id');
    }
         public List<Opportunity> getRelatedOpportunities(){
        List <opportunity> childOpps = New List<Opportunity>();
        for(Opportunity opp:[select Name, Total_Opportunity_Amount__c  
                             from opportunity where Id=: Parent_Acc_Id]){
                        childOpps.add(opp);    
        }
        return childOpps;
    }
}

VF Page:

<apex:page sidebar="false" showHeader="false" standardController="Account" extensions="ChildOpportunitiesAP">
   <apex:form>
    <apex:pageBlock title="Child Opportunities">
          <apex:pageBlockTable value="{! RelatedOpportunities()}" var = "opp">
            Opp Name: <apex:outputField  value="{! opp.Name}"/>  
            Opp Value: <apex:outputField  value="{! opp.Total_Opportunity_Amount__c}"/> 
        </apex:pageBlockTable>
    </apex:pageBlock>
  </apex:form>
</apex:page>

Many thanks
Pankaj_GanwaniPankaj_Ganwani
Hi,

You gave the braces while calling function from <apex:pageblocktable>. Just use below mentioned code. I made some edits in your code.

<apex:page sidebar="false" showHeader="false" standardController="Account" extensions="ChildOpportunitiesAP">
   <apex:form>
    <apex:pageBlock title="Child Opportunities">
          <apex:pageBlockTable value="{! RelatedOpportunities}" var = "opp">
            Opp Name: <apex:outputField  value="{! opp.Name}"/>  
            Opp Value: <apex:outputField  value="{! opp.Total_Opportunity_Amount__c}"/> 
        </apex:pageBlockTable>
    </apex:pageBlock>
  </apex:form>
</apex:page>
This was selected as the best answer
Pankaj_GanwaniPankaj_Ganwani
If this solves your issue, Please close this thread by marking the best answer so others can also refer the same and it can help them out.
louisa barrett 7louisa barrett 7
Thank you. I'll test it first thing in the morning. Thank you for your assistance Sent from Samsung Mobile on O2
louisa barrett 7louisa barrett 7
This is the code that worked in the end for me. I replaced the Apex:OutputField with apex:column values instead.
Many thanks for all your assistance.

Apex Class
public class ChildOpportunitiesAP {
    private id Parent_Acc_Id;
    
          public ChildOpportunitiesAP(ApexPages.StandardController controller) {
    Parent_Acc_Id = ApexPages.currentPage().getparameters().get('id');
    }
         public List<Opportunity> getRelatedOpportunities(){
             List <Opportunity> childOpps = New List<Opportunity>();
        FOR(Opportunity opp:[SELECT Account.Name, Name, Total_Opportunity_Amount__c  
                             FROM Opportunity WHERE Account.Parent.id=: Parent_Acc_Id]){
                        childOpps.add(opp); 
        } 
           Return childOpps;   
         }
 }

VF Page
<apex:page sidebar="false" showHeader="false" standardController="Account" extensions="ChildOpportunitiesAP">
   <apex:form>
    <apex:pageBlock title="Child Opportunities">
          <apex:pageBlockTable value="{! RelatedOpportunities}" var = "opp">
               <apex:column value="{! opp.Account.Name}"/>
              <apex:column value="{! opp.Name}"/>
              <apex:column value="{! opp.Total_Opportunity_Amount__c}"/>
          </apex:pageBlockTable>
    </apex:pageBlock>
  </apex:form>
</apex:page>