<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Invoke Apex from a Custom Button using a Visualforce Page</title>
	<atom:link href="http://sfdc.arrowpointe.com/2009/01/08/invoke-apex-from-a-custom-button-using-a-visualforce-page/feed/" rel="self" type="application/rss+xml" />
	<link>http://sfdc.arrowpointe.com/2009/01/08/invoke-apex-from-a-custom-button-using-a-visualforce-page/</link>
	<description>Authored by Scott Hemmeter of Arrowpointe Corp, this blog is written from the perspective of a Salesforce.com solution provider and contains information on Arrowpointe's AppExchange products as well as tips, findings, sample code, functionality wishes, etc.</description>
	<lastBuildDate>Fri, 12 Mar 2010 21:54:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Scott Hemmeter</title>
		<link>http://sfdc.arrowpointe.com/2009/01/08/invoke-apex-from-a-custom-button-using-a-visualforce-page/comment-page-1/#comment-62804</link>
		<dc:creator>Scott Hemmeter</dc:creator>
		<pubDate>Thu, 18 Feb 2010 17:39:29 +0000</pubDate>
		<guid isPermaLink="false">http://sfdc.arrowpointe.com/?p=525#comment-62804</guid>
		<description>@Andy, the blog is active.  It&#039;s been 6 months since a comment on this post, but the blog itself has received comments as recently as today.

The issue is in your pageRef line.  First of all, put the pageReference lines above your Event currentEvent lines.  Second, you need to create a reference to your Visualforce page.  Should be something along the lines of:

&lt;code&gt;
PageReference pageRef = Page.yourVFPageName;
pageRef.getParameters().put(&#039;id&#039;, String.valueOf(currentEvent.ID));
Test.setCurrentPage(pageRef);
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>@Andy, the blog is active.  It&#8217;s been 6 months since a comment on this post, but the blog itself has received comments as recently as today.</p>
<p>The issue is in your pageRef line.  First of all, put the pageReference lines above your Event currentEvent lines.  Second, you need to create a reference to your Visualforce page.  Should be something along the lines of:</p>
<p><code><br />
PageReference pageRef = Page.yourVFPageName;<br />
pageRef.getParameters().put('id', String.valueOf(currentEvent.ID));<br />
Test.setCurrentPage(pageRef);<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andy Forbes</title>
		<link>http://sfdc.arrowpointe.com/2009/01/08/invoke-apex-from-a-custom-button-using-a-visualforce-page/comment-page-1/#comment-62801</link>
		<dc:creator>Andy Forbes</dc:creator>
		<pubDate>Thu, 18 Feb 2010 14:41:43 +0000</pubDate>
		<guid isPermaLink="false">http://sfdc.arrowpointe.com/?p=525#comment-62801</guid>
		<description>I&#039;m having the same problem as BrianP. I can&#039;t seem to find a way to get this statement in the controller:

String theId = ApexPages.currentPage().getParameters().get(&#039;id&#039;);

To return anything but null.

I see that the last comment on this blog was half a year ago :-), but if you&#039;re still checking it any insight would be greatly appreciated.

Test code:

@isTest
private class CancelEventTest {

    static testMethod void CETest() {
    	
    	Profile currentProfile = [select id from profile where name=&#039;System Administrator&#039;]; 
		Userrole currentRole = [select id from UserRole where Name = &#039;Administration&#039;];
        User currentUser = new User(alias = &#039;admin&#039;, email=&#039;admin@testorg.com&#039;, 
            emailencodingkey=&#039;UTF-8&#039;, lastname=&#039;Testing&#039;, languagelocalekey=&#039;en_US&#039;, 
            localesidkey=&#039;en_US&#039;, profileid = currentProfile.Id, 
            timezonesidkey=&#039;America/Los_Angeles&#039;, username=&#039;admin@testorg.com&#039;,
            userroleid = currentRole.Id);

        Event currentEvent = new Event(ownerid=currentUser.Id, subject=&#039;Test&#039;, 
        	startdatetime=System.Now(), enddatetime=System.Now());
 
 		ApexPages.StandardController sc = new ApexPages.StandardController(currentEvent);
        CancelEventController CETestController = new CancelEventController(sc);
 
        PageReference pageRef = new PageReference(&#039;/&#039; + currentEvent.ID);
        Test.setCurrentPage(pageRef);
 
	    CETestController.autorun();

    }
}</description>
		<content:encoded><![CDATA[<p>I&#8217;m having the same problem as BrianP. I can&#8217;t seem to find a way to get this statement in the controller:</p>
<p>String theId = ApexPages.currentPage().getParameters().get(&#8216;id&#8217;);</p>
<p>To return anything but null.</p>
<p>I see that the last comment on this blog was half a year ago <img src='http://sfdc.arrowpointe.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> , but if you&#8217;re still checking it any insight would be greatly appreciated.</p>
<p>Test code:</p>
<p>@isTest<br />
private class CancelEventTest {</p>
<p>    static testMethod void CETest() {</p>
<p>    	Profile currentProfile = [select id from profile where name='System Administrator'];<br />
		Userrole currentRole = [select id from UserRole where Name = 'Administration'];<br />
        User currentUser = new User(alias = &#8216;admin&#8217;, email=&#8217;admin@testorg.com&#8217;,<br />
            emailencodingkey=&#8217;UTF-8&#8242;, lastname=&#8217;Testing&#8217;, languagelocalekey=&#8217;en_US&#8217;,<br />
            localesidkey=&#8217;en_US&#8217;, profileid = currentProfile.Id,<br />
            timezonesidkey=&#8217;America/Los_Angeles&#8217;, username=&#8217;admin@testorg.com&#8217;,<br />
            userroleid = currentRole.Id);</p>
<p>        Event currentEvent = new Event(ownerid=currentUser.Id, subject=&#8217;Test&#8217;,<br />
        	startdatetime=System.Now(), enddatetime=System.Now());</p>
<p> 		ApexPages.StandardController sc = new ApexPages.StandardController(currentEvent);<br />
        CancelEventController CETestController = new CancelEventController(sc);</p>
<p>        PageReference pageRef = new PageReference(&#8216;/&#8217; + currentEvent.ID);<br />
        Test.setCurrentPage(pageRef);</p>
<p>	    CETestController.autorun();</p>
<p>    }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Osama&#39;s Weblog &#187; Invoke APEX code from custom button</title>
		<link>http://sfdc.arrowpointe.com/2009/01/08/invoke-apex-from-a-custom-button-using-a-visualforce-page/comment-page-1/#comment-62694</link>
		<dc:creator>Osama&#39;s Weblog &#187; Invoke APEX code from custom button</dc:creator>
		<pubDate>Mon, 11 Jan 2010 19:32:33 +0000</pubDate>
		<guid isPermaLink="false">http://sfdc.arrowpointe.com/?p=525#comment-62694</guid>
		<description>[...] is a really good example of invoking apex code through apex code here. But I have found another way to invoke the apex method without using visualforce [...]</description>
		<content:encoded><![CDATA[<p>[...] is a really good example of invoking apex code through apex code here. But I have found another way to invoke the apex method without using visualforce [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: &#187; Sites &#38; Visualforce: Some early lessons (The Enforcer.net, a force.com blog)</title>
		<link>http://sfdc.arrowpointe.com/2009/01/08/invoke-apex-from-a-custom-button-using-a-visualforce-page/comment-page-1/#comment-61924</link>
		<dc:creator>&#187; Sites &#38; Visualforce: Some early lessons (The Enforcer.net, a force.com blog)</dc:creator>
		<pubDate>Tue, 28 Jul 2009 12:10:10 +0000</pubDate>
		<guid isPermaLink="false">http://sfdc.arrowpointe.com/?p=525#comment-61924</guid>
		<description>[...] trigger field updates via a link , look at Scott Hemmeter&#8217;s article on how to Invoke Apex from a Custom Button using a Visualforce Page [...]</description>
		<content:encoded><![CDATA[<p>[...] trigger field updates via a link , look at Scott Hemmeter&#8217;s article on how to Invoke Apex from a Custom Button using a Visualforce Page [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Invoking Apex from a Button (JS &#8211;&#62; Apex Web Service) &#124; Perspectives on Salesforce.com</title>
		<link>http://sfdc.arrowpointe.com/2009/01/08/invoke-apex-from-a-custom-button-using-a-visualforce-page/comment-page-1/#comment-61874</link>
		<dc:creator>Invoking Apex from a Button (JS &#8211;&#62; Apex Web Service) &#124; Perspectives on Salesforce.com</dc:creator>
		<pubDate>Mon, 13 Jul 2009 16:54:21 +0000</pubDate>
		<guid isPermaLink="false">http://sfdc.arrowpointe.com/?p=525#comment-61874</guid>
		<description>[...] January I posted about how to invoke Apex from a Custom Button using a Visualforce Page.  It has been a popular post and is a topic which is of interest to many developers.  I wanted to [...]</description>
		<content:encoded><![CDATA[<p>[...] January I posted about how to invoke Apex from a Custom Button using a Visualforce Page.  It has been a popular post and is a topic which is of interest to many developers.  I wanted to [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Craig Harris</title>
		<link>http://sfdc.arrowpointe.com/2009/01/08/invoke-apex-from-a-custom-button-using-a-visualforce-page/comment-page-1/#comment-61873</link>
		<dc:creator>Craig Harris</dc:creator>
		<pubDate>Mon, 13 Jul 2009 15:53:28 +0000</pubDate>
		<guid isPermaLink="false">http://sfdc.arrowpointe.com/?p=525#comment-61873</guid>
		<description>I appear to have solved this a different way.

1) Create a controller extension (in my case Account)
&lt;blockquote&gt;
public with sharing class AccountControllerExtension {

   private final Account acct;

   public AccountControllerExtension(ApexPages.StandardController stdController) {
      this.acct = (Account)stdController.getRecord();
   }
   
   public PageReference doAction() {
      // Call other Apex classes to perform action
      ...
      // Return a page ref
      PageReference customPage = new PageReference(&#039;/apex/NewPage&#039;);
      customPage.getParameters().put(&#039;id&#039;, acct.Id);
      return customPage;
   }
}
&lt;/blockquote&gt;

2) Create a page that overrides the standard view
&lt;blockquote&gt;

  
    
  
  

&lt;/blockquote&gt;

3) Override the Account view &lt;strong&gt;Setup -&gt; Customize -&gt; Account -&gt; Buttons and Links&lt;/strong&gt;

4) Add a custom button (Execute Javascript) with the following body
&lt;blockquote&gt;doAction();&lt;/blockquote&gt;

How it works:

	The default Action view is replace with my custom page that is identical to the original with the exception of the hidden form with the hidden actionFunction and the inclusion of the controller extension
	When the custom button is pressed it invokes the actionFunction which in turn invokes the action on the controller extension.
	The action on the controller returns a PageReference (with redirect attribute set) to direct the user to a new screen, or back to the current screen using ApexPages.currentPage()
</description>
		<content:encoded><![CDATA[<p>I appear to have solved this a different way.</p>
<p>1) Create a controller extension (in my case Account)</p>
<blockquote><p>
public with sharing class AccountControllerExtension {</p>
<p>   private final Account acct;</p>
<p>   public AccountControllerExtension(ApexPages.StandardController stdController) {<br />
      this.acct = (Account)stdController.getRecord();<br />
   }</p>
<p>   public PageReference doAction() {<br />
      // Call other Apex classes to perform action<br />
      &#8230;<br />
      // Return a page ref<br />
      PageReference customPage = new PageReference(&#8216;/apex/NewPage&#8217;);<br />
      customPage.getParameters().put(&#8216;id&#8217;, acct.Id);<br />
      return customPage;<br />
   }<br />
}
</p></blockquote>
<p>2) Create a page that overrides the standard view</p>
<blockquote>
</blockquote>
<p>3) Override the Account view <strong>Setup -&gt; Customize -&gt; Account -&gt; Buttons and Links</strong></p>
<p>4) Add a custom button (Execute Javascript) with the following body</p>
<blockquote><p>doAction();</p></blockquote>
<p>How it works:</p>
<p>	The default Action view is replace with my custom page that is identical to the original with the exception of the hidden form with the hidden actionFunction and the inclusion of the controller extension<br />
	When the custom button is pressed it invokes the actionFunction which in turn invokes the action on the controller extension.<br />
	The action on the controller returns a PageReference (with redirect attribute set) to direct the user to a new screen, or back to the current screen using ApexPages.currentPage()</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott Hemmeter</title>
		<link>http://sfdc.arrowpointe.com/2009/01/08/invoke-apex-from-a-custom-button-using-a-visualforce-page/comment-page-1/#comment-61827</link>
		<dc:creator>Scott Hemmeter</dc:creator>
		<pubDate>Fri, 12 Jun 2009 14:32:51 +0000</pubDate>
		<guid isPermaLink="false">http://sfdc.arrowpointe.com/?p=525#comment-61827</guid>
		<description>@BrianP,

I&#039;d have to see your code to know for sure.  One thing I do know is that you should be creating your custom object data in your test class, not selecting existing data.  If your autoRun code handles many different data scenarios, create custom object data in your test class to accommodate each and call autorun for each record.

If you want to, you can &lt;a href=&quot;http://www.arrowpointe.com/support&quot; rel=&quot;nofollow&quot;&gt;log a case&lt;/a&gt; and show me your code and we can go from there.</description>
		<content:encoded><![CDATA[<p>@BrianP,</p>
<p>I&#8217;d have to see your code to know for sure.  One thing I do know is that you should be creating your custom object data in your test class, not selecting existing data.  If your autoRun code handles many different data scenarios, create custom object data in your test class to accommodate each and call autorun for each record.</p>
<p>If you want to, you can <a href="http://www.arrowpointe.com/support" rel="nofollow">log a case</a> and show me your code and we can go from there.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: BrianP</title>
		<link>http://sfdc.arrowpointe.com/2009/01/08/invoke-apex-from-a-custom-button-using-a-visualforce-page/comment-page-1/#comment-61826</link>
		<dc:creator>BrianP</dc:creator>
		<pubDate>Fri, 12 Jun 2009 13:58:20 +0000</pubDate>
		<guid isPermaLink="false">http://sfdc.arrowpointe.com/?p=525#comment-61826</guid>
		<description>Using you code examples above i am haivng trouble getting the test method to work correctly.  When i run the test method the code coverage is very low, because the test method runs selects one row, but then the autorun() method returns a null value.  

Any Ideas


static testmethod void TestCtrBn()
	{
	Custom_Object__c cn = [select ID from Custom_Object__c where Custom_Object_Number__c = &#039;1234&#039;];

	ApexPages.StandardController sc = new ApexPages.StandardController(cn);
	VFConvertTrade controller = new VFConvertTrade(sc);

	controller.autoRun();</description>
		<content:encoded><![CDATA[<p>Using you code examples above i am haivng trouble getting the test method to work correctly.  When i run the test method the code coverage is very low, because the test method runs selects one row, but then the autorun() method returns a null value.  </p>
<p>Any Ideas</p>
<p>static testmethod void TestCtrBn()<br />
	{<br />
	Custom_Object__c cn = [select ID from Custom_Object__c where Custom_Object_Number__c = '1234'];</p>
<p>	ApexPages.StandardController sc = new ApexPages.StandardController(cn);<br />
	VFConvertTrade controller = new VFConvertTrade(sc);</p>
<p>	controller.autoRun();</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DavidPSG</title>
		<link>http://sfdc.arrowpointe.com/2009/01/08/invoke-apex-from-a-custom-button-using-a-visualforce-page/comment-page-1/#comment-61476</link>
		<dc:creator>DavidPSG</dc:creator>
		<pubDate>Tue, 17 Mar 2009 18:15:13 +0000</pubDate>
		<guid isPermaLink="false">http://sfdc.arrowpointe.com/?p=525#comment-61476</guid>
		<description>Scott:

Great!  That solved the instantiation problem.
Thanks again for taking the time to help me out!</description>
		<content:encoded><![CDATA[<p>Scott:</p>
<p>Great!  That solved the instantiation problem.<br />
Thanks again for taking the time to help me out!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott Hemmeter</title>
		<link>http://sfdc.arrowpointe.com/2009/01/08/invoke-apex-from-a-custom-button-using-a-visualforce-page/comment-page-1/#comment-61474</link>
		<dc:creator>Scott Hemmeter</dc:creator>
		<pubDate>Tue, 17 Mar 2009 16:44:43 +0000</pubDate>
		<guid isPermaLink="false">http://sfdc.arrowpointe.com/?p=525#comment-61474</guid>
		<description>@DavidPSG, you need to modify the lines where you set the Standard Controller and setup the CtrBnController object.

The first line takes the Contact object, not just the ID.  The second line takes in the Standard Controller object you created in Line 1.

&lt;blockquote&gt;
ApexPages.StandardController sc = new ApexPages.StandardController(cn);

CtrBnController controller = new CtrBnController(sc);
&lt;/blockquote&gt;</description>
		<content:encoded><![CDATA[<p>@DavidPSG, you need to modify the lines where you set the Standard Controller and setup the CtrBnController object.</p>
<p>The first line takes the Contact object, not just the ID.  The second line takes in the Standard Controller object you created in Line 1.</p>
<blockquote><p>
ApexPages.StandardController sc = new ApexPages.StandardController(cn);</p>
<p>CtrBnController controller = new CtrBnController(sc);
</p></blockquote>
]]></content:encoded>
	</item>
	<item>
		<title>By: DavidPSG</title>
		<link>http://sfdc.arrowpointe.com/2009/01/08/invoke-apex-from-a-custom-button-using-a-visualforce-page/comment-page-1/#comment-61471</link>
		<dc:creator>DavidPSG</dc:creator>
		<pubDate>Tue, 17 Mar 2009 13:03:58 +0000</pubDate>
		<guid isPermaLink="false">http://sfdc.arrowpointe.com/?p=525#comment-61471</guid>
		<description>Scott:

Thanks very much for the quick reply.  
My controller is virtually identical to your example, except it acts on the Contract object rather than Opportunity, and of course the method itself is different.  The test method (I thought) was very straightforward:
&lt;blockquote&gt;
static testmethod void TestCtrBn()
	{
	Contract cn = [select ID from Contract where CustomerSignedTitle = &#039;CtrBnController&#039;];
	
	ApexPages.StandardController sc = new ApexPages.StandardController(cn.ID);
	CtrBnController controller = new CtrBnController(cn.ID);
	
	controller.UpdateTE();
	Integer t = [Select count() from Time_entry__c where Contract__c = :cn.ID];
	
	system.AssertEquals(3, t);
	}
&lt;/blockquote&gt;

The problem is when I try ti save it I get these errors:
&lt;blockquote&gt;
Save error: Constructor not defined: [ApexPages.StandardController].(Id)

Save error: Constructor not defined: [CtrBnController].(Id)
&lt;/blockquote&gt;

I don&#039;t want to take too much of your time, but was hoping a quick look might reveal my mistake.

Thanks again.</description>
		<content:encoded><![CDATA[<p>Scott:</p>
<p>Thanks very much for the quick reply.<br />
My controller is virtually identical to your example, except it acts on the Contract object rather than Opportunity, and of course the method itself is different.  The test method (I thought) was very straightforward:</p>
<blockquote><p>
static testmethod void TestCtrBn()<br />
	{<br />
	Contract cn = [select ID from Contract where CustomerSignedTitle = 'CtrBnController'];</p>
<p>	ApexPages.StandardController sc = new ApexPages.StandardController(cn.ID);<br />
	CtrBnController controller = new CtrBnController(cn.ID);</p>
<p>	controller.UpdateTE();<br />
	Integer t = [Select count() from Time_entry__c where Contract__c = :cn.ID];</p>
<p>	system.AssertEquals(3, t);<br />
	}
</p></blockquote>
<p>The problem is when I try ti save it I get these errors:</p>
<blockquote><p>
Save error: Constructor not defined: [ApexPages.StandardController].(Id)</p>
<p>Save error: Constructor not defined: [CtrBnController].(Id)
</p></blockquote>
<p>I don&#8217;t want to take too much of your time, but was hoping a quick look might reveal my mistake.</p>
<p>Thanks again.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
