+ Start a Discussion
Huy NguyenHuy Nguyen 

Salesforce webservice call out external API

Hi expert .

Can anyone show me an example code for that ? Thank a lot
NagaNaga (Salesforce Developers) 
Hi Huy,

Please see the sample code below

User-added imagePlease see the below link for more info

https://developer.salesforce.com/page/Apex_Web_Services_and_Callouts

Best Regards
Naga Kiran
Le NguyenLe Nguyen
Hi Huy,

Below is a sample code to call an external service called Sester.  You can base on this to call other Web Service with Endpoint.
 
public list<SetsterProviderData> getSetsterProvider(string provID,string sessionID)
	{
		Http http           = new Http();
		HttpRequest req 	= new HttpRequest(); 
		HttpResponse res    = new HttpResponse();
		
		//Set end point to Authenciate
		string EndPoint = 'http://www.setster.com/api/v2/employee?id='+ provID +'&session_token='+sessionID;
		
		req.setEndpoint( EndPoint );
		req.setMethod('GET');
		req.setTimeout(120000);               
		req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		integer responseCode = 0;
		string jsonResponse = ''; 
		if(Test.isRunningTest())
		{
			responseCode = 200;
			jsonResponse = '{"statusCode":0,"statusDescription":"OK","data":[{"id":"16863","company_id":"10973","email":"tester123@acutedge.com","first_name":"Tester","last_name":"Tester","job":"Tester","bio":"abcde","phone":"(090) 11111","photo_url":null,"public_email":"0","status":"1","username":"TesterTester89691100","nickname":"JetNguyen","linkedin":"","twitter":"","facebook":"","receivesms":"0","sms_email":"","intuit_user_id":"","ipp_mode":"0","intuit_auth_id":"","intuit_db_id":"","intuit_realm_id":"","intuit_ticket_id":"","newsletter":"0","is_owner":"0","can_login":0,"permissions":null,"location_links":[],"name":"Tester Tester","photo_absolute_url":"","photo_thumb_absolute_url":""}]}';
		}
		else
		{
			res = http.send(req);
			responseCode = res.getStatusCode(); 
			jsonResponse = res.getBody();
		}
		system.debug(jsonResponse);
		//mapping class
		if(responseCode == 200)
		{
			SetsterProviderResponses sas =  (SetsterProviderResponses)JSON.deserialize(jsonResponse, SetsterProviderResponses.class);
			
			return sas.data; 
		}
		else
		{
			return null;
		}
	}
 
//For JSON deserialize
public with sharing class SetsterProviderResponses {
	public integer statusCode { get; set; }
    public string statusDescription { get; set; }
    public List<SetsterProviderData> data { get; set; }
}


public with sharing class SetsterProviderData {
	public string id { get; set; }
    public string company_id { get; set; }
    public string email { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string job { get; set; }
    public string bio { get; set; }
    public string phone { get; set; }
    public string public_email { get; set; }
    public string nickname { get; set; }
    //public LocationLinks location_links { get; set; }
    public string status {get;set;}
    public string name { get; set; }
    public string facebook {get;set;} 

    
}

Hope this can help you.

Regards,

Le