+ Start a Discussion

Pre populate fields using apex code


I try to pre populate the "new contract" fields using this post: https://na6.salesforce.com/secur/forgotpassword.jsp?r=F0j6FwEhch5yAH5YV_T_3ArID6fs0T.iZQby4uyjseu93wK2_bIiNgdnh_t12R67


Basically what I did:



A visualforce page with this line only:


<apex:page standardController="Task" extensions="CustomEditContract" action="{!doCreate}"/>



And and apex class (CustomEditContract):


public class CustomEditContract { public CustomEditContract(ApexPages.StandardController controller) { } public PageReference doEdit() { /* Get the pagereference for the contract edit */ Contact contr = new Contact(); ApexPages.StandardController theC= new ApexPages.standardController(contr); PageReference p = theC.edit(); return p; } }

 The problem is that the edit() doesn't accepts an object without an ID.

I don't want to use S-controls or building the entire page using visualforce, but I didn't found any other solutions.

So, is there no way of pre populating a new object using apex and the StandardController?








In your page you call a docreate method and the code has a doEdit method. Is this simply a typing mistake? It's not completely clear what you're trying to do so I'm going to make some assumptions.. namely you're trying to redirect the user to the edit page but want them to use you page to get there? 


According to this post  there is no easy Apex way to implement the 'new' action. Sorry buddy. 






Hey Farid,

I have a similar requirement for the Case object.  We had to insert the new Case populating certain fields according to an internal logic before calling the edit() method.  It nicely display the edit page; however another problem arise, which is if the user selects cancel she/he would expect the Case object not to have been persisted.

So I am still in the same position as you; I do not want to define a new VF page from scratch, which means that I want to either be able to pass to the controller an object not yet persisted or have a way (possibly simple) to hookup an extension to a controller at run-time, something like (using a sort of delegation pattern):


ApexPages.StandardController theC= new ApexPages.standardController(new Case(), myControllerExtension); 


I would define in my controller extension logic in the cancel method to first delete the Case and then call the super cancel().


Please let me know if you found a solution to your problem.