The button has the following: (which i got from the salesforce documentation)
{!REQUIRESCRIPT("/soap/ajax/37.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/37.0/apex.js")}
var result = sforce.apex.execute("AccountToFACustomer","CreateCustomer",
{
Account.Name:"{! Account.Name}",
Account.Email__c: "{!Account.Email__c}",
Account.Phone:"{!Account.Phone}",
Account.Website: "{!Account.Website}",
Account.Sage_ID__c:"{!Account.SAGE_Id__c}",
Account.BillingStreet:"{!Account.BillingStreet}",
Account.BillingCity: "{!Account.BillingCity}",
Account.BillingState: "{!Account.BillingState}",
Account.BillingCountry: "{!Account.BillingCountry}"
}
);
alert(result);
window.location.reload();
and I have it calling a class i created below, should this work or am i totally missing the mark?
public with sharing class AccountToFACustomer{
public class Response {
public integer code {get; set;}
public String body {get; set;}
public boolean success {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(){
Response resp;
string endpoint = 'https://apistaging.website.net/customer/';
string token = 'Token XXXXXXXX';
string method = 'POST';
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http h = new http();
req.setEndpoint(endpoint);
req.setMethod(method);
req.setHeader('Authorization', token);
req.setHeader('Content-Type', 'application/json');
req.setHeader('Accept-Type', 'application/json');
req.setBody(
'{"name":"'+ Account.Name +'",' +
'"email":"'+ Account.Email__c + '",'+
'"phone":"'+ Account.Phone +'",'+
'"website":"'+ Account.Website +'",'+
'"location":{'+
'"name":"Account No: '+ Account.Sage_ID__c +'",'+
'"streetName":"'+ Account.BillingStreet +'",'+
'"locality":"'+ Account.BillingCity +'",'+
'"postcode":"'+ Account.BillingState +'",'+
'"country":"'+ Account.BillingCountry +'" }}'
);
res = h.send(req);
resp = new Response(res.getStatusCode(), res.getBody());
return resp;
}
}
When calling apex method from javascript we are essentiall making a callout using SOAP API. For this to be succesfull the scope of class and method should be modified.The scope of the class should be global and method should be webservice.Please find below few articles with information:https://developer.salesforce.com/page/Apex_Web_Services_and_Calloutshttps://developer.salesforce.com/docs/atlas.en-us.workbook.meta/workbook/button_intro.htm