Monday, September 30, 2013

SALESFORCE: Creating a button on object or VFpage to link to another page

FIRST SCENARIO: Creating button from custom object going to another URL
Step 1: 
      If Standard Object: Go to Setup>Customize>[sObject]>Buttons, Links, and Actions
      If Custom Object: Step 1: Go to Setup>Create>Objects>[custom object]> Buttons, Links, and Actions
Step 2: Click "New Button or Link"
Step 3: Fill in the necessary fields
      If you wanted to put the button on top of the page layout:
            Display Type: 'Detail Page Button'
      If you wanted to put the button on the related list of another object:
            Display Type: 'List Button'
      If you wanted it to be on the detail page:
            Display Type: 'Detail Page Link'
Step 4: Set Behavior to 'Execute JavaScript'
Step 5: Enter the code below:
      If you want to go to a VF page:
      top.location.href='apex/VFPage'
            where:      VFPage is the name of you Visualforce page
     
      Note: you can also put parameters here if you wanted to make your page dynamic.
      top.location.href='apex/VFPage?id={!Account.Id}'
            where: {!Account.Id} is an ID parameter
     
      If you want to go to an external site
      top.location.href='http://www.noobgrammers.blogspot.com'

SECOND SCENARIO: Creating a button from VFpage to go to another URL
Step 1: In your VF page, create a apex:CommandButton or apex:CommandLink
                  Example:
                  <apex:commandButton value="Go There" action="{!goThere}" />
Step 2: In you controller class, create a pageReference method named after the action, in this instance, it is called "goThere"
                  Example:
                  public pageReference goThere(){     
                        // Change current page to Location VF page
                        PageReference pgRef = Page.Location;
                        //you can also put parameters here (assuming you have a query of account, e.g. Id accVar = [SELECT Id FROM Account LIMIT 1].Id;
                        pgRef.getParameters().put('Id',accVar);
                        pgRef.setRedirect(true);
                        return pgRef;
                  }// End goThere Method