Pages

Thursday, July 28, 2011

Visualforce: Hide the In-Line Editor

So you have developer mode on and every page that you visit displays the in-line editor at the bottom of the page. I love that thing, but as handy as it is, when I'm testing pages or trying to demo something for someone, it gets in the way.

Don't you wish there were an easy way to disable that?

Try appending this to your VF page name within your URL:
?core.apexpages.devmode.url=1

Tuesday, July 26, 2011

Visualforce: Render Page as .Doc

It's amazingly easy to create a VF page and have it open as a Word document. I've used this a handful of times when my VF page is a form and I want to offer a printable copy.

VF:
<apex:page contentType="application/msword">

However, this post isn't about that. It's about breaking up your content within that .Doc. Sometimes, you need different data in different sections of separate pages. Easy breezy, just include this every time you want a page break in your .doc file.

VF:
<br clear="all" style="page-break-before:always" />

Example: Setting and Retreiving Cookies

In this example, I'm setting a cookie with its value obtained from the "Name" input box when the user clicks on "Add Cookie." When the user clicks on "Show Cookie," the cookie is retrieved, the panel is refreshed, and the name should be displayed. When the user clicks on "Validate Cookie," the cookie's maxAge is looked at. If the cookie hasn't expired, the user is taken to an Apex page named, "not_expired" and alternately "expired" if the cookie is no longer valid.




VF:
<apex:outputPanel id="CookieTest">
   <apex:form >
      Name:<apex:inputtext value="{!Name}"/>
      <br />
      <apex:commandButton action="{!addtocookie}" value="Add to Cookie"/>
      <apex:commandButton action="{!ShowCookie}" value="ShowCookie" rerender="CookieTest"/>
      <apex:commandButton action="{!ValidateCookie}" value="ValidateCookie"/>
      <br />
      The Name set in the cookie is : {!NameAtCookie}<br />
      Max Age: {!MaxAge}
   </apex:form>
</apex:outputPanel>

Class:
public with sharing class testCookie {
   public string Name{get; set;}
   public string NameAtCookie{get; set;}
   public integer maxAge{get; set;}
   public testCookie(test controller) {
   }
   public Pagereference addToCookie(){
      InsertValueToCookie(Name);
      return null;
   }

   public Pagereference ValidateCookie(){
      SetCookieToNameAtCookies();
      if(NameAtCookie == 'expired'){
         pageReference pageRef = new pageReference('/apex/expired');
         return pageRef;
      }
      else{
         pageReference pageRef = new pageReference('/apex/not_expired');
         return pageRef;
      }
   }
   public Pagereference ShowCookie(){
      SetCookieToNameAtCookies();
      return null;
   }
   public void InsertValueToCookie(string val){
   /* create a new cookie with name 'counter', an initial value of Name at the page,
path 'null', maxAge '-1', and isSecure 'false'.*/
      Cookie myCookies=new Cookie('mycookie',val,null,-1,false);
      ApexPages.currentPage().setCookies(new Cookie[]{myCookies});
   }

   public void SetCookieToNameAtCookies(){
   /* reading the value of the cookie 'mycookie' , and insert him to 'NameAtCookie' field */
      Cookie myCookies = ApexPages.currentPage().getCookies().get('mycookie');
      if(myCookies != null){
         NameAtCookie=myCookies.getValue();
         maxAge=myCookies.getMaxAge();
      }
      else{
         NameAtCookie='expired';
         maxAge=null;
      }
   }
}

Friday, July 22, 2011

Quick Search the Apex and VF Deverloper's Guides

I find myself frequently accessing the HTML version of the Apex and VF Dev Guides. A simple shortcut wasn't cutting it anymore - I wanted to pop-open the page and search all in one step.

Within Firefox, create two bookmarks:
1) http://www.salesforce.com/us/developer/docs/pages/index.htm?%s
2) http://www.salesforce.com/us/developer/docs/apexcode/index.htm?%s

Right-click on the book marks, choose properties, and update the "Keyword" box with "vf" or "apex" accordingly.

Now within the URL bar of FF, you can type something like "apex outbound" or "vf commandbutton" to quickly pull up the reference material.




For Chrome, Jeff Douglas has an excellent tool, here: http://blog.jeffdouglas.com/force-com-utility-belt/

Clean Up Classes w/ Hard-Coded SF URLS

Whether you are building something for an internal VF page or an external site, you'll be instantiating instances of the pageReference class.  <-- Did I say that right?  May not have mentioned this, but I have no background in traditional coding, just what I've picked up through my SF work.

For a while, I found my self doing something like this:
pageReference pageRef = new pageReference('https://na4.salesforce.com/apex/mypagename');

Obviously, those links would break when pushed from a sandbox to production and I would have to modify the code.

To save time, I came up with this to prevent those changes:
String hostname = ApexPages.currentPage().getHeaders().get('Host');
pageReference pageRef = new pageReference ('https://'+hostname +'/yourpagenamehere’);
system.debug('pageRef was:  ';+ pageref);
return pageRef;

VF Pages and Emails: Dynamic Copyright Date

"Thanks for getting that e-mail out so quickly... but the footer still says 2008!"

I hate hearing that and luckily there is an easy way around it.  I can't tell you how many email templates or VF pages I have in my SF instance that had hard-coded dates.  The most frequent culprit was a duplicated VF email template with a standard copyright footer. 

Rather than hardcode something like:
Copyright ©2005-2011 Acme, LLC. All Rights Reserved

You could specify a floating year like this:
Copyright ©2005-{!year(today())} Acme, LLC. All Rights Reserved

Blog Purpose

I'm starting this blog (inspired by Jeff Douglas) to share some tips that I've picked up.  I began using Salesforce in 2007 when I began working for my current employer as a help desk analyst.  My role has shifted numerous times over the last few years and around mid-2010, I began working heavily with Salesforce.  I'd love to become one of the best (or most versatile) Salesforce admins in the area.

There probably won't be a lot of huge posts, detailing large projects, but just little tid-bits that might help out my fellow SF'ers.