You need to sign in to do that
Don't have an account?
Unable to validate AccountManager APEX Web Service Trailhead Challenge
I've completed the challenge, it has 100% coverage. I've checked all the method names. The URL is valid. I've used Work Bench and curl to test and even tested with multiple Accounts with and without contacts.
I know on other challenges, punctionation was important. What about the defination of the return? Are there expected names?
I built a class to hold Account ID & Name along with a List of Contact names and IDs. Is this my issue? Anyone else have a challenge with this challenge?
Any help or hints will be appreciated.
Here are snippets of my code:
@RestResource(urlMapping='/Accounts/*/contacts')
global with sharing class AccountManager {
....
global class APIAccount {
public ID Id;
public String Name;
List<APIContact> Contacts;
...
@HttpGet
global static APIAccount getAccount() {
RestRequest request = RestContext.request;
...
I know on other challenges, punctionation was important. What about the defination of the return? Are there expected names?
I built a class to hold Account ID & Name along with a List of Contact names and IDs. Is this my issue? Anyone else have a challenge with this challenge?
Any help or hints will be appreciated.
Here are snippets of my code:
@RestResource(urlMapping='/Accounts/*/contacts')
global with sharing class AccountManager {
....
global class APIAccount {
public ID Id;
public String Name;
List<APIContact> Contacts;
...
@HttpGet
global static APIAccount getAccount() {
RestRequest request = RestContext.request;
...
Below please find my working code, and be careful with the lower case for contacts.
All Answers
I have a similar problem - Salesforce failed to validate my solution, as below:
any help is truely appreciated. Thanks you, Sinan
Below please find my working code, and be careful with the lower case for contacts.
Thanks for your insight,
Sinan
- The result must be an account (its how I passed) as Lei Shi pointed it out
- Be very very careful with no records returned from your query as could a nightmare as I faced. Solved by adding: @isTest(SeeAllData=true)
I think it is very woth, at least it was for me to read this article about this: Demystifying SeeAllData in Unit Tests http://opfocus.com/blog/demystifying-seealldata-in-unit-tests/.All the best,
Esteve
AccountManager Apex Class
@RestResource(urlMapping='/Accounts/*/contacts')
global class AccountManager {
@HttpGet
global static Account getAccount() {
RestRequest req = RestContext.request;
String accId = req.requestURI.substringBetween('Accounts/', '/contacts');
Account acc = [SELECT Id, Name, (SELECT Id, Name FROM Contacts)
FROM Account WHERE Id = :accId];
return acc;
}
}
TEST CLASS
@isTest
private class AccountManagerTest {
private static testMethod void getAccountTest1() {
Id recordId = createTestRecord();
// Set up a test request
RestRequest request = new RestRequest();
request.requestUri = 'https://na1.salesforce.com/services/apexrest/Accounts/'+ recordId +'/contacts' ;
request.httpMethod = 'GET';
RestContext.request = request;
// Call the method to test
Account thisAccount = AccountManager.getAccount();
// Verify results
System.assert(thisAccount != null);
System.assertEquals('Test record', thisAccount.Name);
}
// Helper method
static Id createTestRecord() {
// Create test record
Account TestAcc = new Account(
Name='Test record');
insert TestAcc;
Contact TestCon= new Contact(
LastName='Test',
AccountId = TestAcc.id);
return TestAcc.Id;
}
}
After saving it "Run All" test from developer console. It'll help you to clear the challange.
Please choose it as best answer.
I looked at the developer console log after ruinning the challange and found '/contacts' (lower case) was passed by the engine.
replacing '/Contacts' --> '/contacts' in my code solved my problem.
I may help somebody.
Wouldn't it be more appropriate to use a @TestSetup method insead of SeeAllData=True to follow SF unit testing standards?
That was my issue has well, I could not get the List to populate with data and once I updated to Contacts from contacts my List populated and I passed the challenge.
@RestResource(urlMapping='/Accounts/*/contacts')
global class AccountManager {
@HttpGet
global static Account getAccount(){
RestRequest request = RestContext.request;
return [Select id, name,(SELECT id,name from Contacts) from Account where id=:request.requestURI.substringBetween('Accounts/', '/contacts')];
}
}
Test class
@isTest
public class AccountManagerTest {
@isTest public static void executeGetAccount(){
//making Data ready for Testing
Account acc = new Account(Name='Jayanth B');
insert acc;
insert new Contact(LastName='Jayanth',FirstName='b',AccountId=acc.id);
// Set up a test request
RestRequest request = new RestRequest();
request.requestURI = 'https://ap4.salesforce.com/services/apexrest/Accounts/'+(String)acc.id+'/contacts';
request.httpMethod ='GET';
//set up request Context
RestContext.request = request;
// call the method
Account returnedAcc = AccountManager.getAccount();
}
}
Thanks for posting "optimised" code. Next time, could you mark your code as a code block (in the WYSIWYG editor)? That makes it easier for future generations to read.
For all who come in the future, the challenge requires the class to start with Note the trailing "/contacts". The challenge also requires you to specify /contacts on the end of your REST URI.
The class actually doesn't require this, but this unit doesn't make it clear. You *can* use the urlMapping without '/contacts' and the resulting data is almost identical. Unfortunately, the challenge doesn't mention this, nor does it test for this simpler (i.e. more easily maintained) usage. In fact, the challenge should make it clear exactly what type the return result should be and what the URI construction should be.
Its not very clear in instructions, that you have to make a test record for account in your test as Test methods cannot read your Organization data.
So either you create some test resords in test class or enable see all data (@IsTest(SeeAllData=true)). This will allow to read org data in your test method.
Challenge not yet complete... here's what's wrong:
There was an unexpected error in your org which is preventing this assessment check from completing: System.TypeException: Cannot call test methods in non-test context
Best of luck with the rest of the trail.
String Accountid = request.requestURI.substringBetween('Accounts/','/contacts');
When i execute the query in Query Editor it is returning a row.
Following is my AccountManager
and my Test Class
Can someone help in figuring out the issue
I used the following to extract the record Id:
but I much prefer Lei's simple solution:
@RestResource(urlMapping='/Accounts/*/contacts')
global class AccountManager {
@HttpGet
global static Account getAccount() {
RestRequest request = RestContext.request;
String accId = request.requestURI.substringBetween('Accounts/','/contacts');
List<Account> acc = [SELECT Id, Name, (SELECT Id, Name FROM Contacts) FROM Account WHERE Id = :accId];
return acc.get(0);
}
}
@isTest
public class AccountManagerTest {
@isTest(SeeAllData=true)
public static void getAccountTest(){
//create Test Data
Account acc=new Account(Name='testAccount1');
Contact con=new Contact(Account=acc,AssistantName='vidhyaTest',LastName='mukmuk');
insert acc;
Id recordId=acc.Id;
insert con;
RestRequest request = new RestRequest();
request.requestUri = 'https://sadas-dev-ed.my.salesforce.com/services/apexrest/Accounts/'+recordId+'/mukmuk';
request.httpMethod = 'GET';
RestContext.request = request;
// Call the method to test
Account thisAccount = AccountManager.getAccount();
// Verify results
System.assert(thisAccount != null);
}
}
Challenge not yet complete in PLAYGROUND
Executing the 'AccountManager' method failed. Either the service isn't configured with the correct urlMapping, is not global, does not have the proper method name or does not return the requested account and all of its contacts.
What could it be??
My final class look like this:
And the test like this:
Good luck.
Executing the 'AccountManager' method failed. Either the service isn't configured with the correct urlMapping, is not global, does not have the proper method name or does not return the requested account and all of its contacts.
Have u pass the challenge by changing playground . I m facing the same error help here thank you..
Test class
@isTest
private class AccountManagerTest {
private static testMethod void getAccountTest1() {
Id recordId = createTestRecord();
// Set up a test request
RestRequest request = new RestRequest();
request.requestUri = 'https://na1.salesforce.com/services/apexrest/Accounts/'+ recordId +'/contacts' ;
request.httpMethod = 'GET';
RestContext.request = request;
// Call the method to test
Account thisAccount = AccountManager.getAccount();
// Verify results
System.assert(thisAccount != null);
System.assertEquals('Test record', thisAccount.Name);
}
// Helper method
static Id createTestRecord() {
// Create test record
Account TestAcc = new Account(
Name='Test record');
insert TestAcc;
Contact TestCon= new Contact(
LastName='Test',
AccountId = TestAcc.id);
return TestAcc.Id;
}
}
Accountmanager class
@RestResource(urlMapping='/Accounts/*/contacts')
global class AccountManager {
@HttpGet
global static Account getAccount() {
RestRequest request = RestContext.request;
String accId = request.requestURI.substringBetween('Accounts/','/contacts'); Account acc = [SELECT Id, Name, (SELECT Id, Name FROM Contacts) FROM Account WHERE Id = :accId];
return acc;
} }
I beat my head against a rock on this one for a long time. Finally, I narrowed it down to an issue with the test and found Esteve's comment from 2015 with the answer to my problem. Adding (SeeAllData=True) to the method's @isTest annotation did the trick.
For quick reference here is a snippet of the code.
Esteve, it was a nightmare. The dawn arrived and I woke up when I found your comment.
Thanks, everyone!
I'm just a Salesforce noob. Trying in de last 6 weeks to get some development knowledge using Trailhead. But these challenges are too difficult to solve on my own. I continuously miss Salesforce background information. Because there are so many Units, Modules and Trails. Which makes it easy to get lost on the level of needed experiences. And I forgot also a lot. Needed knowledge is shattered. Difficult to say you know enough, to do a challenge. Even it's part of a trail.
I had a lot of problems to solve this on my own, I lack the skill set. Came on this page, to learn about the mistakes I've been made.
- Module too complex, like the suddenly introduction of `cUrl`. I now what is, I use it sparingly. But I couldn't get it up and running with al these security codes, url's. I would say, explain it in a separate unit.Why explain it, if you don't really need it. It is handy, but it is distracting. Like, I'm using at home Win7, no curl. A small problem to solve, but it breaks the real focus of the lesson.
- I missed the clue; "urlMapping='/Accounts/*/contacts'" ~ /Accounts/<id_account>/contacts'. It is REST Logic, but how to recognize this.
- Never knew you can not access 'live' data or use the bad testdesign option @isTest(SeeAllData=true), Took me ages to understand why my account list was empty all the time.
And yes, I've learned a lot! but 50min took me a half day.@RestResource(urlMapping='/Accounts/*/contacts')
global with sharing class AccountManager{
@HttpGet
global static Account getAccount(){
RestRequest request = RestContext.request;
String accountId = request.requestURI.substringBetween('Accounts/','/contacts');
system.debug(accountId);
Account objAccount = [SELECT Id,Name,(SELECT Id,Name FROM Contacts) FROM Account WHERE Id = :accountId LIMIT 1];
return objAccount;
}
}
Test Class:
@IsTest
private class AccountManagerTest {
@isTest(SeeAllData=true)
static void testGetAccount() {
Id accountId = '0015j00000XrVLlAAN';
String accountName = 'PanCake';
//set up a test request
RestRequest request = new RestRequest();
request.requestUri ='https://wise-goat-e0r2m4-dev-ed.my.salesforce.com/Accounts/'+ accountId + '/contacts/';
request.httpMethod = 'GET';
RestContext.request = request;
// method call to test
Account thisAccount = AccountManager.getAccount();
// verify results
System.assert(thisAccount != null);
System.assertEquals(thisAccount.name, accountName, 'Company ABC');
}
}
Hey Guys ,Try this one. It will definitely help you to pass the challenge.100% working. If you also passed the challenge hit the thumb.
So can any one tell me where I am doing wrong here
#Remote Site Setting
Remote Name - > SOAP
URL -> https://th-apex-soap-service.herokuapp.com
#Error
Challenge not yet complete in My Trailhead Playground 1
Executing the method 'getAccount' from the Apex class 'AccountManager' failed. Either the service isn't configured with the correct urlMapping, is not global, does not have the proper method name or does not return the requested account and all of its contacts.
#Code
@RestResource(urlMapping='/Accounts/*/contacts')
global class AccountManager {
@HttpGet
global static Account getAccount() {
RestRequest req = RestContext.request;
String accId = req.requestURI.substringBetween('Accounts/', '/contacts');
Account acc = [SELECT Id, Name, (SELECT Id, Name FROM Contacts)
FROM Account WHERE Id = :accId];
return acc;
}
}
#Test Class
@isTest
private class AccountManagerTest {
private static testMethod void getAccountTest1() {
Id recordId = createTestRecord();
// Set up a test request
RestRequest request = new RestRequest();
request.requestUri = 'https://na1.salesforce.com/services/apexrest/Accounts/'+ recordId +'/contacts' ;
request.httpMethod = 'GET';
RestContext.request = request;
// Call the method to test
Account thisAccount = AccountManager.getAccount();
// Verify results
System.assert(thisAccount != null);
System.assertEquals('Test record', thisAccount.Name);
}
// Helper method
static Id createTestRecord() {
// Create test record
Account TestAcc = new Account(
Name='Test record');
insert TestAcc;
Contact TestCon= new Contact(
LastName='Test',
AccountId = TestAcc.id);
return TestAcc.Id;
}
}