Standard new/edit page Vs your Visualforce Page

8:05 AM

I just found this stuff to be very exciting and thought would be worth sharing...

I was reading through the forums and found a particular post.. the description is like this..

The "New" button has to be overridden with a Visualforce Page. When the Visualforce page loads some calculations are performed and the user is either redirected to the standard new/edit page or he stays in the same Visualforce page... And the interesting part was the standard new/edit page has to be prefilled with data passed from the visualforce page...

This is how it can be done.. We will take Account as an example.. We will override the Account new button with a visualforce page..

I will create a Visualforce page.. Just a small one

<apex:page standardController="Account" extensions="newvsold" action="{!pageredir}">

</apex:page>


And the Apex class would be..

public class newvsold
{

public newvsold(ApexPages.StandardController controller)
{

}
public Pagereference pageredir()
{
Pagereference newpage = new Pagereference ('/001/e?retURL=/001/');
return newpage;
}
}


So go ahead and override your "new" button with the Visualforce page you just created.. Now when you click on "New" button under Accounts what do you observe..

Yes the same old Account new page is displayed.. It is because we call a method in the "action" attribute of the page tag..(If you are unclear about the "action" attribute then read this article here ).. this method redirects the user to the normal new page.. Now let's analyze the URL we have specified in the Pagerefernce...

/001/e?retURL=/001/ --- The number 001 indicates Account, e indicated edit and retURL specifies where to go after hitting any button on the page.. Like 001 for Account all objects have a number.. You can always check it in the URL bar..

I simply did a redirection.. you can add any complex logic there...

Now going to the interesting part.. How will i prepopulate my new page with values.... Here it is

Go to the Account "new" page...



Right-click and select "View Source" or "View Page Source".... and in the window that appears search for "Account Name"...




Now after the place where you see "Account Name" look for "<input" and note the "id"


In our specific case the id is "acc2" which denotes Account Name....

In the url bar just type ... https://yoursalesforceinstance.com/001/e?acc2=test&retURL=/001/

Or you can modify the Apex Code as below...

public class newvsold
{

public newvsold(ApexPages.StandardController controller)
{

}
public Pagereference pageredir()
{
Pagereference newpage = new Pagereference ('/001/e?acc2=test&retURL=/001/');
return newpage;
}
}


"acc2" denotes Account Name.. similarly all fields have own id... and you can pass values for as many fields as you like unless you do not exceed the maximum URL size...

Even though this sounds a very strange method.. i just thought it might help someone somewhere..

15 comments

  1. Hi There,

    Can we use the same methodolgy on custom objects?

    Regards
    Mukul

    ReplyDelete
  2. Sure Mukul.. You can use them on custom objects as well.. Not sure if this would be a best method though

    ReplyDelete
  3. Hi, I get this to loop around.
    I call a VF page, which redirects me to the (new) page which is overridden then navigates me back to the VF page etc. etc.
    How do I break the loop?
    Regards
    Kaja

    ReplyDelete
  4. Hi.. try using the "nooverride" parameter in the URL... a value of 1 specifies that the override should not be considered i believe

    ReplyDelete
  5. I use something like this to override the contact New page with a VF page. We have a huge DB and duplicates are a menace. Using a VF page, I force the user to enter some basic contact info (name, email and account) and search for dupes. If found, the user just clicks and skips contact creation. If not, the next button goes to the actual SF standard create new page. I use the nooverride=1 parameter to stop the loop back. FYI, if you are using recrodtypes, I have found that you will need to capture the user input from the URl and pass it along to the create new page from the VF page

    ReplyDelete
  6. Hi,
    I am a new follower of your blog. I have a problem that I have not been able to solve, I wonder if you have any experience on this.

    I would like to pass certain data from a Custom Object A to another Custom Object B. Previously I use the URL method to pass data which is not a problem.

    Now I am replacing the Custom Object B as a Visualforce Page and could re-create the Page successfully and using Apex class extension to select the relevant data from Custom Object A to pass to Custom Object B. However it does not work. I would really appreciate for some pointer on how to solve this. Would it help if I send to you the codes i used for both the VF and apex Class?

    Looking forward to your kind reply
    Thanks and Best Regards
    SL

    ReplyDelete
  7. Hi,
    First of all a big thanks for following my blog!!!

    For your problem, i think you can pass the parameters as usual through the URL.. http://salesforceexperts.blogspot.com/2009/06/passing-parameters-to-visualforce-page.html may be helpful...

    You could then fetch the parameters in your apex class and process...

    If you are still not clear or would like further information, feel free to drop me a mail at success.salesforce@gmail.com...

    Best Regards,
    Edwin

    ReplyDelete
  8. Do you have a testmethod for this?
    I am so lost with my testmethod .. I have post it up on the Apex board here is the link:
    http://community.salesforce.com/sforce/board/message?board.id=apex&thread.id=23824&view=by_date_ascending&page=2

    Any help is appreciated...
    Happy Holidays
    Thanks

    ReplyDelete
  9. Is there a way to create a visual force page to accept data into custom objects for anonymous user? I mean a web-to-lead kind of a form for custom objects.
    Is it possible???

    ReplyDelete
  10. Yes.. This is possible.. There is a free project called Web2anything Click here ... View this article to find more resources... Hope it helps !!

    ReplyDelete
  11. Hi,
    I've read the posts and they are very interesting, but I'm still not enougth satisfied. If I want to create my custom page using an extended controller I can leverage the standard view using the "apex:detail" tag. Why isn't there anything analogous to that for the new/edit page?

    If you know the answer, please answer to my mail (vinicius.paiva@gmail.com)

    ReplyDelete
  12. how I use it for custom object?

    ReplyDelete
  13. I have replicated the same thing in my Developer sandbox.
    The problem i am facing is that the URL that i have provided in extension contrller is not changing once i am clicking some other tab.
    Kindly help on this issue.

    ReplyDelete
  14. Hi this is Puneet Srivastava.i have a question that how can i make a visualforce page just same as chatter FIles. if i want to save Images in versions form then ple tell me hows it possible do u have any helping code for it then pls send it to me as soon as possible.i need ur help.i wanna make same visualforce page like FIles then call the Images on our ecternal Application but First i need to make the same Visual Force page as Chatter FIles .....hoows it possible.....pls help me out ..

    ReplyDelete
  15. Hi this is Puneet Srivastava.i have a question that how can i make a visualforce page just same as chatter FIles. if i want to save Images in versions form then ple tell me hows it possible do u have any helping code for it then pls send it to me as soon as possible.i need ur help.i wanna make same visualforce page like FIles then call the Images on our ecternal Application but First i need to make the same Visual Force page as Chatter FIles .....hoows it possible.....pls help me out ..

    ReplyDelete

AD