I am trying to build an APEX Rest class that will push Salesforce data to a third party application.
I have watched some training videos and come up with the below. I am trying to run this in the developer console (Execute Anonymous Window) just to see if the code is correct and working.
Ultimately I want the body request to have Account.Name, Account.Email__c etc rather than the test info i have provided but first i want to make sure this is working.
When i try to run this however i keep getting an unexpected Token error: line 5 class.
Is it possible to test this code in the developer console?
public with sharing class AccountToFACustomer {
public class Response{
public integer code {get; set;}
public String body {get; set;}
public boolean success {get; set;}
public String errorText {get; set;}
public Response (integer code, String body){
this.code = code;
this.body = body;
this.success = (code == 200 || code == 201);
}
}
public class customerResponse{
public string uuid {get; set;}
public string link {get; set;}
public string location {get; set;}
}
public Response CreateCustomer (string description){
Response resp;
string endpoint = 'https://apistaging.website.net/';
string token = 'Token XXXXXXXXXXXXX';
string method = 'POST';
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http h = new http();
req.setEndpoint(endpoint);
req.setMethod(method);
req.setHeader('Autorization', token);
req.setHeader('Content-Type', 'application/json');
req.setHeader('Accept-Type', 'application/json');
req.setBody(
'{"name":"Test300816",' +
'"email":"[email protected],'+
'"phone":"12345678",'+
'"website":"www.website.com",'+
'"location":{'+
'"name":"Account No: 300816",'+
'"streetName":"Street",'+
'"locality":"dublin",'+
'"postcode":"ballsbridge",'+
'"country":"Ireland" }}'
);
try{
res = h.send(req);
resp = new Response(res.getStatusCode(), res.getBody());
if (resp.success) {
customerResponse custResp = (customerResponse)JSON.deserialize(res.getBody(), customerResponse.class);
}
}
catch(System.CalloutException e) {
System.debug('Callout error: '+ e);
return resp;
}
return resp;
}
}