Inbound API using Apex code
In this tutorial, we will look at creating an endpoint for Inbound Salesforce API using Apex code.
We will stick to a nice and easy simple example. Our endpoint will rely on the account object.
We will have 3 methods associated with @HttpGet, @HttpPost, @HttpDelete
Get method will return 6 accounts
Post method creates a new account with name.
Delete method will delete the individual account based on ID supplied.
@RestResource(urlMapping ='/AccountInfo/*')
Global class AccountInfo {
@httpget
global static List<account> GetAccount()
{
list<account> a = [select name from account limit 6];
return a;
}
@httppost
global static string PostNewAccount(string aName)
{
account a = new account();
a.name = aname;
insert a;
return a.id;
}
@httpdelete
global static void DeleteAccount()
{
RestRequest Req = RestContext.request;
string val = req.requestURI.substring(req.requestURI.lastIndexof('/')+1);
delete [select id from account where id =: val];
}
}
Go to Workbench or Postman api to test it
Comments
Post a Comment