Simple HTTP Class in Salesforce

Sample Code:

@RestResource(urlMapping=’/Accounts/*’)
global with sharing class AccountManager {
    @HttpGet
    global static Account getAccount() {
        RestRequest request = RestContext.request;
        String acctId = request.requestURI.removeEnd(‘/contacts’).substring(request.requestURI.removeEnd(‘/contacts’).lastIndexOf(‘/’)+1);
        Account result =  [ SELECT Id, Name, ( SELECT Id, Name FROM Contacts ) FROM Account WHERE Id =: acctId ];
        return result;
    }
}


To test it, do the following


1. Log in into https://workbench.developerforce.com.

2. Select REST Explorer.

3. Enter /services/apexrest/InfallibleTech/Accounts/001U000001ZwOUN/contacts and click “Execute”.

Cheers!!!

Leave a Reply