September 7, 2020

Pass values from one Visualforce Page to Another Visualforce page in Salesforce

Visualforce Page:

<apex:page controller="AccountListController">
    
    <apex:form>
    
        <apex:pageBlock id="pg">
            
            <apex:pageBlockSection >
                <apex:panelGrid columns="3" cellpadding="3" cellspacing="3">
                    <apex:outputLabel value="Account Name: "/>
                    <apex:inputText value="{!acctName}"/>
                    <apex:commandButton value="Go" action="{!searchAcct}"/>
                </apex:panelGrid>
            </apex:pageBlockSection>
        
            <apex:pageBlockSection rendered="{!listAcct.Size > 0}" title="Accounts">        
                <apex:pageBlockTable value="{!listAcct}" var="acc">
                    <apex:column headerValue="Open Account Detail">
                        <apex:outputLink target="_blank" value="/{!acc.Id}">{!acc.Name}</apex:outputLink>
                    </apex:column>
                    <apex:column headerValue="Pass Account Id to a VF Page">
                        <apex:outputLink target="_blank" value="/apex/AccountDetail?accId={!acc.Id}">{!acc.Name}</apex:outputLink>
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
            
        </apex:pageBlock>
        
    </apex:form>
        
</apex:page>

Apex Class:

public class AccountListController {
    
    public String acctName { get; set; }
    public List < Account > listAcct { get; set; }
    
    public AccountListController() {
        
        listAcct = new List < Account >();
        
    }
    
    public void searchAcct() {
        
        String soql = 'SELECT Id, Name FROM Account WHERE Name LIKE \'' + acctName + '%\' LIMIT 10';
        listAcct = Database.query( soql );
        
    }    

}

Visualforce Page:

<apex:page >
    
    <apex:detail subject="{!$CurrentPage.parameters.accId}"/>
    
</apex:page>

Output:


No comments:

Post a Comment