You need to sign in to do that
Don't have an account?
Nichole Smith
How get a list of sibling records in a visualforce page to link to the current record?
We have a custom object called Opportunty_Product__c that is a lookup to an opportunity. What I wanted to get all of the sibling records of the current opportunity product into a visualforce page where I could select one and link it to the current opportunity product. I am new to apex code and have tried to get this done in a class but all I can get in the page is the opportunity product you clicked the visualforce page from. What am I doing wrong?
Here is my Apex Class:
public with sharing class LinkOfferOpportunityProduct {
public list <Opportunity_Product__c> OppProdList {get;set;}
public list <Opportunity_Product__c> OppProdId {get;set;}
public list <Opportunity_Product__c> LOppProds{get;set;}
public list <Opportunity_Product__c> OppList {get;set;}
public list <string> OppIds {get; set;}
public string selectedOppProd {get;set;}
public string sSelectedDeal {get; set;}
public LinkOfferOpportunityProduct(){
try {
OppProdId = new list<Opportunity_Product__c> ();
OppIds = new list<string> ();
selectedOppProd = ApexPages.currentPage().getParameters().get('id');
OppProdID = [SELECT Id,Opportunity__c FROM Opportunity_Product__c
WHERE Id =: selectedOppProd];
for (Opportunity_Product__c OppProd : OppProdID) {
OppIds.add(OppProd.Opportunity__c);
}
LOppProds = [SELECT id,Final_Deal_Title_Editable__c,Opportunity__c,Member_Price__c,Start_Date__c FROM Opportunity_Product__c
WHERE id =: OppProdId];
}catch (Exception oException){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, oException.getMessage()));
}
}
public PageReference AddlinkedOffer() {
PageReference OppProdPageRef;
Opportunity_Product__c SelectedOppProdId= [SELECT
Id,Linked_Deal__c
FROM
Opportunity_Product__c
WHERE
Id = :LOppProds];
SelectedOppProdId.Linked_Deal__c= sSelectedDeal;
try {
update SelectedOppProdId;
OppProdPageRef = new PageReference('/' + selectedOppProdId);
} catch (Exception oException){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, oException.getMessage()));
}
return OppProdPageRef;
}
}
An here is my visualforce page:
<apex:page controller="LinkedOfferOpportunityProduct">
<apex:form >
<apex:pageBlock >
<apex:PageBlockTable value="{!LOppProds}" var="oppprods" > <apex:column headerValue="Deal Title">
<apex:outputText value="{!oppprods.Final_Deal_Title_Editable__c}" />
</apex:column> <apex:column headerValue="Member Price">
<apex:outputText value="{!oppprods.Member_Price__c}" />
</apex:column> <apex:column headerValue="Start Date">
<apex:outputText value="{!oppprods.Start_Date__c}" />
</apex:column>
<apex:column headerValue="Link Offer to Opportunity Product" >
<apex:commandLink action="{!Addlinkedoffer}" value="Select" id="commandLink" >
<apex:param name="nickName" value="{!oppprods.Id}" assignTo="{!sSelectedDeal}"/>
</apex:commandLink>
</apex:column>
</apex:PageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Here is my Apex Class:
public with sharing class LinkOfferOpportunityProduct {
public list <Opportunity_Product__c> OppProdList {get;set;}
public list <Opportunity_Product__c> OppProdId {get;set;}
public list <Opportunity_Product__c> LOppProds{get;set;}
public list <Opportunity_Product__c> OppList {get;set;}
public list <string> OppIds {get; set;}
public string selectedOppProd {get;set;}
public string sSelectedDeal {get; set;}
public LinkOfferOpportunityProduct(){
try {
OppProdId = new list<Opportunity_Product__c> ();
OppIds = new list<string> ();
selectedOppProd = ApexPages.currentPage().getParameters().get('id');
OppProdID = [SELECT Id,Opportunity__c FROM Opportunity_Product__c
WHERE Id =: selectedOppProd];
for (Opportunity_Product__c OppProd : OppProdID) {
OppIds.add(OppProd.Opportunity__c);
}
LOppProds = [SELECT id,Final_Deal_Title_Editable__c,Opportunity__c,Member_Price__c,Start_Date__c FROM Opportunity_Product__c
WHERE id =: OppProdId];
}catch (Exception oException){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, oException.getMessage()));
}
}
public PageReference AddlinkedOffer() {
PageReference OppProdPageRef;
Opportunity_Product__c SelectedOppProdId= [SELECT
Id,Linked_Deal__c
FROM
Opportunity_Product__c
WHERE
Id = :LOppProds];
SelectedOppProdId.Linked_Deal__c= sSelectedDeal;
try {
update SelectedOppProdId;
OppProdPageRef = new PageReference('/' + selectedOppProdId);
} catch (Exception oException){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, oException.getMessage()));
}
return OppProdPageRef;
}
}
An here is my visualforce page:
<apex:page controller="LinkedOfferOpportunityProduct">
<apex:form >
<apex:pageBlock >
<apex:PageBlockTable value="{!LOppProds}" var="oppprods" > <apex:column headerValue="Deal Title">
<apex:outputText value="{!oppprods.Final_Deal_Title_Editable__c}" />
</apex:column> <apex:column headerValue="Member Price">
<apex:outputText value="{!oppprods.Member_Price__c}" />
</apex:column> <apex:column headerValue="Start Date">
<apex:outputText value="{!oppprods.Start_Date__c}" />
</apex:column>
<apex:column headerValue="Link Offer to Opportunity Product" >
<apex:commandLink action="{!Addlinkedoffer}" value="Select" id="commandLink" >
<apex:param name="nickName" value="{!oppprods.Id}" assignTo="{!sSelectedDeal}"/>
</apex:commandLink>
</apex:column>
</apex:PageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
There are a few mistakes in the code. I am listing them below:
1. There is no need for the below FOR Loop:
for (Opportunity_Product__c OppProd : OppProdID) {
OppIds.add(OppProd.Opportunity__c);
}
Reason: There is only one Opportunity Product Id passed in the id parameter, you are fetching using
ApexPages.currentPage().getParameters().get('id');
So, on querying, it will return only one record and therefore, you need not iterate it.
2. Also, [SELECT Id,Opportunity__c FROM Opportunity_Product__c
WHERE Id =: selectedOppProd];
will return only one Opportunity Product. So, there is no need to take a list. Instead declare it like:
Opportunity_Product__c currentOpportunityProduct=[SELECT Id,Opportunity__c FROM Opportunity_Product__c
WHERE Id =: selectedOppProd];
3. In the below line, where you are fetching the Sibling Opportunity Product records, you need to put the Opportunity__c field for comparison, so that all the Opportunity Products for that Opportunity will come. Also, you need not fetch the current Opportunity Product record.
LOppProds = [SELECT id,Final_Deal_Title_Editable__c,Opportunity__c,Member_Price__c,Start_Date__c FROM Opportunity_Product__c
WHERE id =: OppProdId];
4. In the AddlinkedOffer method, you need not fetch the Opportunity Product record again.
Opportunity_Product__c SelectedOppProdId= [SELECT
Id,Linked_Deal__c
FROM
Opportunity_Product__c
WHERE
Id = :LOppProds];
Reason: You have already fetched it in the constructor OppProdID = [SELECT Id,Opportunity__c FROM Opportunity_Product__c
WHERE Id =: selectedOppProd];
You just need to add one more field in the query Linked_Deal__c and declare the variable (I have mentioned for Point 2 above ) currentOpportunityProduct in the class level.
Don't worry we all have faced problems when we started coding, don't feel embarrassed to ask questions. :)
I have corrected the code and explained what is going on in it.
Please find below the code:
Apex Class:
Visualforce Page:
Please let me know if this helps you.
If yes, please mark the question as Solved.
Thanks and Regards,
Anirudh Singh
All Answers
There are a few mistakes in the code. I am listing them below:
1. There is no need for the below FOR Loop:
for (Opportunity_Product__c OppProd : OppProdID) {
OppIds.add(OppProd.Opportunity__c);
}
Reason: There is only one Opportunity Product Id passed in the id parameter, you are fetching using
ApexPages.currentPage().getParameters().get('id');
So, on querying, it will return only one record and therefore, you need not iterate it.
2. Also, [SELECT Id,Opportunity__c FROM Opportunity_Product__c
WHERE Id =: selectedOppProd];
will return only one Opportunity Product. So, there is no need to take a list. Instead declare it like:
Opportunity_Product__c currentOpportunityProduct=[SELECT Id,Opportunity__c FROM Opportunity_Product__c
WHERE Id =: selectedOppProd];
3. In the below line, where you are fetching the Sibling Opportunity Product records, you need to put the Opportunity__c field for comparison, so that all the Opportunity Products for that Opportunity will come. Also, you need not fetch the current Opportunity Product record.
LOppProds = [SELECT id,Final_Deal_Title_Editable__c,Opportunity__c,Member_Price__c,Start_Date__c FROM Opportunity_Product__c
WHERE id =: OppProdId];
4. In the AddlinkedOffer method, you need not fetch the Opportunity Product record again.
Opportunity_Product__c SelectedOppProdId= [SELECT
Id,Linked_Deal__c
FROM
Opportunity_Product__c
WHERE
Id = :LOppProds];
Reason: You have already fetched it in the constructor OppProdID = [SELECT Id,Opportunity__c FROM Opportunity_Product__c
WHERE Id =: selectedOppProd];
You just need to add one more field in the query Linked_Deal__c and declare the variable (I have mentioned for Point 2 above ) currentOpportunityProduct in the class level.
Don't worry we all have faced problems when we started coding, don't feel embarrassed to ask questions. :)
I have corrected the code and explained what is going on in it.
Please find below the code:
Apex Class:
Visualforce Page:
Please let me know if this helps you.
If yes, please mark the question as Solved.
Thanks and Regards,
Anirudh Singh