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
Amr MohsenAmr Mohsen 

How to pqass parameters to embed VF Page

Hello, 
I want to create and embed VF page iniside Case page to display all similar cases which have the same type and subtype for current case, but I want to send Type and sub stype as parameters to embed VF page to display similar cases based on those parameters? 
I need help please.
Best Answer chosen by Amr Mohsen
Vishwajeet kumarVishwajeet kumar
Try this:
<apex:page standardController="Case" extensions="CaseController" tabStyle="Case">
    <apex:pageBlock>
        <apex:pageblocktable value="{!SimilarCases}" var="rec">
            <apex:column headerValue="Case Number">			
             <apex:outputLink value="/{!rec.ID}" target="_blank">{!rec.CaseNumber}</apex:outputLink>
            </apex:column>
            <apex:column headerValue="Case Subject">
            <apex:outputLink value="/{!rec.ID}" target="_blank">{!rec.Subject}</apex:outputLink>
            </apex:column>
            <apex:column headerValue="Case Owner">
            <apex:outputLink value="/{!rec.OwnerID}" target="_blank">{!rec.Owner.Name}</apex:outputLink>
            </apex:column>
        </apex:pageblocktable>
    </apex:pageBlock>
</apex:page>

 

All Answers

Vishwajeet kumarVishwajeet kumar
if you want to query cases with same type of fields, you don't need to pass those as parameter. you can just query it from current case and use it for querying it , for other similar cases.
OR
You could just add fields in Standard controller field list use them for querying other records.

Example : 
public with sharing class CaseController{
public Case caseRecord{get;set;}
public CaseController(ApexPages.StandardController controller){
        if (!Test.isRunningTest()) {
            //Add all field which is required to copy from parent case to child case
            controller.addFields(new List<String> {
                'owner.email'
                ,'ParentID'
                ,'OwnerId'
                ,'Reason'
                ,'Status'
                ,'Account'
                ,'Contact'
                ,'Contact.Email'
                ,'Contact.FirstName'
                ,'Contact.LastName'              
            });
        }
        caseRecord = (Case)controller.getRecord();
    }

//implement other methods to query records using "caseRecord".
}

 
Amr MohsenAmr Mohsen
Thank you Vishwajeet for your answer, but forry, could you tell me whats is (!Test.isRunningTest()) ? 
Vishwajeet kumarVishwajeet kumar
Code throws error when it is used from Test class(offcorse in test class,data could be created or queried it self, so their is no need of this code), so if condition is added to execute it only if controller is called in non-test environment.
 
Amr MohsenAmr Mohsen
So Code in VF Page can be like this ? 
 
<apex:page standardController="CaseController" tabStyle="Case">
    <apex:pageBlock>
        <apex:pageBlockSection>
            <apex:outputField value="{!Case.CaseNumber}"/>
            <apex:outputField value="{!Case.Subject}"/>
            <apex:outputField value="{!Case.Owner}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>

Correct me if I'm wrong please , I'm still new to SF development
Vishwajeet kumarVishwajeet kumar
<apex:page standardController="Case" extensions="CaseController" tabStyle="Case">
    <apex:pageBlock>
<!-- Use custom record variable to display the child list-->
        <apex:pageBlockSection>
            <apex:outputField value="{!Case.CaseNumber}"/>
            <apex:outputField value="{!Case.Subject}"/>
            <apex:outputField value="{!Case.Owner}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>
you need to use extension controller, and Standard Controller will be "Case" to embed it in case page layout.

 
Amr MohsenAmr Mohsen
Thank you so much , 
Finally I've created this method to find similar cases
//implement other methods to query records using "caseRecord".
    public List<Case> SimilarCases(){
        List<Case> cList = [SELECT id FROM Case WHERE Case_Sub_Type__c =: caseRecord.Case_Sub_Type__c];
        return cList;
    }

I just need to know how to use it in visual force page.
Vishwajeet kumarVishwajeet kumar
you should call method in visulaforce in a table:
<apex:pageblocktable value="{!SimilarCases}" var="rec">
<apex:colmn value="{!rec.Id}"/>
</apex:pagebloacktable>

you can go through trailhead, they are good way to learn.https://developer.salesforce.com/trailhead/
Amr MohsenAmr Mohsen
Thank you , but I get an error again 
<apex:page standardController="Case" extensions="CaseController" tabStyle="Case">
    <apex:pageBlock>
        <apex:PageBlockTable value="{!SimilarCases}" var="rec" >
            <apex:column value="{!rec.Id}" />
        </apex:PageBlockTable>
    </apex:pageBlock>
</apex:page>

Unknown property 'CaseStandardController.SimilarCases'
Vishwajeet kumarVishwajeet kumar
Can you put your controller code here?
Method needs to be visible to page.
Amr MohsenAmr Mohsen
Controller code 
public with sharing class CaseController{
public Case caseRecord{get;set;}
public CaseController(ApexPages.StandardController controller){ 
    
            //Add all field which is required to copy from parent case to child case
            controller.addFields(new List<String> {
                'owner.email'
                ,'ParentID'
                ,'Case_Sub_Type__c'
                ,'OwnerId'
                ,'Reason'
                ,'Status'
                ,'Account'
                ,'Contact'
                ,'Contact.Email'
                ,'Contact.FirstName'
                ,'Contact.LastName'              
            });
        
        caseRecord = (Case)controller.getRecord();
    }

//implement other methods to query records using "caseRecord".
    public List<Case> SimilarCases(){
        List<Case> cList = [SELECT id FROM Case WHERE Case_Sub_Type__c =: caseRecord.Case_Sub_Type__c];
        return cList;
    }
}

VF page Code
<apex:page standardController="Case" extensions="CaseController" tabStyle="Case">
    <apex:pageBlock>
        <apex:pageblocktable value="{!SimilarCases}" var="rec">
            <apex:column value="{!rec.Id}" />
        </apex:pageblocktable>
    </apex:pageBlock>
</apex:page>

 
Vishwajeet kumarVishwajeet kumar
Can you try to change below method name as "getSimilarCases" and see!
 public List<Case> SimilarCases(){
Amr MohsenAmr Mohsen
I did exactly what you told me but I got different error 
"Invalid api version:0.0"
Amr MohsenAmr Mohsen
Thank you Vishwajeet, this is really helpfull , 
Now I can display the similar cases into VF page in a block in case standard page, 
But all columns of the table are text not a hyper link to cases, I tried to do it by the code below 
<apex:page standardController="Case" extensions="CaseController" tabStyle="Case">
    <apex:pageBlock>
        <apex:pageblocktable value="{!SimilarCases}" var="rec">
            <apex:column headerValue="Case Number">
             <apex:outputLink value="{!rec.CaseNumber}">{!rec.CaseNumber}</apex:outputLink>
            </apex:column>
            <apex:column headerValue="Case Subject">
            <apex:outputLink value="{!rec.Subject}">{!rec.Subject}</apex:outputLink>
            </apex:column>
            <apex:column headerValue="Case Owner">
            <apex:outputLink value="{!rec.Owner.Name}">{!rec.Owner.Name}</apex:outputLink>
            </apex:column>
        </apex:pageblocktable>
    </apex:pageBlock>
</apex:page>

But when I click any link it navigates me to URL which  No Longer Exists , also the new page is embded and I want to open it in new separated page.

Thank you again Vishwajeet.
Vishwajeet kumarVishwajeet kumar
Try this:
<apex:page standardController="Case" extensions="CaseController" tabStyle="Case">
    <apex:pageBlock>
        <apex:pageblocktable value="{!SimilarCases}" var="rec">
            <apex:column headerValue="Case Number">			
             <apex:outputLink value="/{!rec.ID}" target="_blank">{!rec.CaseNumber}</apex:outputLink>
            </apex:column>
            <apex:column headerValue="Case Subject">
            <apex:outputLink value="/{!rec.ID}" target="_blank">{!rec.Subject}</apex:outputLink>
            </apex:column>
            <apex:column headerValue="Case Owner">
            <apex:outputLink value="/{!rec.OwnerID}" target="_blank">{!rec.Owner.Name}</apex:outputLink>
            </apex:column>
        </apex:pageblocktable>
    </apex:pageBlock>
</apex:page>

 
This was selected as the best answer
Amr MohsenAmr Mohsen
Thank you Vishwajeet , I really appreciate your help.