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
Raj R.Raj R. 

How to create a button that calls apex calls that does future callout?

Hi,

We have a need to create a button that on click will take the current Opportunity data (some fields) and pass it into the apex class (that is a future callout) so it can execute the logic in the class. 

How would i do this?
 
//class that does future callout to some endpoint

class Sample {

            @future(callout=true)
             public sendCallOut() {
                       //some action using the current opportunity
                      HttpRequest req = new HttpRequest();
                      //add the appropriate headers
                      ///put opportunity data in the body of request

                     HTTPResponse resp = new HTTPResponse();
                     //send and process request
             }//end sendCallOut()
}//end Sample

 
Best Answer chosen by Raj R.
James EllisJames Ellis
Hi rr,

To do this I'd create a custom controller APEX class, put the method in that, then create a Visualforce page with nothing in it and call the method from the controller.
I'd then create a custom URL button that called that visualforce page with the record Id appended to the URL so it could be passed into the controller and used referenced in the call out method

I havent tested the below but something like:-
controller:-
 
public class myFutureButtonController{
 
private String opportunityId ;
 
public myFutureButtonController()
{
 //get the opportunityId
this.opportunityId = ApexPages.CurrentPage().getparameters().get('thisOppId') //custom parameter name, make sure this is used in the custom button
if(opportunityId != null) //don't queue the job unless we have the id to pass in
 {
 sendCallOut(opportunityId); //call the future method and let it know the id of the opty you want it to work with
 }
}

        @future(callout=true)
          public sendCallOut(String oppId) {
                    //some action using the current opportunity
 Opportunity theOppRecord ;
List<Opportunity> lo = new List<Opportunity>([select id, any other fields for the call out etc from Opportunity where Id = :opportunityId]); //query as a list even if you only want one record to avoid no object found to assign to sobject variable error
    if(lo.size() > 0) //only do the call out if you have the data needed
       theOppRecord = lo[0];

       //now use the query result theOppRecord to complete any pre call out logic                               
HttpRequest req = new HttpRequest();                       
       //add the appropriate headers                       
      ///put opportunity data in the body of request                     
      HTTPResponse resp = new HTTPResponse(); 
                     
      //send and process request
    }
    else  { //handle the error, send the user an email/post to chatter}

 
    }//end sendCallOut()
 
}




visualforce page called myPage
 
<apex:page controller="myFutureButtonController" ></apex:page>





the button - create one that directs to a URL
enter this url /apex/myPage?thisOppId={!OpportunityId}
add to opportunity page layout





Hope this helps you get started

If this solves your question please mark as best answer to help others.