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
M3M3 

Calls to Apex Code from a Button/Workflow/Trigger?

I have a simple example:

 

Every time we create or update a Lead, I'd like to send back data from the Lead object to our proprietary company software to evaluate a Lead and assign a score based on their attributes.

I'd like to have a custom field in the Lead object, called Score, that gets updated with this value.

I have an Apex class that I generated from our company's WSDL, and I'd like to make a callout from this class (which will send data from Salesforce to our company, and the return value would be an Integer score value) everytime the Lead is created/updated, but triggers don't allow callouts.  They allow method calls with @future, but @future restricts calls with primitive parameters, so I can't pass in a Lead object in order to update its fields... :(

It doesn't appear that workflows or buttons allow calls to Apex code to do this either... racking my brain for the last few days... 

any thoughts on what direction I should go to accomplish this task?  Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You can't pass a Lead object to the @future method, but you can pass its ID.

 

Thus you can retrieve the data prior to making the callout.  Here's a snippet from one of my dev orgs:

 

 

@future (callout=true)
public static void NotifyLeads(List<Id> leadIds)
{
   List<Lead> leads=[select id, External_Id__c, Notify__c from Lead where id in :leadIds];
		
  // do what is required to the leads here

  update leads;
}

 

 

All Answers

bob_buzzardbob_buzzard

You can't pass a Lead object to the @future method, but you can pass its ID.

 

Thus you can retrieve the data prior to making the callout.  Here's a snippet from one of my dev orgs:

 

 

@future (callout=true)
public static void NotifyLeads(List<Id> leadIds)
{
   List<Lead> leads=[select id, External_Id__c, Notify__c from Lead where id in :leadIds];
		
  // do what is required to the leads here

  update leads;
}

 

 

This was selected as the best answer
DodiDodi

You can create a custom onclick java script button and call your Apex callout class from Java Script

M3M3

Awesome.  Thanks for your helpful response!  We were finally able to get this to work from a trigger.

M3M3

Is it possible to include javascript in the page so it fires on load of the page?  I see you can do it with a button, but what about when I click on a Lead, or Opportunity, etc.? 

bob_buzzardbob_buzzard

You can include javascript in a visualforce page that is embedded in a record view, and you can add this to the onload functions, but this won't be able to interact with the record itself, as it comes from a seperate server.

 

You can also add javascript to an HTML component in the sidebar, which you may be able to get into the onload functions, but if Salesforce decide to plug this loophole your code would stop working.

 

Do you have anything particular in mind for the javascript?

M3M3

That makes sense... We probably would rather have a solution that couldn't be invalidated by Salesforce at any given time :)

For a Lead record there are some fields we don't want to store in Salesforce (because they're changing daily in our external system), but we'd like to see their value when we view a Lead.  

 

So for example, the Lead might be associated with a debtor we do business with and we'd like to see the current credit limit we're extending to that particular debtor when we bring up the Lead record...

 

Hence, wanting an onLoad capability to pull this data from our system whenever a Lead is opened by one of our Sales guys.  Make sense?

bob_buzzardbob_buzzard

That sounds like something which would be a good fit for a visualforce page embedded in the record view.  This could either pull information from your external system on demand or include an iframe to your external system.