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
Nichole SmithNichole 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>

 
Best Answer chosen by Nichole Smith
Anirudh SinghAnirudh Singh
Hi Nicole,

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:
public with sharing class LinkOfferOpportunityProduct
{
    //This variable holds the Id of the Opportunity Product on which the button was clicked.
    private Id currentOpportunityProductId;
    
    //This will hold the Opportunity Product record on which the button was clicked.
    private Opportunity_Product__c currentOpportunityProduct;
    
    //This list holds the siblings Oppotunity Product records.
    public List<Opportunity_Product__c> siblingOpportunityProductsList{get; set;}
    
    //This variable will fetch the Id of the Selected Sibling Opportunity Product to be linked to the Opportunity Product record on which the button was clicked.
    public Id selectedDealId{get; set;}
    
    public LinkOfferOpportunityProduct()
    {
        try
        {
            //The Opportunity Product Id on which you clicked the button.
            currentOpportunityProductId=ApexPages.currentPage().getParameters().get('Id');
            
            //The Opportunity Product Record to bring the Opportunity Id.
            currentOpportunityProduct=[
                SELECT Id, Opportunity__c, Linked_Deal__c 
                FROM Opportunity_Product__c 
                WHERE Id=:currentOpportunityProductId
            ];
            
            //Sibling Opportunity Product records for that Opportunity Product.
            //This list will not include the Opportunity on which the button was clicked.
            siblingOpportunityProductsList=[
                SELECT Id, Final_Deal_Title_Editable__c, Opportunity__c, Member_Price__c, Start_Date__c 
                FROM Opportunity_Product__c 
                WHERE Opportunity__c=:currentOpportunityProduct.Opportunity__c 
                AND Id!=:currentOpportunityProductId
            ];
        }
        catch(Exception oException)
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, oException.getMessage()));
        }     
    }
    
    //This method will be called when Select link on the Visualforce Page is clicked.
    public PageReference AddlinkedOffer()
    {
        PageReference OppProdPageRef;
        
        //The Opportunity Product record on which the button was clicked,
        //should be linked to the Selected Oppotunity Product.
        currentOpportunityProduct.Linked_Deal__c=selectedDealId;
        
        try
        {   
            update currentOpportunityProduct;
            
            //The page is redirected back to the Opportunity Product on which the button was clicked.
            //If you have to redirect it to the Selected Sibling Opportunity Product on the Visualforce Page. Change the currentOpportunityProductId to selectedDealId.
            OppProdPageRef=new PageReference('/'+currentOpportunityProductId);
        }
        catch(Exception oException)
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, oException.getMessage()));
        }
        return OppProdPageRef;          
    }   
}

Visualforce Page:
<apex:page controller="LinkOfferOpportunityProduct">
    <apex:form >
        <apex:pageBlock title="Sibling Opportunity Products">
            <apex:pageBlockTable value="{!siblingOpportunityProductsList}" var="siblingsOppProducts">
                <apex:column headerValue="Deal Title">
                    <apex:outputText value="{!siblingsOppProducts.Final_Deal_Title_Editable__c}" />
                </apex:column>
                
                <apex:column headerValue="Member Price">
                    <apex:outputText value="{!siblingsOppProducts.Member_Price__c}" />
                </apex:column>
                
                <apex:column headerValue="Start Date">
                    <apex:outputText value="{!siblingsOppProducts.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="{!siblingsOppProducts.Id}" assignTo="{!selectedDealId}"/>
                    </apex:commandLink>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Please let me know if this helps you.
If yes, please mark the question as Solved.


Thanks and Regards,
Anirudh Singh
 

All Answers

Anirudh SinghAnirudh Singh
Hi Nicole,

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:
public with sharing class LinkOfferOpportunityProduct
{
    //This variable holds the Id of the Opportunity Product on which the button was clicked.
    private Id currentOpportunityProductId;
    
    //This will hold the Opportunity Product record on which the button was clicked.
    private Opportunity_Product__c currentOpportunityProduct;
    
    //This list holds the siblings Oppotunity Product records.
    public List<Opportunity_Product__c> siblingOpportunityProductsList{get; set;}
    
    //This variable will fetch the Id of the Selected Sibling Opportunity Product to be linked to the Opportunity Product record on which the button was clicked.
    public Id selectedDealId{get; set;}
    
    public LinkOfferOpportunityProduct()
    {
        try
        {
            //The Opportunity Product Id on which you clicked the button.
            currentOpportunityProductId=ApexPages.currentPage().getParameters().get('Id');
            
            //The Opportunity Product Record to bring the Opportunity Id.
            currentOpportunityProduct=[
                SELECT Id, Opportunity__c, Linked_Deal__c 
                FROM Opportunity_Product__c 
                WHERE Id=:currentOpportunityProductId
            ];
            
            //Sibling Opportunity Product records for that Opportunity Product.
            //This list will not include the Opportunity on which the button was clicked.
            siblingOpportunityProductsList=[
                SELECT Id, Final_Deal_Title_Editable__c, Opportunity__c, Member_Price__c, Start_Date__c 
                FROM Opportunity_Product__c 
                WHERE Opportunity__c=:currentOpportunityProduct.Opportunity__c 
                AND Id!=:currentOpportunityProductId
            ];
        }
        catch(Exception oException)
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, oException.getMessage()));
        }     
    }
    
    //This method will be called when Select link on the Visualforce Page is clicked.
    public PageReference AddlinkedOffer()
    {
        PageReference OppProdPageRef;
        
        //The Opportunity Product record on which the button was clicked,
        //should be linked to the Selected Oppotunity Product.
        currentOpportunityProduct.Linked_Deal__c=selectedDealId;
        
        try
        {   
            update currentOpportunityProduct;
            
            //The page is redirected back to the Opportunity Product on which the button was clicked.
            //If you have to redirect it to the Selected Sibling Opportunity Product on the Visualforce Page. Change the currentOpportunityProductId to selectedDealId.
            OppProdPageRef=new PageReference('/'+currentOpportunityProductId);
        }
        catch(Exception oException)
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, oException.getMessage()));
        }
        return OppProdPageRef;          
    }   
}

Visualforce Page:
<apex:page controller="LinkOfferOpportunityProduct">
    <apex:form >
        <apex:pageBlock title="Sibling Opportunity Products">
            <apex:pageBlockTable value="{!siblingOpportunityProductsList}" var="siblingsOppProducts">
                <apex:column headerValue="Deal Title">
                    <apex:outputText value="{!siblingsOppProducts.Final_Deal_Title_Editable__c}" />
                </apex:column>
                
                <apex:column headerValue="Member Price">
                    <apex:outputText value="{!siblingsOppProducts.Member_Price__c}" />
                </apex:column>
                
                <apex:column headerValue="Start Date">
                    <apex:outputText value="{!siblingsOppProducts.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="{!siblingsOppProducts.Id}" assignTo="{!selectedDealId}"/>
                    </apex:commandLink>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Please let me know if this helps you.
If yes, please mark the question as Solved.


Thanks and Regards,
Anirudh Singh
 
This was selected as the best answer
Nichole SmithNichole Smith
Thank you anirudh this works great. I appreciate the help.
Anirudh SinghAnirudh Singh
No problem Nicole. Don't worry to ask questions. Have a great day. Thanks. :)