Oh my god. It's full of code!

Salesforce Call Apex Code From Hompeage Component

So this is something I had to do for a challenge recently, where I wanted to invoke my Apex class/method from a home page component. I had heard some solutions involving a link that called a visualforce page (nice thought, but a little clunky and hard to pass parameters) or loading a visualforce page tied to the controller in an iframe (again, works but not the most elegant). I remembered that there is a connection library that allows javascript to call Apex, that is normally used for custom buttons and back in the day s-controls. I putzed around with the syntax some and realized I was making it way more complicated than it needed to be. Below is my solution that I think is pretty slick.

1) Write an apex method that is set as a webservice so it can be called by the library (example below)

    webservice static string createLinkSimple(string url, string title)
    {
          string returnVar = 'Link added!';
          try
          {
               favLink__c thisLink = new favLink__c();
               thisLink.location__c = url;
               thisLink.name = title;
               thisLink.owner__c = UserInfo.getUserId();
               insert thisLink;
           }
           catch(Exception e)
           {
               returnVar = 'Error adding link: ' + e.getMessage();
           }    
           return returnVar;
    }

2) Create a home page link (setup->customize->home-> custom links)
3) Create a link link. Set the content source as ‘onClick javascript’
4) Enter code similar to the following

{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}

var result = sforce.apex.execute("favLinks","createLinkSimple", {url: document.location.href, name: document.title});
alert(result);

(The first param is the class name, the second is the method name, and then pass the arguments in the {} brackets)

5) Save it and add the link to a homepage component and display it in the sidebar. You are done!

The will allow you to invoke Apex classes and store the result in a variable to do whatever you want with. Combine this with my Salesforce – Pushing Custom Buttons to the Limit With jQuery and Apex article and you can build some pretty powerful cool stuff.

Hope this helps someone out there.

4 responses

  1. Rafi

    Hi,
    How do you seal with the need to login from the JavaScript, hus exposing the password?
    At least in mt attempt, and even thouh th euser was logged in to SFDC with an SFDC page, there was an exception thwron on loin.

    Thanks,

    January 10, 2012 at 1:24 pm

    • You don’t need to login. When it’s hosted on the same platform including the script libraries with your session id works.

      January 17, 2012 at 12:16 am

  2. thank you for this great post ! i appriciate it 🙂 thank you again .

    October 24, 2012 at 4:40 pm

  3. pawanns

    How to add the link to homepage component. can you tell me? I am confused, coz there is a custom links in home page component explicitly.

    October 17, 2013 at 3:21 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s