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 

APEX Rest Class

Hi,

I am trying to write my first Apex test class and I keep on getting an error: 

Non-void method might not return a value or might have statement after a return statement.

Can someone see what I have done wrong? 
public with sharing class AccountToFACustomer {
    
    public class Response{
        public integer code {get; set;}
        public String body {get; set;}
        public boolean success {get; set;}
        public String errorText {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 (string description){
        
        Response resp;
        
        string endpoint = 'https://apis.website.net/';
        string token = 'Token XXXXX';
        string method = 'POST';
        
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http h = new http();
        
        req.setEndpoint(endpoint);
        req.setMethod(method);
        req.setHeader('Autorization', 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()); 
            
            if (resp.success) {
                customerResponse custResp = (customerResponse)JSON.deserialize(res.getBody(), customerResponse.class);
            }
            
        }
        	 catch(System.CalloutException e) {
   				 System.debug('Callout error: '+ e);
           
        }        
          
    
    }
}

 
Best Answer chosen by Denis O'Leary
Amit Chaudhary 8Amit Chaudhary 8
Please check below posy for more help on same
1) http://amitsalesforce.blogspot.com/search/label/Rest%20API
2) http://amitsalesforce.blogspot.com/2016/04/rest-api-in-salesforce-execute-rest-api.html

Update your code like below
public with sharing class AccountToFACustomer {
    
    public class Response{
        public integer code {get; set;}
        public String body {get; set;}
        public boolean success {get; set;}
        public String errorText {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 (string description){
        
        Response resp;
        
        string endpoint = 'https://apis.website.net/';
        string token = 'Token XXXXX';
        string method = 'POST';
        
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http h = new http();
        
        req.setEndpoint(endpoint);
        req.setMethod(method);
        req.setHeader('Autorization', 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()); 
            
            if (resp.success) {
                customerResponse custResp = (customerResponse)JSON.deserialize(res.getBody(), customerResponse.class);
            }
            
        }
        	 catch(System.CalloutException e) {
   				 System.debug('Callout error: '+ e);
				 return resp;
           
        }        
        return resp;
    
    }
}

 

All Answers

James LoghryJames Loghry
I'm not seeing any sort of @isTest annotation or any evidence that this is a test class.  That being said, the following method is supposed to be returning a Response object:  
 
public Response CreateCustomer (string description)

However, you never return any response object.  Try adding 
 
return resp;

to the end of your method and see if that works.
Amit Chaudhary 8Amit Chaudhary 8
Please check below posy for more help on same
1) http://amitsalesforce.blogspot.com/search/label/Rest%20API
2) http://amitsalesforce.blogspot.com/2016/04/rest-api-in-salesforce-execute-rest-api.html

Update your code like below
public with sharing class AccountToFACustomer {
    
    public class Response{
        public integer code {get; set;}
        public String body {get; set;}
        public boolean success {get; set;}
        public String errorText {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 (string description){
        
        Response resp;
        
        string endpoint = 'https://apis.website.net/';
        string token = 'Token XXXXX';
        string method = 'POST';
        
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http h = new http();
        
        req.setEndpoint(endpoint);
        req.setMethod(method);
        req.setHeader('Autorization', 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()); 
            
            if (resp.success) {
                customerResponse custResp = (customerResponse)JSON.deserialize(res.getBody(), customerResponse.class);
            }
            
        }
        	 catch(System.CalloutException e) {
   				 System.debug('Callout error: '+ e);
				 return resp;
           
        }        
        return resp;
    
    }
}

 
This was selected as the best answer
Denis O'LearyDenis O'Leary
@James - That was a type above, I meant to say it was an Apex Rest class (not test)
@Amit - Thanks for the links, I will read through them now 

Thanks Amit/James!