function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kenji775Kenji775 

Call Apex from homepage component

Hey everyone,

I am working on a small project where I need to call an Apex method from a home page component. I don't care how it gets done, I just have to figure out some way to do it.

 

First I tried rigging a visualforce page to the class/method I want, and calling it via an http requst on click, but of course since the home page components and visualforce pages are on different domains that failed due to cross domain security policies.

 

Second I tried using the connection libraries like you can use for button for calling apex in custom buttons, but I'm getting an session Id error.
 

Uncaught {faultcode:'sf:INVALID_SESSION_ID', faultstring:'INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session', }

So it doesn't seem as though that is going to work. Any other ideas? The more elegant the better. 

Best Answer chosen by Admin (Salesforce Developers) 
Kenji775Kenji775

I found an answer that I like quite well.

 

Create a home page link that is has onClick javscript as an action. Then do something like 

 

{!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);

 just the same as a custom button that invokes apex really. Can't beleive I didn't think of that sooner.