1

I have an Apex class that I want to use to send SF Account information to a third party application when a button is pushed in Salesforce.

I have tried calling the class from a blank VF page that is pointing to the class, but this doesn't seem to be working.

What options are there for getting the class to get called?

Any help/suggestions are appreciated

The class itself is:

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 XXXXXXX';
    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 +'" }}' 
    );

    try{

    res = h.send(req);
    resp = new Response(res.getStatusCode(), res.getBody());      
    }
         catch(System.CalloutException e) {
             System.debug('Callout error: '+ e);
             return resp;
    }        
    return resp;   

}

}

  • Are you trying to write a REST Service? – Adrian Larson Aug 30 '16 at 15:04
  • There are many ways to get an apex class to be called from a button - (1) Ajax Toolkit + class defined as webservice (2) Custom controller with action methods (3) Javascript + VF remote actions for example – cropredy Aug 30 '16 at 15:08
  • @AdrianLarson - Yes, i'm trying to post SF account details as a customer to another application – denisoleary Aug 30 '16 at 15:09
2

You can use a small Visualforce page for the development of this kind of code:

<apex:page controller="Sender">
    <apex:pageMessages/>
    <apex:commandButton value="Send" action="{!send}"/>
</apex:page>

with controller:

public class Sender {
    public PageReference send() {
        try {
            new AccountToFACustomer().CreateCustomer();
        } catch (Exception e) {
            ApexPages.addMessages(e);
        }
        return null;
    }
}

and improve from there for better result reporting. (Remove the try/catch from the AccountToFACustomer class as that will hide any exception.)

  • Can I get the controller to be Account? I want the account page to feed info on the class. e.g. account.name etc – denisoleary Aug 30 '16 at 15:38
  • @denisoleary You could for example make the page an Account controller extension or otherwise accept an Account Id so it can query. But as others have commented there are multiple approaches: my example is mainly just for trying out the code. – Keith C Aug 30 '16 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.