Simple HTTP Class in Salesforce

Sample Code:

global with sharing class AccountManager {
    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

2. Select REST Explorer.

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


No comments:

Post a Comment

Sharing is Caring!!! Share this with your friends!!!

submit to reddit