<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SaaSkatoon: All Things SaaS!&#187; VisualForce</title>
	<atom:link href="http://saaskatoon.deliveredinnovation.com/tag/visualforce/feed/" rel="self" type="application/rss+xml" />
	<link>http://saaskatoon.deliveredinnovation.com</link>
	<description>SaaS, PaaS, and Cloud Computing</description>
	<lastBuildDate>Sun, 22 Aug 2010 22:43:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Force.com Tip: &#8216;New&#8217; Button Override to Assign VisualForce Page to Specific Record Type Using Native Apex Code</title>
		<link>http://saaskatoon.deliveredinnovation.com/2009/08/09/force-com-tip-new-button-override-to-assign-visualforce-page-to-specific-record-type-using-native-apex-code/</link>
		<comments>http://saaskatoon.deliveredinnovation.com/2009/08/09/force-com-tip-new-button-override-to-assign-visualforce-page-to-specific-record-type-using-native-apex-code/#comments</comments>
		<pubDate>Mon, 10 Aug 2009 03:38:30 +0000</pubDate>
		<dc:creator>topalovich</dc:creator>
				<category><![CDATA[Cloud Computing]]></category>
		<category><![CDATA[Force.com]]></category>
		<category><![CDATA[Apex]]></category>
		<category><![CDATA[Salesforce]]></category>
		<category><![CDATA[Salesforce.com]]></category>
		<category><![CDATA[VisualForce]]></category>

		<guid isPermaLink="false">http://saaskatoon.deliveredinnovation.com/?p=389</guid>
		<description><![CDATA[<p><span style="color: #ff0000;">* Updated 8.14.09 with a link to a similar post by Jeff Douglas and refactored code that is more applicable to a broader audience using Apex PageReference methods rather than the original string concatenation that was used for human (non-Salesforce developer) readability</span></p> 
<p>You&#8217;ve probably come across this before&#8230;you need to create a custom VisualForce page for a specific Record Type, but overriding the &#8216;New&#8217; or &#8216;Edit&#8217; button seems to be an all-or-nothing proposition (i.e. you can do a single VisualForce page and embed the Page Layout using Apex:Detail, but you can&#8217;t mix custom pages with standard Page Layouts). We came across this recently at <a title="Force.com SaaS application design and development" href="http://www.deliveredinnovation.com/salesforce-force.com-application-development/"  target="_blank">Delivered Innovation</a>, and we want to share our solution with the community. This specific example involves the Saleforce Case object, but can be applied to any Standard or Custom Object.</p> 
<p><strong>Use Case:</strong> Client has multiple call centers supporting various product lines; support team &#8216;A&#8217; requires a custom Wizard to rapidly search for and collect details for the Case record that the standard Salesforce search interface cannot provide.</p> 
<p><strong>Solution:</strong> While you can assign a specific Page Layout to a specific Record Type and embed VisualForce pages in the object Detail view, currently salesforce.com does not support embedded VisualForce Pages in the Edit view of a Page Layout; likewise, salesforce.com does not support custom VisualForce pages for specific Record Types.  The solution is to &#8220;intercept&#8221; the command to create a new record in an Object before Salesforce processes it, and this is accomplished with a 1-line VisualForce page and a StandardController extension that pulls and analyzes certain URL parameters to enhance the out-of-the-box process routing capabilities of Salesforce.</p> 
<hr /> 
<h2>The VisualForce Page code:</h2> 
<p><code>&#60;apex:page standardController="Case" extensions="caseRedirect" tabStyle="Case" showheader="true" action="{!redirect}" /&#62;</code></p> 
<h2>The Apex controller extension:</h2> 
<p><code>public with sharing class caseRedirect {</code></p> 
<p><code> </code></p> 
<p><code>private ApexPages.StandardController controller;<br /> 
public String retURL {get; set;}<br /> 
public String saveNewURL {get; set;}<br /> 
public String rType {get; set;}<br /> 
public String cancelURL {get; set;}<br /> 
public String ent {get; set;}<br /> 
public String confirmationToken {get; set;}<br /> 
public String accountID {get; set;}<br /> 
public String contactID {get; set;}</p> 
<p>public caseRedirect(ApexPages.StandardController controller) {</p> 
<p>this.controller = controller;</p> 
<p>retURL = ApexPages.currentPage().getParameters().get('retURL');<br /> 
rType = ApexPages.currentPage().getParameters().get('RecordType');<br /> 
cancelURL = ApexPages.currentPage().getParameters().get('cancelURL');<br /> 
ent = ApexPages.currentPage().getParameters().get('ent');<br /> 
confirmationToken = ApexPages.currentPage().getParameters().get('_CONFIRMATIONTOKEN');<br /> 
saveNewURL = ApexPages.currentPage().getParameters().get('save_new_url');<br /> 
accountID = ApexPages.currentPage().getParameters().get('def_account_id');<br /> 
contactID = ApexPages.currentPage().getParameters().get('def_contact_id');</p> 
<p>}</p> 
<p>public PageReference redirect(){</p> 
<p>PageReference returnURL;</p> 
<p>// Redirect if Record Type corresponds to custom VisualForce page</p> 
<p>IF(rType == '<span style="color: #ff0000;">xxxxxxxxxxxxxxx</span>') {</p> 
<p>returnURL = new PageReference('/apex/<span style="color: #ff0000;">insert_VF_Page_Here</span>');<br /> 
}</p> 
<p>ELSE {</p> 
<p>returnURL = new PageReference('/500/e');</p> 
<p>}</p> 
<p>returnURL.getParameters().put('retURL', retURL);<br /> 
returnURL.getParameters().put('RecordType', rType);<br /> 
returnURL.getParameters().put('cancelURL', cancelURL);<br /> 
returnURL.getParameters().put('ent', ent);<br /> 
returnURL.getParameters().put('_CONFIRMATIONTOKEN', confirmationToken);<br /> 
returnURL.getParameters().put('save_new_url', saveNewURL);<br /> 
returnURL.getParameters().put('nooverride', '1');</p> 
<p>IF (accountID != null){</p> 
<p>returnURL.getParameters().put('def_account_id', accountID);</p> 
<p>}</p> 
<p>IF (contactID != null){</p> 
<p>returnURL.getParameters().put('def_contact_id', contactID);</p> 
<p>}</p> 
<p>returnURL.setRedirect(true);<br /> 
return returnURL;</p> 
<p>}</p> 
<p></code></p> 
<p><code>}</code></p> 
<h2 style="font-size: 1.5em;">Original code (prior to refactoring):</h2> 
<p><span style="font-family: -webkit-monospace; "><span style="text-decoration: line-through;">public with sharing class caseRedirect {</span></span></p> 
<p><code><span style="text-decoration: line-through;">private ApexPages.StandardController controller;<br /> 
public String retURL {get; set;}<br /> 
public String rType {get; set;}<br /> 
public String cancelURL {get; set;}<br /> 
public String ent {get; set;}<br /> 
public String confirmationToken {get; set;}</span></code></p> 
<p><code><span style="text-decoration: line-through;">public caseRedirect(ApexPages.StandardController controller) {</span></code></p> 
<p><code><span style="text-decoration: line-through;">this.controller = controller;</span></code></p> 
<p><code><span style="text-decoration: line-through;">retURL = ApexPages.currentPage().getParameters().get('retURL');<br /> 
rType = ApexPages.currentPage().getParameters().get('RecordType');<br /> 
cancelURL = ApexPages.currentPage().getParameters().get('cancelURL');<br /> 
ent = ApexPages.currentPage().getParameters().get('ent');<br /> 
confirmationToken = ApexPages.currentPage().getParameters().get('_CONFIRMATIONTOKEN');</span></code></p> 
<p><code><span style="text-decoration: line-through;">}</span></code></p> 
<p><code><span style="text-decoration: line-through;">public PageReference redirect(){</span></code></p> 
<p><code><span style="text-decoration: line-through;">PageReference returnURL;</span></code></p> 
<p><code><span style="text-decoration: line-through;">// Redirect if the Record Type with custom VF page has been selected</span></code></p> 
<p><code><span style="text-decoration: line-through;">// Note, rType represents 15-charater ID for Record Type</span></code></p> 
<p><code><span style="text-decoration: line-through;">IF(rType == '</span><span style="color: #ff6600;"><span style="text-decoration: line-through;">xxxxxxxxxxxxxxx</span></span><span style="text-decoration: line-through;">'){</span></code></p> 
<p><code><span style="text-decoration: line-through;"> </span></code></p> 
<p><code><span style="text-decoration: line-through;">returnURL = new PageReference('/apex/</span><span style="color: #ff6600;"><span style="text-decoration: line-through;">(insert VF page here)</span></span><span style="text-decoration: line-through;">' + '?' + 'retURL=' + retURL + '&#38;' + 'RecordType=' + rType + '&#38;' + 'cancelURL=' + cancelURL + '&#38;' +<br /> 
'ent=' + ent + '&#38;' + 'nooverride=1' + '&#38;' + '_CONFIRMATIONTOKEN=' + confirmationToken);</span></code></p> 
<p><code> </code></p> 
<p><code><span style="text-decoration: line-through;">}</span></code></p> 
<p><code><span style="text-decoration: line-through;">// Now we need to tell Salesforce what to do in the event that this is not the Record Type we're looking for</span></code></p> 
<p><code><span style="text-decoration: line-through;">ELSE{</span></code></p> 
<p><code><span style="text-decoration: line-through;"> </span></code></p> 
<p><code><span style="text-decoration: line-through;">returnURL = new PageReference('/500/e' + '?' + 'retURL=' + retURL + '&#38;' + 'RecordType=' + rType + '&#38;' + 'cancelURL=' + cancelURL + '&#38;' +<br /> 
'ent=' + ent + '&#38;' + 'nooverride=1' + '&#38;' + '_CONFIRMATIONTOKEN=' + confirmationToken);</span></code></p> 
<p><code> </code></p> 
<p><code><span style="text-decoration: line-through;">}</span></p> 
<p><span style="text-decoration: line-through;">returnURL.setRedirect(true);<br /> 
return returnURL;</span></p> 
<p><span style="text-decoration: line-through;">}</span></p> 
<p></code></p> 
<p><code><span style="text-decoration: line-through;">}</span></code></p> 
<p>Now for the important part &#8211; in order for this to work when Salesforce users create new Case records, you need to override the &#8216;New&#8217; button for the &#8216;Case&#8217; Standard Object.  This can be accomplished by selecting &#8216;Setup / Customize / Cases / Buttons and Links / &#8216;Override&#8217; on &#8216;New&#8217;.  Select &#8216;VisualForce Page&#8217; for &#8220;Content Type,&#8221; and then select the VisualForce page you created using your modification of the code above in the &#8220;Content Name&#8221; dropdown list, and you&#8217;re set.  The &#8220;Comment&#8221; area is optional. Don&#8217;t forget to set the security for this VisualForce page to provide the required Profiles with access.</p> 
<p>Jeff Douglas has provided another method for achieving automated redirection to VisualForce pages on his blog.  You can find the link <a href="http://blog.jeffdouglas.com/2008/11/14/redirecting-users-to-different-visualforce-pages">here</a>.</p> 
<p>Questions? Suggestions? Leave us a comment, or email me directly at blog[at]deliveredinnovation.com.</p> 
<p>Mike Topalovich, CTO<br /> 
<a title="Force.com application design" href="http://www.deliveredinnovation" target="_blank">Delivered Innovation</a></p>]]></description>
			<content:encoded><![CDATA[<p><span style="color: #ff0000;">* Updated 8.14.09 with a link to a similar post by Jeff Douglas and refactored code that is more applicable to a broader audience using Apex PageReference methods rather than the original string concatenation that was used for human (non-Salesforce developer) readability</span></p>
<p>You&#8217;ve probably come across this before&#8230;you need to create a custom VisualForce page for a specific Record Type, but overriding the &#8216;New&#8217; or &#8216;Edit&#8217; button seems to be an all-or-nothing proposition (i.e. you can do a single VisualForce page and embed the Page Layout using Apex:Detail, but you can&#8217;t mix custom pages with standard Page Layouts). We came across this recently at <a title="Force.com SaaS application design and development" href="http://www.deliveredinnovation.com/salesforce-force.com-application-development/" target="_blank">Delivered Innovation</a>, and we want to share our solution with the community. This specific example involves the Saleforce Case object, but can be applied to any Standard or Custom Object.</p>
<p><strong>Use Case:</strong> Client has multiple call centers supporting various product lines; support team &#8216;A&#8217; requires a custom Wizard to rapidly search for and collect details for the Case record that the standard Salesforce search interface cannot provide.</p>
<p><strong>Solution:</strong> While you can assign a specific Page Layout to a specific Record Type and embed VisualForce pages in the object Detail view, currently salesforce.com does not support embedded VisualForce Pages in the Edit view of a Page Layout; likewise, salesforce.com does not support custom VisualForce pages for specific Record Types.  The solution is to &#8220;intercept&#8221; the command to create a new record in an Object before Salesforce processes it, and this is accomplished with a 1-line VisualForce page and a StandardController extension that pulls and analyzes certain URL parameters to enhance the out-of-the-box process routing capabilities of Salesforce.</p>
<p><span id="more-389"></span></p>
<h2>The VisualForce Page code:</h2>
<p><code>&lt;apex:page standardController="Case" extensions="caseRedirect" tabStyle="Case" showheader="true" action="{!redirect}" /&gt;</code></p>
<h2>The Apex controller extension:</h2>
<p><code>public with sharing class caseRedirect {</code></p>
<p><code> </code></p>
<p><code>private ApexPages.StandardController controller;<br />
public String retURL {get; set;}<br />
public String saveNewURL {get; set;}<br />
public String rType {get; set;}<br />
public String cancelURL {get; set;}<br />
public String ent {get; set;}<br />
public String confirmationToken {get; set;}<br />
public String accountID {get; set;}<br />
public String contactID {get; set;}</code></p>
<p><code>public caseRedirect(ApexPages.StandardController controller) {</code></p>
<p><code> </code></p>
<p><code>this.controller = controller;</p>
<p>retURL = ApexPages.currentPage().getParameters().get('retURL');<br />
rType = ApexPages.currentPage().getParameters().get('RecordType');<br />
cancelURL = ApexPages.currentPage().getParameters().get('cancelURL');<br />
ent = ApexPages.currentPage().getParameters().get('ent');<br />
confirmationToken = ApexPages.currentPage().getParameters().get('_CONFIRMATIONTOKEN');<br />
saveNewURL = ApexPages.currentPage().getParameters().get('save_new_url');<br />
accountID = ApexPages.currentPage().getParameters().get('def_account_id');<br />
contactID = ApexPages.currentPage().getParameters().get('def_contact_id');</p>
<p>}</p>
<p>public PageReference redirect(){</p>
<p>PageReference returnURL;</p>
<p>// Redirect if Record Type corresponds to custom VisualForce page</p>
<p>IF(rType == '<span style="color: #ff0000;">xxxxxxxxxxxxxxx</span>') {</p>
<p>returnURL = new PageReference('/apex/<span style="color: #ff0000;">insert_VF_Page_Here</span>');<br />
}</p>
<p>ELSE {</p>
<p>returnURL = new PageReference('/500/e');</p>
<p>}</p>
<p>returnURL.getParameters().put('retURL', retURL);<br />
returnURL.getParameters().put('RecordType', rType);<br />
returnURL.getParameters().put('cancelURL', cancelURL);<br />
returnURL.getParameters().put('ent', ent);<br />
returnURL.getParameters().put('_CONFIRMATIONTOKEN', confirmationToken);<br />
returnURL.getParameters().put('save_new_url', saveNewURL);<br />
returnURL.getParameters().put('nooverride', '1');</p>
<p>IF (accountID != null){</p>
<p>returnURL.getParameters().put('def_account_id', accountID);</p>
<p>}</p>
<p>IF (contactID != null){</p>
<p>returnURL.getParameters().put('def_contact_id', contactID);</p>
<p>}</p>
<p>returnURL.setRedirect(true);<br />
return returnURL;</p>
<p>}</p>
<p></code></p>
<p><code>}</code></p>
<h2 style="font-size: 1.5em;">Original code (prior to refactoring):</h2>
<p><span style="font-family: -webkit-monospace; "><span style="text-decoration: line-through;">public with sharing class caseRedirect {</span></span></p>
<p><code><span style="text-decoration: line-through;">private ApexPages.StandardController controller;<br />
public String retURL {get; set;}<br />
public String rType {get; set;}<br />
public String cancelURL {get; set;}<br />
public String ent {get; set;}<br />
public String confirmationToken {get; set;}</span></code></p>
<p><code><span style="text-decoration: line-through;">public caseRedirect(ApexPages.StandardController controller) {</span></code></p>
<p><code><span style="text-decoration: line-through;">this.controller = controller;</span></code></p>
<p><code><span style="text-decoration: line-through;">retURL = ApexPages.currentPage().getParameters().get('retURL');<br />
rType = ApexPages.currentPage().getParameters().get('RecordType');<br />
cancelURL = ApexPages.currentPage().getParameters().get('cancelURL');<br />
ent = ApexPages.currentPage().getParameters().get('ent');<br />
confirmationToken = ApexPages.currentPage().getParameters().get('_CONFIRMATIONTOKEN');</span></code></p>
<p><code><span style="text-decoration: line-through;">}</span></code></p>
<p><code><span style="text-decoration: line-through;">public PageReference redirect(){</span></code></p>
<p><code><span style="text-decoration: line-through;">PageReference returnURL;</span></code></p>
<p><code><span style="text-decoration: line-through;">// Redirect if the Record Type with custom VF page has been selected</span></code></p>
<p><code><span style="text-decoration: line-through;">// Note, rType represents 15-charater ID for Record Type</span></code></p>
<p><code><span style="text-decoration: line-through;">IF(rType == '</span><span style="color: #ff6600;"><span style="text-decoration: line-through;">xxxxxxxxxxxxxxx</span></span><span style="text-decoration: line-through;">'){</span></code></p>
<p><code><span style="text-decoration: line-through;"> </span></code></p>
<p><code><span style="text-decoration: line-through;">returnURL = new PageReference('/apex/</span><span style="color: #ff6600;"><span style="text-decoration: line-through;">(insert VF page here)</span></span><span style="text-decoration: line-through;">' + '?' + 'retURL=' + retURL + '&amp;' + 'RecordType=' + rType + '&amp;' + 'cancelURL=' + cancelURL + '&amp;' +<br />
'ent=' + ent + '&amp;' + 'nooverride=1' + '&amp;' + '_CONFIRMATIONTOKEN=' + confirmationToken);</span></code></p>
<p><code> </code></p>
<p><code><span style="text-decoration: line-through;">}</span></code></p>
<p><code><span style="text-decoration: line-through;">// Now we need to tell Salesforce what to do in the event that this is not the Record Type we're looking for</span></code></p>
<p><code><span style="text-decoration: line-through;">ELSE{</span></code></p>
<p><code><span style="text-decoration: line-through;"> </span></code></p>
<p><code><span style="text-decoration: line-through;">returnURL = new PageReference('/500/e' + '?' + 'retURL=' + retURL + '&amp;' + 'RecordType=' + rType + '&amp;' + 'cancelURL=' + cancelURL + '&amp;' +<br />
'ent=' + ent + '&amp;' + 'nooverride=1' + '&amp;' + '_CONFIRMATIONTOKEN=' + confirmationToken);</span></code></p>
<p><code> </code></p>
<p><code><span style="text-decoration: line-through;">}</span></code></p>
<p><code><span style="text-decoration: line-through;">returnURL.setRedirect(true);<br />
return returnURL;</span></code></p>
<p><code> </code></p>
<p><code><span style="text-decoration: line-through;">}</span></p>
<p></code></p>
<p><code><span style="text-decoration: line-through;">}</span></code></p>
<p>Now for the important part &#8211; in order for this to work when Salesforce users create new Case records, you need to override the &#8216;New&#8217; button for the &#8216;Case&#8217; Standard Object.  This can be accomplished by selecting &#8216;Setup / Customize / Cases / Buttons and Links / &#8216;Override&#8217; on &#8216;New&#8217;.  Select &#8216;VisualForce Page&#8217; for &#8220;Content Type,&#8221; and then select the VisualForce page you created using your modification of the code above in the &#8220;Content Name&#8221; dropdown list, and you&#8217;re set.  The &#8220;Comment&#8221; area is optional. Don&#8217;t forget to set the security for this VisualForce page to provide the required Profiles with access.</p>
<p>Jeff Douglas has provided another method for achieving automated redirection to VisualForce pages on his blog.  You can find the link <a href="http://blog.jeffdouglas.com/2008/11/14/redirecting-users-to-different-visualforce-pages" target="_blank">here</a>.</p>
<p>Questions? Suggestions? Leave us a comment, or email me directly at blog[at]deliveredinnovation.com.</p>
<p>Mike Topalovich, CTO<br />
<a title="Force.com application design" href="http://www.deliveredinnovation" target="_blank">Delivered Innovation</a></p>
]]></content:encoded>
			<wfw:commentRss>http://saaskatoon.deliveredinnovation.com/2009/08/09/force-com-tip-new-button-override-to-assign-visualforce-page-to-specific-record-type-using-native-apex-code/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
