You need to sign in to do that
Don't have an account?
louisa 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 correct
Thanks
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 correct
Thanks
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
Hi,
I made some edits in your code. Please refer the same.
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.
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
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>
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>