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
Ravi_SFDCRavi_SFDC 

How to pass the parameters from Visualforce to Apex Class

I do have a page with /apex/AccountPrioritySort?id={!Account_Plan__c.Id}&type=AP. Please find my code as below

Visual Force Page

<apex:page standardController="Account_Plan__c" extensions="accountprioritysort1" tabStyle="Account" id="thePage">
    <apex:form id="theForm" >
        <apex:pageBlock title="Editing Top 5 Account Priorities" mode="edit">
            <apex:messages />
                <apex:pageBlockButtons location="top">
                    <apex:commandButton value="Update Sorted Account Priorities" action="{!SaveAction}"/>
                    <apex:commandButton value="Cancel" action="{!Cancel}"/>
                </apex:pageBlockButtons>
            
                <apex:pageBlockTable value="{!actPList}" var="item">
                    <apex:column value="{!item.Account_Priorities__c}"/>
                    <apex:column headervalue="# (Top 5 Priorities)">
                        <apex:inputField value="{!item.Field1__c}"/>
                    </apex:column>
                </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

APEX Class

public with sharing class accountprioritysort1 {    

    public ApexPages.StandardController stdController {get; set;}
    public Account_Plan__c AccPri { get; set; }
    public list<Company_Priorities__c> actPList  { get; set; }
    public integer aField { get; set;}
    
    public accountprioritysort1 (ApexPages.StandardController stdController){
        System.debug('Is it in the Constructor');
        this.stdController = stdController;
        this.AccPri = (Account_Plan__c)this.stdController.getRecord();
        actPList = [select Field1__c,Account_Priorities__c from Company_Priorities__c where Account_Plan__c =: AccPri.Id
                ORDER BY Field1__c ASC ];
    }
    
    public PageReference SaveAction(){
        update actPList;
        PageReference page = new PageReference('/apex/AccountPrioritySort?id='+AccPri.id);
        page.setRedirect(true);
        return page;
    }
    
    public PageReference CancelAction(){
            PageReference page = new PageReference('/apex/AccountPrioritySort?id='+AccPri.id);
            page.setRedirect(true);
            return page;
        }
    }


/apex/AccountPrioritySort?id={!Account_Plan__c.Id}&type=AP

Now i want to pass this 


1. The value (which is as type=AP) from the Parameter Type in the Apex Controller

2. How can render by using the type variable to between Account Priorities and Risk in the VF page and Controller. This type should be used to decide what data needs to be saved i.e. should the risk code execute or Account priorities code execute.
 

Can any one help on this code.

 

Best Answer chosen by Ravi_SFDC
hitesh90hitesh90
Hello Venkata,

Please see below updated visualforce page code:

Visualforce page:
<apex:page standardController="Account_Plan__c" extensions="accountprioritysort1" tabStyle="Account" id="thePage">
    <apex:form id="theForm" >
        <apex:pageBlock title="Editing Top 5 Account Priorities" mode="edit">
            <apex:messages />
                <apex:pageBlockButtons location="top">
                    <apex:commandButton value="Update Sorted Account Priorities" action="{!SaveAction}"/>
                    <apex:commandButton value="Cancel" action="{!Cancel}"/>
                </apex:pageBlockButtons>
            
                <apex:pageBlockTable value="{!actPList}" var="item">
                    <apex:column value="{!item.Account_Priorities__c}"/>
                    <apex:column headervalue="# (Top 5 Priorities)">
                        <apex:inputField value="{!item.Field1__c}"/>
                    </apex:column>
                </apex:pageBlockTable>
        </apex:pageBlock>

        
        <apex:pageblock rendered="{!If(strType == 'AP', true, false)}" title="AP (type)">
            <apex:outputLabel value="This is AP"></apex:outputLabel>
        </apex:pageblock>
        
        <apex:pageblock rendered="{!If(strType == 'RS', true, false)}" title="RS (type)">
            <apex:outputLabel value="This is RS"></apex:outputLabel>
        </apex:pageblock>
    </apex:form>
</apex:page>

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/

All Answers

hitesh90hitesh90
Hello Venkata,

Try to use following code. you will get more idea.

Visualforce Page:
<apex:page standardController="Account_Plan__c" extensions="accountprioritysort1" tabStyle="Account" id="thePage">
    <apex:form id="theForm" >
        <apex:pageBlock title="Editing Top 5 Account Priorities" mode="edit">
            <apex:messages />
                <apex:pageBlockButtons location="top">
                    <apex:commandButton value="Update Sorted Account Priorities" action="{!SaveAction}"/>
                    <apex:commandButton value="Cancel" action="{!Cancel}"/>
                </apex:pageBlockButtons>
            
                <apex:pageBlockTable value="{!actPList}" var="item">
                    <apex:column value="{!item.Account_Priorities__c}"/>
                    <apex:column headervalue="# (Top 5 Priorities)">
                        <apex:inputField value="{!item.Field1__c}"/>
                    </apex:column>
                </apex:pageBlockTable>
        </apex:pageBlock>
        <apex:outputLabel value="Type value is :- {!strType}"></apex:outputLabel>
    </apex:form>
</apex:page>

Apex Class:
public with sharing class accountprioritysort1 {    

    public ApexPages.StandardController stdController {get; set;}
    public Account_Plan__c AccPri { get; set; }
    public list<Company_Priorities__c> actPList  { get; set; }
    public integer aField { get; set;}    
    public string strType {get; set;}
    
    public accountprioritysort1 (ApexPages.StandardController stdController){
        System.debug('Is it in the Constructor');
        this.stdController = stdController;
        this.AccPri = (Account_Plan__c)this.stdController.getRecord();
        actPList = [select Field1__c,Account_Priorities__c from Company_Priorities__c where Account_Plan__c =: AccPri.Id
                ORDER BY Field1__c ASC ];
                
        strType = ApexPages.currentPage().getParameters().get('type');
    }
    
    public PageReference SaveAction(){
        update actPList;
        PageReference page = new PageReference('/apex/AccountPrioritySort?id='+AccPri.id);
        page.setRedirect(true);
        return page;
    }
    
    public PageReference CancelAction(){
            PageReference page = new PageReference('/apex/AccountPrioritySort?id='+AccPri.id);
            page.setRedirect(true);
            return page;
        }
    }
Let me know if you have any question on this. Please mark this "Solved" if it helps.

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/


 
Ravi_SFDCRavi_SFDC

Hi Hitesh,

Thank you this worked, But the Type is displayed on the Visualforce Page. I do have 2 pageblocks one for AP (type) other for (RS) type. So I should be able to render using the type variable to between Account Priorities and Risk in the VF page and Controller. This type should be used to decide what data needs to be saved i.e. should the risk code execute or Account priorities code execute.

Can you help me with this code.

Ravi_SFDCRavi_SFDC

Hi Hitesh,

How can add the value of strType in the URL /apex/AccountPrioritySort?id='+AccPri.id after the ID is displayed.

public with sharing class accountprioritysort1 {    

    public ApexPages.StandardController stdController {get; set;}
    public Account_Plan__c AccPri { get; set; }
    public list<Company_Priorities__c> actPList  { get; set; }
    public string strType {get; set;}
    
    public accountprioritysort1 (ApexPages.StandardController stdController){
        System.debug('Is it in the Constructor');
        this.stdController = stdController;
        this.AccPri = (Account_Plan__c)this.stdController.getRecord();
        actPList = [select Field1__c,Account_Priorities__c from Company_Priorities__c where Account_Plan__c =: AccPri.Id
                ORDER BY Field1__c ASC ];
        strType = ApexPages.currentPage().getParameters().get('type');
    }
    
    public PageReference SaveAction(){
        update actPList;
        PageReference page = new PageReference('/apex/AccountPrioritySort?id='+AccPri.id);
        page.setRedirect(true);
        return page;
    }
    
    public PageReference CancelAction(){
            PageReference page = new PageReference('/apex/AccountPrioritySort?id='+AccPri.id);
            page.setRedirect(true);
            return page;
        }
    }
 

 

hitesh90hitesh90
Hello Venkata,

Please see below updated visualforce page code:

Visualforce page:
<apex:page standardController="Account_Plan__c" extensions="accountprioritysort1" tabStyle="Account" id="thePage">
    <apex:form id="theForm" >
        <apex:pageBlock title="Editing Top 5 Account Priorities" mode="edit">
            <apex:messages />
                <apex:pageBlockButtons location="top">
                    <apex:commandButton value="Update Sorted Account Priorities" action="{!SaveAction}"/>
                    <apex:commandButton value="Cancel" action="{!Cancel}"/>
                </apex:pageBlockButtons>
            
                <apex:pageBlockTable value="{!actPList}" var="item">
                    <apex:column value="{!item.Account_Priorities__c}"/>
                    <apex:column headervalue="# (Top 5 Priorities)">
                        <apex:inputField value="{!item.Field1__c}"/>
                    </apex:column>
                </apex:pageBlockTable>
        </apex:pageBlock>

        
        <apex:pageblock rendered="{!If(strType == 'AP', true, false)}" title="AP (type)">
            <apex:outputLabel value="This is AP"></apex:outputLabel>
        </apex:pageblock>
        
        <apex:pageblock rendered="{!If(strType == 'RS', true, false)}" title="RS (type)">
            <apex:outputLabel value="This is RS"></apex:outputLabel>
        </apex:pageblock>
    </apex:form>
</apex:page>

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/
This was selected as the best answer
hitesh90hitesh90
How can add the value of strType in the URL /apex/AccountPrioritySort?id='+AccPri.id after the ID is displayed.

see below Apex Class:
 
public with sharing class accountprioritysort1 {    

    public ApexPages.StandardController stdController {get; set;}
    public Account_Plan__c AccPri { get; set; }
    public list<Company_Priorities__c> actPList  { get; set; }
    public integer aField { get; set;}    
    public string strType {get; set;}
    
    public accountprioritysort1 (ApexPages.StandardController stdController){
        System.debug('Is it in the Constructor');
        this.stdController = stdController;
        this.AccPri = (Account_Plan__c)this.stdController.getRecord();
        actPList = [select Field1__c,Account_Priorities__c from Company_Priorities__c where Account_Plan__c =: AccPri.Id
                ORDER BY Field1__c ASC ];
                
        strType = ApexPages.currentPage().getParameters().get('type');
    }
    
    public PageReference SaveAction(){
        update actPList;
        PageReference page = new PageReference('/apex/AccountPrioritySort?id='+AccPri.id + '&type=RS');
        page.setRedirect(true);
        return page;
    }
    
    public PageReference CancelAction(){
            PageReference page = new PageReference('/apex/AccountPrioritySort?id='+AccPri.id + '&type=AP');
            page.setRedirect(true);
            return page;
        }
    }

 
Ravi_SFDCRavi_SFDC

Hi Hitesh,

As you are hardcoded the value &type=RS and &type=AP in both Save and Cancel actions. Would it not cause any issue. 

1. In this below code update actPList is updating the value but remaining on the same page. It should Save with Database.SaveResults are return to the previous page with Text msg as SUCCESS or FAILURE.

public PageReference SaveAction(){
        update actPList;
        PageReference page = new PageReference('/apex/AccountPrioritySort?id='+AccPri.id);
        page.setRedirect(true);
        return page;
    }


2. I should be able to render the page blocks with , where i am writing both Account Priorities and Risks in the same Apex Class and Visual Force Page. Can you please help me render the blocks using the type. So that based on the type it should excecute that query and visual force page code.​

Ravi_SFDCRavi_SFDC

My Visual Force Page

<apex:page standardController="Account_Plan__c" extensions="accountprioritysort1" tabStyle="Account" id="thePage">
    <apex:form id="theForm" >
        <!--Begin of Account Priority Block-->	
        <apex:pageBlock title="Sorting Account Priorities" mode="edit" rendered="{!If(strType == 'AP', true, false)}">
            	<apex:messages />
                	<apex:pageBlockButtons location="top">
                    	<apex:commandButton value="Update" action="{!SaveAction}"/>
                    	<apex:commandButton value="Cancel" action="{!Cancel}"/>
                	</apex:pageBlockButtons>
                	<apex:pageBlockTable value="{!actPList}" var="item">
	                   	<apex:column headervalue="#">
    	                    <apex:inputField value="{!item.Field1__c}" style="width:25px"/>
        	            </apex:column>
                        <apex:column value="{!item.Account_Priorities__c}"/>
                	</apex:pageBlockTable>
        	</apex:pageBlock>
        <!--End of Account Priority Block-->
        
		<!--Begin of Risks Block-->
           	<apex:pageBlock title="Sorting Risks" mode="edit" rendered="{!If(strType == 'RS', true, false)}">
            	<apex:messages />
                	<apex:pageBlockButtons location="top">
                    	<apex:commandButton value="Update" action="{!SaveAction}"/>
                    	<apex:commandButton value="Cancel" action="{!Cancel}"/>
                	</apex:pageBlockButtons>
            
                	<apex:pageBlockTable value="{!riskPList}" var="item">
                    	<apex:column headervalue="#">
                        	<apex:inputField value="{!item.Priority__c}" style="width:25px"/>
						</apex:column>
                    	<apex:column value="{!item.Name}"/>
                	</apex:pageBlockTable>
        	</apex:pageBlock>
		<!--End of Risks Block-->
    </apex:form>
</apex:page>


Partial Apex Class Page

Can you please check the below part on the APEX Code fo SaveAction. Requirement is that it should update the record values based on the strType and return back to the page and display error messages too.

public PageReference SaveAction(){
		if (strType=='AP'){
            update actPList;
        } else if (strType=='RS'){ 
            update riskPList;
        }
		Savepoint sp = Database.setSavepoint();
        try{
            SaveAction();
		}catch(Exception e){
            System.debug('H'+e);
            apexpages.addMessages(e);
            Database.rollback(sp);
            return null;
        }
        PageReference page = new PageReference('/apex/AccountPrioritySort?id='+AccountPlan.id +'&type='+strType);
    	page.setRedirect(true);
    	return page;
    }
 

The Complete APEX Class File

public with sharing class accountprioritysort1 {    

    public ApexPages.StandardController stdController {get; set;}
    public Account_Plan__c AccountPlan {get; set;}
    public list<Company_Priorities__c> actPList  {get; set;}
    public list<Risks__c> riskPList  {get; set;}
    public string strType {get; set;}
    public string type {get; set;}
    
    public accountprioritysort1 (ApexPages.StandardController stdController){
        System.debug('Is it in the Constructor');
        this.stdController = stdController;
        this.AccountPlan = (Account_Plan__c)this.stdController.getRecord();
        actPList = [select Field1__c,Account_Priorities__c from Company_Priorities__c where Account_Plan__c =: AccountPlan.Id
                ORDER BY Field1__c ASC ];
        riskPList = [select Name, Priority__c from Risks__c where Account_Plan__c =: AccountPlan.Id order by Priority__c ASC];
        strType = ApexPages.currentPage().getParameters().get('type');
    }
    
	public PageReference SaveAction(){
		if (strType=='AP'){
            update actPList;
        } else if (strType=='RS'){ 
            update riskPList;
        }
		Savepoint sp = Database.setSavepoint();
        try{
            SaveAction();
		}catch(Exception e){
            System.debug('H'+e);
            apexpages.addMessages(e);
            Database.rollback(sp);
            return null;
        }
        PageReference page = new PageReference('/apex/AccountPrioritySort?id='+AccountPlan.id +'&type='+strType);
    	page.setRedirect(true);
    	return page;
    }
}