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
Denis O'LearyDenis 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)
{!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;   
    
    }
}


 
Best Answer chosen by Denis O'Leary
Prateek Singh SengarPrateek Singh Sengar
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_Callouts
https://developer.salesforce.com/docs/atlas.en-us.workbook.meta/workbook/button_intro.htm

All Answers

Prateek Singh SengarPrateek Singh Sengar
Hi Denis,
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"

 
Denis O'LearyDenis O'Leary
thanks Prateek, i have made the class global but i'm not sure i understand what you mean by changing the method signature 
the error i am getting when i click the detail button is that there is an unexpected token 
Prateek Singh SengarPrateek Singh Sengar
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_Callouts
https://developer.salesforce.com/docs/atlas.en-us.workbook.meta/workbook/button_intro.htm
This was selected as the best answer