0

I'm new to Salesforce and I was wondering why do I get the following error for my code. if you can please write me back code would be gratefully appreciated.

API class

public class CheckbookAPI {

    // DigitalCheck__APIConfig__c is a custom setting object that stores the API Keys
    public static DigitalCheck__APIConfig__c Config = DigitalCheck__APIConfig__c.getOrgDefaults();

    public CheckbookAPI()
    {
    }

    public static String getChecks() {
        HttpRequest req = CheckbookAPI.getHttpRequest('GET', '/transactions');
        return CheckbookAPI.getHttpResponse(req);
    }    
    public static String createCheck(String email, Decimal amount, String firstName, String lastName, String semail, String description) {
        HttpRequest req = CheckbookAPI.getHttpRequest('POST', '/send_check');

        Map<String,Object> mapParams = new Map<String,Object>();
        mapParams.put('email', email);
        mapParams.put('amount', amount);
        mapParams.put('first_name', firstName);
        mapParams.put('last_name', lastName);
        mapParams.put('sender_email', semail);
        mapParams.put('description', description);
        req.setBody(JSON.serialize(mapParams));
        return CheckbookAPI.getHttpResponse(req);
    }


    private static String getHttpResponse(HttpRequest req) {
        Http http = new Http();
        HTTPResponse response = http.send(req);
        return response.getBody();
    }

    private static HttpRequest getHttpRequest(String Method, String Path) {
        // Initialize the request
        HttpRequest req = new HttpRequest();

        // Build the selected elements
        String SelectedElements = '';


        // Set the method
        req.setMethod(Method);
        SelectedElements += Method;

        // Set the Content-Type header
        if (Method == 'POST') {
            SelectedElements += 'application/json';
            req.setHeader('Content-Type', 'application/json');
        }

        // Set the endpoint
        String CompletePath = '/' + CheckbookAPI.Config.DigitalCheck__VersionAPI__c + Path;
        SelectedElements += CompletePath;
        req.setEndpoint(CheckbookAPI.Config.DigitalCheck__ServerURL__c + CompletePath);

        return req;
    }



}

API CALL class :

global with sharing class DigitalChecksChargeController {

    public DigitalChecksChargeController(ApexPages.StandardController stdController)
    {

    }    

    @RemoteAction
    public static String processCharge() {
        return '200'; // Simulate the status code of the <charge> request
    }

    @RemoteAction
    public static String createCheck(String email, Decimal amount, String firstName, String lastName, String semail, String description) {    
        return CheckbookAPI.createCheck(email, amount, firstName, lastName, semail, description);
    }    
}

TEST class:

@isTest
public class CheckbookAPI_UsingStaticResources {
    public testmethod static void testWS() {
        String testBody = 'This is a test :-)';

        StaticResourceCalloutMock mock = new StaticResourceCalloutMock();
        mock.setStaticResource('Test_WS');
        mock.setStatusCode(200);
        Test.setMock(HttpCalloutMock.class, mock);

        CheckbookAPI  callout = new CheckbookAPI ();
        HttpResponse resp = callout.getChecks();  // <== GETTING ERROR : Static methods cannot be invoked through an object instance: getChecks()

        //System.assertEquals(200, resp.getStatusCode());        
        //System.assertEquals(resp.getBody(), testBody);
    }
}

So I'm getting

Static methods cannot be invoked through an object instance: getChecks()
4

If you define your method as static you can call it directly without having to create an instance of your class. So instead of

CheckbookAPI  callout = new CheckbookAPI ();
HttpResponse resp = callout.getChecks();

You can do:

String resp = CheckbookAPI.getChecks();
| improve this answer | |
0

There is no need to instantiate a class if you are to call a static method.

instead of

CheckbookAPI  callout = new CheckbookAPI ();
HttpResponse resp = callout.getChecks();

try using

HttpResponse resp = CheckbookAPI.getChecks();
| improve this answer | |
  • I'm getting this error now : Error: Compile Error: Illegal assignment from String to System.HttpResponse at line 11 column 14 – Alex Seifi Mar 18 '16 at 1:59

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.