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;
} }