How to call the controller to VF page with parameter?I have two Visual force pages, and two controllers
VF Page:DEMO
<apex:page standardController="Account" extensions="NewAndExistingController" id="demoId" > <apex:pageBlock > <br> </br> <apex:outputField value="{!a.Id}"/> <br> </br> </apex:pageBlock> <apex:form > <apex:pageBlock > <apex:commandButton value="Call visualforce Page" action="{!click}"/> </apex:pageBlock> </apex:form> </apex:page>
VF page:XYZ
<apex:page standardController="Order__c" extensions="MyOrderPadController" > <apex:detail /> <apex:form > <apex:pageBlock > <apex:pageBlockButtons > <apex:commandButton action="{!save}" value="Save"/> <apex:commandButton action="{!edit}" value="Edit"/> <apex:commandButton action="{!reset}" value="Cancel" /> </apex:pageBlockButtons> <apex:pageBlockSection columns="2" title="Order Pad"> <apex:inputField value="{!Order__c.Order_Description__c}" /> <apex:inputField value="{!Order__c.Creat_Date__c}" /> <apex:inputField value="{!Order__c.Closed_Date__c}" /> <apex:inputField value="{!Order__c.Conformation__c}" /> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>
Controller:NewAndExistingController
public class NewAndExistingController { Public Account a{get;set;}public String accId; public NewAndExistingController(ApexPages.StandardController controller) { try{ a =[select id,name,accountnumber,annualrevenue from account where id=:controller.getId()]; MyOrderPadController.staticVar =a.Id; }catch(exception e){} } public PageReference click() { PageReference openvfpage = New Pagereference('/apex'+'/XYZ); openvfpage.setRedirect(false); return openvfpage ; } public NewAndExistingController() { } }
Controller: MyOrderPadController
public class MyOrderPadController { public Order__c order{ get; private set; } public static String staticVar; public String instanceVar {get; set;} NewAndExistingController test = new NewAndExistingController (); public MyOrderPadController () { instanceVar = staticVar; } public MyOrderPadController(ApexPages.StandardController sc) { order = (Order__c)sc.getRecord(); } public PageReference edit() { return null; } public PageReference reset() { PageReference newpage = new PageReference(System.currentPageReference().getURL()); newpage.setRedirect(true); return newpage; } public PageReference save() { TRY { order.Name = 'Demo Test Order '+order.Order_Description__c; order.Order_Description__c =order.Order_Description__c+':====>'+staticVar ; order.Account__c = '0019000000NAr7bAAD';//for testing only IF(order.Conformation__c == TRUE){ INSERT order; PageReference newpage = new PageReference(System.currentPageReference().getURL()); newpage.setRedirect(true); return newpage; } } catch(System.DMLException e) { ApexPages.addMessages(e); SYSTEM.DEBUG('ERROR ORDER PAD CONTROLLER :'+e); return null; } // After Save, navigate to the default view page: //return (new ApexPages.StandardController(order)).view(); return null; } }
The “DEMO” VF page added to Account under the one section ,As of now I am getting Account Id in the controller “NewAndExistingController” .This controller again called to the VF page “XYZ” .How to access this Account id in controller “MyOrderPadController”.Right now it is hard coded,Once it get I remved.
I want get account id in the “MyOrderPadController” Any idea please share with me.
Regards,
Ramesh
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
In NewAndExistingController try adding a.id to Pagereference
PageReference openvfpage = New Pagereference('/apex'+'/XYZ+'?aid='+a.id);
You can refer this in the other controller as
order.Account__c = ApexPages.currentPage().getParameters().get('aid');
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0