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
Karthikeyan ChandranKarthikeyan Chandran 

VF page Help.

Hi Team,

I want to get opportunities that are related to case as a related list using VF page.

Can anyone help to fix this?

Thanks & Regards,
Karthikeyan Chandran
Best Answer chosen by Karthikeyan Chandran
Lokesh KumarLokesh Kumar
Hi Chandra,

Thanks for the detail requirement please use below code written as aper your requirement. You can customize the look and feel as per your further requirement. 

VF page
<apex:page standardController="Case"   extensions="Displayopportunityoncase" sidebar="false" showHeader="false">
    <apex:pageBlock>
    	<apex:pageBlockTable value="{!lstOpportunity}" var="opp">
            <apex:column>
            <apex:outputLink value="/{opp.id}"> {!opp.Name}</apex:outputLink>
            </apex:column>
            <apex:column value="{!opp.Name}"/>
            <apex:column value="{!opp.StageName}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

Apex Controller
 
public class Displayopportunityoncase {
    public static List<opportunity> lstOpportunity {get;set;}
    public static Id caseRecordId;
    
    public Displayopportunityoncase(ApexPages.StandardController controller){
        caseRecordId = controller.getRecord().Id;
        getOpportunity();
    }
    
    public static void getOpportunity(){
        lstOpportunity = new List<opportunity>();
        Case[] acc = [select Accountid from case where id = :caseRecordId];
        if(acc[0] != null){
        	lstOpportunity = [Select id,name,StageName,OwnerID from Opportunity where AccountID = :acc[0].AccountID];
           
            }
    }
}

Thanks
Lokesh

All Answers

Lokesh KumarLokesh Kumar

HI Chandra,

You can easily get Opportunities as a related list to achieve this
create a lookup field on opportunity object to Case and fill the lookup with case number.

Let me know if you have different scenario.
 

Thanks
Lokesh
 

Karthikeyan ChandranKarthikeyan Chandran
Hi Lokesh,

Thanks for the quick response.

Here in case object i have Account lookup and Opportunity Lookup. i want show the opportunities list that are related to Account.

Ex: Case -> Account-> Opportunities

Account have more than 1 Opportunities.
Lokesh KumarLokesh Kumar
Hi Chandra,

Thanks for the detail requirement please use below code written as aper your requirement. You can customize the look and feel as per your further requirement. 

VF page
<apex:page standardController="Case"   extensions="Displayopportunityoncase" sidebar="false" showHeader="false">
    <apex:pageBlock>
    	<apex:pageBlockTable value="{!lstOpportunity}" var="opp">
            <apex:column>
            <apex:outputLink value="/{opp.id}"> {!opp.Name}</apex:outputLink>
            </apex:column>
            <apex:column value="{!opp.Name}"/>
            <apex:column value="{!opp.StageName}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

Apex Controller
 
public class Displayopportunityoncase {
    public static List<opportunity> lstOpportunity {get;set;}
    public static Id caseRecordId;
    
    public Displayopportunityoncase(ApexPages.StandardController controller){
        caseRecordId = controller.getRecord().Id;
        getOpportunity();
    }
    
    public static void getOpportunity(){
        lstOpportunity = new List<opportunity>();
        Case[] acc = [select Accountid from case where id = :caseRecordId];
        if(acc[0] != null){
        	lstOpportunity = [Select id,name,StageName,OwnerID from Opportunity where AccountID = :acc[0].AccountID];
           
            }
    }
}

Thanks
Lokesh
This was selected as the best answer
brahmaji tammanabrahmaji tammana
Hi,

You can also try something like this.

Visualforce Page:
<apex:page showHeader="true" sidebar="true" controller="CaseAccOppController">
	<apex:form>		
		<apex:pageBlock>
			<apex:pageBlockTable value="{!OppList}" var="o">
				<apex:column value="{!o.Name}" />
			</apex:pageBlockTable>
		</apex:pageBlock>
	</apex:form>	
</apex:page>

Controller:
public class CaseAccOppController {

	public List<Opportunity> getOppList() {
		Case c = [select id,Accountid from Case where id =: ApexPages.currentPage().getParameters().get('id')];
		return [select id, name from Opportunity where accountid =: c.AccountId];
	}
}

Thanks
Brahma
Lokesh KumarLokesh Kumar
Hi Brahmajit,

To embedded the VF page in Standard detail page you have to set standard controller of the same object in which you are trying to add so use extension for your custom controller BTW Happy coding.

Thanks
Lokesh
 
Karthikeyan ChandranKarthikeyan Chandran
Hi Lokesh,

I tried your suggestion and it's pulling only one opportunity.

My case record, related to one Account and this Account have 10 Opportunities.
Lokesh KumarLokesh Kumar
HI Chandra,

Can you try putting System.debug('$$$'+lstOpportunity ) and check what values are coming or you can run these SOQL in developer console to verify the Record level.

Because I have tested this already and for me, it's working fine.

User-added image

Let me know if you have any concern.

Thanks,
Lokesh
Karthikeyan ChandranKarthikeyan Chandran
I did debug log and the log,
 
00:20:06.0 (74787693)|USER_DEBUG|[15]|DEBUG|$$$(Opportunity:{Id=006m0000007exymAAA, Name=Ru DMC5 (MP-167078), RecordTypeId=012m00000000lK7AAI})
00:20:06.0 (74795806)|METHOD_EXIT|[7]|01pm0000000EglX|Displayopportunityoncase.getOpportunity()
00:20:06.0 (74865226)|SYSTEM_MODE_ENTER|true

 
Lokesh KumarLokesh Kumar

I Think you don't have full permission to query all the record in your controller as above displaying in System.debug logs you have only one record did you tried through SOQL query editor in developer console.

Thanks,
Lokesh

Karthikeyan ChandranKarthikeyan Chandran
Hi Lokesh,

My appologies for the delay. the VF page in opportunity view i am getting properly in my page and it's working as expected.

Thanks once again.

Thanks & regards,
Karthikeyan Chandran