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
CloudGeekCloudGeek 

Apex REST Callouts - Challenge Not Yet Complete : Make Sure the method Exists with the Name

Challenge Not yet complete... here's what's wrong: 
Executing the 'getAnimalNameById' method on 'AnimalLocator' failed. Make sure the method exists with the name 'getAnimalNameById', is public and static, accepts an Integer and returns a String.

My Class Code is here :
 
global class AnimalLocator {
    
    public static String getAnimalNameById(Integer num) 
    {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/:'+num);
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        
        String answer='VenMal';
        
        
        // If the request is successful, parse the JSON response.
        if (response.getStatusCode() == 200) 
        {
            System.debug('response.getBody() ========'+response.getBody());
            answer = 'NEW ANIMAL'; 
        }
        return answer;
    }      

}

And the TEST Class is as below:
 
@isTest
global class AnimalLocatorTest 
{
 static testMethod void testCallOut() 
{
    // Set mock callout class 
    Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock()); 
    // This causes a fake response to be sent
    // from the class that implements HttpCalloutMock. 
    string response = AnimalLocator.getAnimalNameById(1);
    // Verify that the response received contains fake values
       System.debug('response === '+response);
    
}

}

Test Mock Class is as below:
 
@isTest
global class AnimalLocatorMock implements HttpCalloutMock {
    // Implement this interface method
    global HTTPResponse respond(HTTPRequest request) 
    {
        // Create a fake response
        HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type', 'application/json');
        response.setBody('{"animal":{"id":0,"name":"VenMal","eats":"MeaTT","says":"ThanKYoU"}}');
        response.setStatusCode(200);
        return response; 
    }
}

When I queried the ApexClass Object, I could see all 3-classes 

Apex REST Callouts TrailHead Classes


Am I missing something or what should I do to succesfully validate this Unit ?
 
Best Answer chosen by CloudGeek
Shashikant SharmaShashikant Sharma
Hi,

Your logic seems not correct and that is why it is giving this error.

Try this:
 
public class AnimalLocator{
    public static String getAnimalNameById(Integer x){
        Http http = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/' + x);
        req.setMethod('GET');
        
        HttpResponse res = http.send(req);
            if (response.getStatusCode() == 200) {
        Map<String, Object> results = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());
        Map<String, Object> animal = (Map<String, Object>) results.get('animal');
        return (String)animal.get('name');

}
    }
}

Read these posts for more: 
https://developer.salesforce.com/forums/?id=906F0000000MJXvIAO
https://developer.salesforce.com/forums/?id=906F0000000MLlfIAG

Thanks
Shashikant

All Answers

Shashikant SharmaShashikant Sharma
Hi,

Your logic seems not correct and that is why it is giving this error.

Try this:
 
public class AnimalLocator{
    public static String getAnimalNameById(Integer x){
        Http http = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/' + x);
        req.setMethod('GET');
        
        HttpResponse res = http.send(req);
            if (response.getStatusCode() == 200) {
        Map<String, Object> results = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());
        Map<String, Object> animal = (Map<String, Object>) results.get('animal');
        return (String)animal.get('name');

}
    }
}

Read these posts for more: 
https://developer.salesforce.com/forums/?id=906F0000000MJXvIAO
https://developer.salesforce.com/forums/?id=906F0000000MLlfIAG

Thanks
Shashikant
This was selected as the best answer
CloudGeekCloudGeek
Hi Shashikant,

Thanks a ton for your help!

Any pointers on what is OAuth/ authentication in SFDC how should I start with implmenting auth in SFDC?
  

 
Shashikant SharmaShashikant Sharma
Hi,

This should help you : https://developer.salesforce.com/trailhead/en/mobile_sdk_introduction/mobilesdk_intro_security

Thanks
Shashikant