Using with Custom Endpoints

There are instances where the standard rest API may not meet all your needs. This typically happens if:

  1. You need more transactional control over a series of operations

  2. The end user does not have access to object/fields/operations you need to perform

  3. This is a public endpoint and you need to enforce logic on the server-side

We can still call these custom endpoints using ts-force and often even use our generated classes with the request and response.

@RestResource

@RestResource(urlMapping='/myservice')
global with sharing class RestServiceTesting {
    @HttpPost
     global static Account doPost(Account acc) {
        return acc;
    }
}

Things to Note:

  • We can call our endpoint by using new Rest().request.post<SObject>.

  • We can use acc.toJson to map our Account object into the format that salesforce is expecting

  • Account.fromSFObject(data) to parse the response from salesforce back to our Account object

Invokable

A special method invokeAction is provided to make it easier to call invokable methods

public class AccountInsertAction {
  @InvocableMethod(label='Insert Accounts' description='Inserts the accounts specified and returns the IDs of the new accounts.')
  public static List<ID> insertAccounts(List<Account> accounts) {
    Database.SaveResult[] results = Database.insert(accounts);
    List<ID> accountIds = new List<ID>();
    for (Database.SaveResult result : results) {
      if (result.isSuccess()) {
        accountIds.add(result.getId());
      }
    }
    return accountIds;
  }
}

@RemoteAction

If you are running inside a VisualForce page, you may want to use @RemoteActions to call dedicated controller code.

public class MyController{
  @RemoteAction
  public static String foo(Account acc) {
    return "hello world";
  }
}

See remote-action-promise for more details on calling @RemoteActions from VisualForce apps.

Last updated

Was this helpful?