0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SFDC AccountManager

Posted at

クラスとそのテストクラスを下記のように実装し
確認するたびにエラーが発生
List has no rows for assignment to SObject

最後解決した方法が
@RestResource(urlMapping='/Account//contacts')

@RestResource(urlMapping='/Accounts/
/contacts')

要は複数に変更し、確認すれば、sfdc trailheadが通してくれた。
ご参考まで

#java
@RestResource(urlMapping='/Accounts/*/contacts')
global with sharing class AccountManager {

@HttpGet
global static Account getAccount() {
    RestRequest req = RestContext.request;
    RestResponse res = RestContext.response;
    String accountId = req.requestURI.substringBetween('Accounts/', '/contacts');

    Account acc = [SELECT Id, Name, (SELECT Id, Name FROM Contacts) FROM Account WHERE Id = :accountId];
    return acc;
}

}

#java
@istest
public class AccountManagerTest {

@isTest static void testGetAccount() {
    Id recordId = createTestRecord();
    // Set up a test request
    RestRequest request = new RestRequest();

    request.requestUri =
        'https://ap5.salesforce.com/services/apexrest/Accounts/'+recordId+'/contacts';
    request.httpMethod = 'GET';
    RestContext.request = request;
    // Call the method to test
    Account thisAcc = AccountManager.getAccount();
    // Verify results
    System.assert(thisAcc != null);
    System.assertEquals('Test record', thisAcc.Name);
}

// Helper method
static Id createTestRecord() {
    // Create test record
    Account accTest = new Account(
        Name='Test record');
    insert accTest;

    Contact con = new Contact();
    con.LastName = 'test con';
    con.Account = accTest;
   	insert con;
    return accTest.Id;
}

}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?