Please help me with the code!
I was solving the given challenge,
*Create an Apex class that calls a REST endpoint to return the name of an animal, write unit tests that achieve 100% code coverage for the class using a mock response, and run your Apex tests.
Create an Apex class:
Name: AnimalLocator
Method name: getAnimalNameById
The method must accept an Integer and return a String.
The method must call https://th-apex-http-callout.herokuapp.com/animals/id, using the ID passed into the method
The method returns the value of the name property (i.e., the animal name)*
My code is,
**
public class AnimalLocator {
public static string getAnimalNameById(integer animalId) {
string animalName;
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/'+animalId);
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> r.get('animal');
animalName = string.valueOf(animal.get('name'));
}
return animalName;
}
}
**
it throws the errors,
**
7 件の回答
Hi Mo,
If you are facing this issue, either you have entered the wrong HTTP URL in the remote site settings or have not created the remote site in salesforce. So now try these steps then try to complete the challenge:
1. Goto setup in salesforce search Remote site settings in Quick find box then clicks on 'Remote site Settings'.
2. Click on New Remote Site.
3. Enter the Remote Site Name.
4. Enter the Remote Site URL as 'https://th-apex-http-callout.herokuapp.com'.
5. Checked the Active button.
6. Click Save.
Now try to complete the challenge.
Hope this explanation will resolve your query.
Thanks
Akshay