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
Joseph Miramon 1Joseph Miramon 1 

How to call an REST API via a button?

Hi guys, I need your help. I am not a developer and I don't know to edit the codes below. I placed these codes on a Detail Page Button with Content Source as OnClick Javascript. These codes were only handed to me, with the purpose of calling an external web API.

-------------------------
HttpRequest req = new HttpRequest();
Http http = new Http();        
        
// set the request method

req.setMethod(‘POST');

String url = 'http://api.xxx.com/quickbooks/createinvoice’ + '?invoiceRefNumber=' + EncodingUtil.urlEncode([TheInvoiceReferenceNumber],'UTF-8’) + '&price=' + EncodingUtil.urlEncode([TheInvoicePrice],'UTF-8’) + '&date=' + EncodingUtil.urlEncode([TheInvoiceDate],'UTF-8');
        
// add the endpoint to the request

req.setEndpoint(url);
        
// create the response object

HTTPResponse resp = http.send(req);
----------------------

When the button is clicked, I get an error: Unexpected identifier 'req'

What should I do? Thanks guys.
Hugo_HernandezHugo_Hernandez
Hi Josep assuming that your code snippet does not need other functionality you can try with this:

Controller:
global with sharing class classForAnswerCommunity  {
	public classForAnswerCommunity () {
		System.debug('Starting my help :D');	
	}


	@RemoteAction
    global static String callToServer(String param) {

    	System.debug('---> '+ param);

        HttpRequest req = new HttpRequest();
		Http http = new Http();        
		String url = 'http://api.xxx.com/quickbooks/createinvoice?invoiceRefNumber='+EncodingUtil.urlEncode('TheInvoiceReferenceNumber','UTF-8')+'&price='+EncodingUtil.urlEncode('TheInvoicePrice','UTF-8')+'&date='+EncodingUtil.urlEncode('TheInvoiceDate','UTF-8');
		
		req.setEndpoint(url);
		req.setMethod('POST');

		HTTPResponse resp = http.send(req);
		        

		if(resp.getBody() != null)
			return 'Response of server: '+resp.getBody();
		else
			return 'Call failure';
    
    }
}

Page:
<apex:page showHeader="true" sidebar="true" controller="classForAnswerCommunity">
    
    <head>
    	
		<script type="text/javascript">
		    function callToServer() {

		        Visualforce.remoting.Manager.invokeAction(
		            '{!$RemoteAction.classForAnswerCommunity.callToServer}',
		            'pass parameter only if is necessary', 
		            function(result, event){
		                if (event.status) {
		                	//In the variable result stored the data that return callToServer

		                	alert(result);
		                } else if (event.type === 'exception') {

		                	alert('message: '+event.message+ ' where: '+event.where);
		                } else {
		                    alert('Message: '+event.message);
		                }
		            }, 
		            {escape: true}
		        );
		    }
	    </script>
    </head>

    <body>
    	<button onclick="callToServer();">Call To Server</button>
    </body>
</apex:page>


NOTE: Copy & paste and review the parameters that you concatenate in the variable "String url" because I had to change these values


I hope I've helped, regards from Mexico :D