Wednesday, December 30, 2009

Override URL and Button through Apex And Visual Force

Hi Welcome back...
I have been soo busy but promise to put up some new and neat things in 2010.
So lets start:
As you may know S-Controls are going away in the year 2010. It was very simple to override an URL and button using S-Controls.
But I have figured out a neat and simple way to do it using Apex and VisualForce.

Scenario:
Override the New Button for Opportunity, from contact and pre-populating the field in the URL pushing it forward to Opportunity ContactName field.

Beginner's Steps:
  1. Get the field ID of the contact Lookup Field in Opportunity. Click on: Setup==>Customize==>Opportunities==>Fields.
  2. Look for the lookup Relationship field to Contact (basically in the


    Opportunity Custom Fields & Relationships Lookup for DataType which would state Lookup(Contact)).




  3. Click on it and get the ID from the URL. It would be something like this:https://tapp0.salesforce.com/00N70000002RFSE The last part of the URL is the salesforce ID that is needed for the Controller and URL we need.
  4. Also we need to know the prefixes for the New Opportunity URL and existing Contact URL. By default the New Opportunity will start with /006/e? and the existing or new Contact will start with /003/e?.
  5. Now we shall place all of this in the controller.
  6. Then the page in which we will call the controller
  7. Override the New Button in the Opportunity Button and Links section.
Lets Start with the Apex Controller:

// The Name of the class
public class OverrideOppButton{

// Create a variable to get the Contact and set the variable
Contact c {get; set;}

//Use a boolean to determine if there are contact records available and set it to false initially
boolean gotcontactrecords = false;

//Open up the constructor to get the contact records from the current URL
public OverrideOppButton(ApexPages.StandardController stdController){

// Create a variable to return the current Contact URL;
string ret;
ret = ApexPages.currentPage().getParameters().get('RetURL');
//This will pick up the URL on the current contact record
//For example: https://tapp0.salesforce.com/003T000000MFRcS
//We need to separate the 003 which is the contact record and the T000000MFRcS which is the ID of //the contact record

ret = ret.substring(1,ret.length());

if(ret.startswith('003')) {
//if the substring of the URL starts with '003' which it does '003T000000MFRcS'
//Then query the database for that record from the contact

    c = [Select name, lastname, firstname, id from Contact where id =: ret];

//if they are records then set the gotcontactrecords to true
    gotcontactrecords = true;
 }
} // end constructor


/*
Here we start the PageReference Class which will tell the URL of what to
place into it
*/
public PageReference init(){

/*
We need to specify where the URL will redirect after the New
Button is clicked
*/

//Define the variable first
string redirectURL = '';
/*
Place in if then logic if there is a contact record then place in the url parameters and
redirect the user to the new Opportunity Page
if not then redirect them to a new Opportunity Page
with no parameter in the URL
*/
if (gotcontactrecords ) {
    redirectURL = '/006/e?CF003T000000MFRcS =
     ' + EncodingUtil.urlEncode(c.name, 'UTF-8') + '
    &CF003T000000MFRcS_lkid=' + c.id + '
    &nooverride=1';
}
/*
Explanation of the RedirectURL:
/006/ <== Creating New Opportunity Record
e?      <== What parameters need to be inputed.
CFT000000MFRcS <== 'CF' is the custom field which is the lookup Contact field in
                                               the Opportunity Object. field id is T000000MFRcS.
EncodingUtil.urlEncode(c.name, 'UTF-8')<== Takes in symbols associated with the name
                                                                             For example: Haaris & Benny
nooverride=1 <== Need when one overrides the new button.
*/
else {
    redirectURL = '/006/e?nooverride=1';
}

/*
Set the Page Reference of what URL will it take if it passes the criteria mentioned above
*/
PageReference createnewOpp = new PageReference(redirectURL);
return createnewOpp;
}

} //end class
*************************************************************************
Create the VisualForce Page:
This page is very simple.
  • Give the page a new which will be used when you override the New Button on the Opportunity
  • Place the initiative in the first tag of Visual Force
  • standardController = "Opportunity" which takes the Look & feel of the Opportunity Page
  • extensions = "OverrideOppButton" which places in the class/controller we just created.
  • action = "{!init}" which calls the PageReference and tells what the URL should be for the user.

apex:page standardController="Opportunity" extensions="OverrideOppButton" action="{!init}"
Do not forget the "<" and "/>" in this statement and your closing apex:page tag

*************************************************************************
Now go to Setup==> Customize ==> Opportunities ==> Button and Links
Click on Override next to the New Button
Click on the radio button which states Visualforce Page for Content Type selection
Drop down the Content Name and you should see the name of your Visualforce Page
which you just created.

That is it, your done.
Please leave your comments below.
Thanks



1 comment:

  1. Here is a silly question. Suppose you do override the new button. But if that New button was invoked from a Master relationship related list (i.e. Account to Custom Object A), you want to KNOW what that Account was so that you don't do certain actions, how do you get that value for use in your VF APEX? Thanx!

    ReplyDelete