You need to sign in to do that
Don't have an account?
Denis O'Leary
Calling an Apex class from a detail page button
Hi,
i have a detail button set on an Account page as an Execute JavaScript/On Click JavaScript button.
The button has the following: (which i got from the salesforce documentation)
i have a detail button set on an Account page as an Execute JavaScript/On Click JavaScript button.
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; } }
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_Callouts
https://developer.salesforce.com/docs/atlas.en-us.workbook.meta/workbook/button_intro.htm
All Answers
Few things that needs to be updated.
1) The class should be of global context instead public
2) The method should of of signature "webservice static string webservice static"
the error i am getting when i click the detail button is that there is an unexpected token
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_Callouts
https://developer.salesforce.com/docs/atlas.en-us.workbook.meta/workbook/button_intro.htm