Skip to main content Join us at TDX in San Francisco or on Salesforce+ on March 5-6 for the Developer Conference for the AI Agent Era. Register now.

#Trailhead Challenges2,396 discussing

Hands-on challenges are the “secret sauce” of Trailhead. Before searching for solutions to superbadge challenges, review the Salesforce Certification Program Agreement and Policies.
24 answers
0/9000

I have written this code : public class AnimalLocator {

    

public static String getAnimalNameById(Integer i) {

        Http http = new Http();

        HttpRequest request = new HttpRequest();

        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals'+i);

        request.setMethod('GET');

        HttpResponse response = http.send(request); 

    

    //if the request is successful, parse the JSON response

    if(response.getStatusCode()==200){

        Map<String,Object> result = (Map<String,Object>)JSON.deserializeUntyped(response.getBody());

    Map<String,Object> animal = (Map<String, Object>) result.get('animal');

    return string.valueOf(animal.get('name'));

        

    }else{

        return null;

    }

    

    }

 

}

and I am getting this error

 

#Trailhead Challenges

8 answers
  1. Dec 9, 2024, 8:34 PM

    Hi @Sravya Kummari ,

     

    whenever we work with REST API we need to allow salesforce to hit that API, for that the endpoint URL should be added in remote site settings;

     

    go to Setup -> Remote site settings:- add a new remote site setting name it anything but URL should be https://th-apex-http-callout.herokuapp.com . then you can check the challenge

     

    https://trailhead.salesforce.com/trailblazer-community/feed/0D54S00000BqpSCSAZ

0/9000
5 answers
  1. Nov 12, 2024, 7:19 AM

    No Remote Site named ‘animals_http’ was found with the correct Name, URL, and Description.Apex REST Callouts last step error

0/9000

Hi Trailhead community,

I'm working on the Apex REST Callouts challenge, and I’ve completed the exercise as instructed. Here are the key details:

  • The AnimalLocator class has a public static method named getAnimalNameById.
  • The method accepts an Integer as a parameter and returns a String.
  • I’ve written the AnimalLocatorTest class to test this method, and all tests pass successfully, achieving 100% code coverage.
  • The AnimalLocatorMock and AnimalLocatorMockError classes are implemented to mock the responses, and they seem to work as expected.

However, when I validate the challenge, I get the following error message:

"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."

I’ve double-checked the method name, signature, and access modifiers, but the error persists.

Has anyone encountered a similar issue? Are there any specific things I should look into to resolve this?

Thanks in advance for your help!

(Here is a print:)

Apex REST Callouts Challenge Validation Issue

 

#Trailhead Challenges

4 answers
  1. Divya Chauhan (Kcloud Technologies) Forum Ambassador
    Nov 19, 2024, 5:32 AM
0/9000

Hi all,

The instructions say to create a test class that uses a mock class to mock the callout. 

I think that the mock class uses a static resource to mock the callout and does the callout to the main class.  Does it then return a StaticResourceCalloutMock object to the test, with the return value from the mock callout set as the body of the mock object?

 

Thanks in advance,

Matt 

 

#Trailhead Challenges

6 answers
  1. Nov 13, 2024, 8:35 PM

    Hi, @Matt Doyle 

     

    When you're writing a test class for an Apex REST callout, you need to use a mock class to simulate the HTTP response. 

     

    The purpose of the mock class is to avoid making real HTTP callouts during testing, which is not allowed in test contexts. 

     

    For example:

    @isTest

    global class AnimalLocatorMock implements HttpCalloutMock {

    global HTTPResponse respond(HTTPRequest request) {

    HttpResponse response = new HttpResponse();

    response.setHeader('Content-Type', 'application/json');

    response.setBody('{"animal":{"id":1,"name":"chicken","eats":"chicken food","says":"cluck cluck"}}');

    response.setStatusCode(200);

    return response;

    }

    }

    Also too, make sure if you activated Setup Remote site settingsHi, When you're writing a test class for an Apex REST callout, you need to use a mock class to simulate the HTTP response.

    Sincerely, 

    Mykhailo Vdovychenko 

    Bringing Cloud Excellence with IBVCLOUD OÜ 

0/9000

I am working on the Apex Integration Services-Apex rest Callouts module. When i am creating the File|New|Static resource for name enter GetAnimalResource and MIME type is text/plain, i am getting the below error:

Apex REST Callouts

 

#Trailhead Challenges

8 answers
  1. Nov 7, 2024, 10:10 PM

    i have created the static resource from setup in the previous module of the unit. for this unit  Apex Rest Callouts, they are mentioning to create a static resource using JSON formatted string to use the GET request in the developer console, when i was doing the steps mentioned, then i got the error

0/9000

Hi, I am working on the "Apex Integration Services"  Module on the  "Apex REST Callouts" unit. I need to create a Static Resource in the developers console:

File -> New -> Static Resource

I enter the Name (GetAnimalResource). select the MIME type:text/plain and when I press submit i get this error:

UNKNOWN_EXCEPTION

An unexpected error occurred. Please include this ErrorId if you contact support: 1345751719-421449 (1976928079).

 

I've tried with other browsers and playgrounds but got the same error always.

Can you help me?

 

#Trailhead Challenges

0/9000