Now learning in salesforce to much easy as compare to our time. The Salesforce Trailhead now offers a fun way to learn everything related to Salesforce Integration
1) https://developer.salesforce.com/trailhead/module/apex_integration_services?utm_campaign=trailhead&utm_source=sfdc&utm_medium=chatter-success
Pleae check above Trailhead module for detail knowledge of web service.
REST API
The Force.com REST API lets you integrate with Force.com applications using simple HTTP methods, in either XML or JSON formats, making this an ideal API for developing mobile applications or external clients. Force.com also supports Apex REST, which lets you create Web services on Force.com using Apex
Create REST Apex class
@RestResource(urlMapping='/api/Account/*')
global with sharing class MyFirstRestAPIClass
{
@HttpGet
global static Account doGet()
{
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
String AccNumber = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
Account result = [SELECT Id, Name, Phone, Website FROM Account WHERE AccountNumber = :AccNumber ];
return result;
}
@HttpDelete
global static void doDelete()
{
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
String AccNumber = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
Account result = [SELECT Id, Name, Phone, Website FROM Account WHERE AccountNumber = :AccNumber ];
delete result;
}
@HttpPost
global static String doPost(String name,String phone,String AccountNumber )
{
Account acc = new Account();
acc.name= name;
acc.phone=phone;
acc.AccountNumber =AccountNumber ;
insert acc;
return acc.id;
}
}
global with sharing class MyFirstRestAPIClass
{
@HttpGet
global static Account doGet()
{
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
String AccNumber = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
Account result = [SELECT Id, Name, Phone, Website FROM Account WHERE AccountNumber = :AccNumber ];
return result;
}
@HttpDelete
global static void doDelete()
{
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
String AccNumber = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
Account result = [SELECT Id, Name, Phone, Website FROM Account WHERE AccountNumber = :AccNumber ];
delete result;
}
@HttpPost
global static String doPost(String name,String phone,String AccountNumber )
{
Account acc = new Account();
acc.name= name;
acc.phone=phone;
acc.AccountNumber =AccountNumber ;
insert acc;
return acc.id;
}
}
As you can see, the class is annotated with @RestResource(urlMapping='/api/Account/*).
The base endpoint for Apex REST is
https://instance.salesforce.com/services/apexrest/.
The URL mapping is appended to the base endpoint to form the endpoint for your REST service.
For example, in the class example, the REST endpoint is
https://instance.salesforce.com/services/apexrest/api/Account/.
Test Class for REST API
@IsTest
private class MyFirstRestAPIClassTest
{
static testMethod void testGetMethod()
{
Account acc = new Account();
acc.Name='Test';
acc.AccountNumber ='12345';
insert acc;
RestRequest request = new RestRequest();
request.requestUri ='/services/apexrest/api/Account/12345';
request.httpMethod = 'GET';
RestContext.request = request;
Account acct = MyFirstRestAPIClass.doGet();
System.assert(acct != null);
System.assertEquals('Test', acct.Name);
}
static testMethod void testPostMethod()
{
RestRequest request = new RestRequest();
request.requestUri ='/services/apexrest/api/Account/12345';
request.httpMethod = 'POST';
RestContext.request = request;
String strId = MyFirstRestAPIClass.doPost('Amit','2345678','12345');
System.assert(strId !=null );
}
static testMethod void testDeleteMethod()
{
Account acc = new Account();
acc.Name='Test';
acc.AccountNumber ='12345';
insert acc;
RestRequest request = new RestRequest();
request.requestUri ='/services/apexrest/api/Account/12345';
request.httpMethod = 'DELETE';
RestContext.request = request;
MyFirstRestAPIClass.doDelete();
List<Account> ListAcct = [SELECT Id FROM Account WHERE Id=:acc.id];
System.assert(ListAcct.size() ==0 );
}
}
Execute your Apex REST class in workbench
Step 1:- Open https://workbench.developerforce.com/
Step 2:- Select Environment as Production and select the checkbox to agree the terms and condition then select Login with Salesforce.
NOTE:- If you are already login then only above step will work other wise you need to enter user name and password.
Step 3:- In the Workbench tool select Utilities > REST Explorer
Step 4:- In the REST Explorer window paste the following url in the box
Create Account Call
Method :- Post
URL :- /services/apexrest/api/Account
Body:-
{
"name" : "Test Rest",
"phone" : "123456789",
"AccountNumber" : "12345"
}
Get Account Call
Method :- Get
URL :- /services/apexrest/api/Account/12345
Body:- NA
Please check below post for more information
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_rest_methods.htm
2) https://developer.salesforce.com/page/Getting_Started_with_the_Force.com_REST_API
Using php page i want to insert my data into salesforce for that i want to use REST_API.
ReplyDeleteThis is a good one Amit, keep it up
ReplyDeleteThanks Akash
Delete@Amit Chaudhary Suppose i have morethan 50k records and using get method iam fetching the records it causes the govern limit error so how i can avoid it in rest api get method
ReplyDeletecan you guide me on this Please!!!!!
DeleteHi Amit,
ReplyDeleteCould you please let me know how to call this rest Apex class in ABAP ?
Thanks in advance.
Hi Amit,
ReplyDeleteI have the following JSON block now as u can take a look all the values are in 18 digit format and I am able to perform a PUT callout and records are getting created in My org.
However, I would not be receiving 18 digit ids . I would be receiving the external ids...Could you please help Me understand how fields like account which is a lookup field and contains 18digit ids of account can be mapped for this record. Please could you help Me it is a bit urgent.
Regards,
Siddhartha
{
"call" : "a0429000004VXxVAAT",
"owner" : "00561000002QxUQRTS",
"account" : "0012900000UbJ7KAAV",
"startdatetime" : "2019-04-24T10:29:06.000Z",
"info" : [ {
"discussed" : "Emerging salesman",
"bivvproduct" : "a0029000004NnMNAA0"
}, {
"discussed" : "Emerging Retailer",
"product" : "a00290000046uHRAAY"
} ]
}
y should check this Id's account is there or not in your org then try to put it
DeleteHi Amit,
ReplyDeleteI want to use REST APi to insert order. I followed the steps mentioned. But got stuck with AccountId.
How can I get the AccountId and map with Order.
Thanks.
hi Amit,
ReplyDeleteHow can i respond to incoming HTTP OPTIONS method call from an external page. Currently the salesforce org responds with success 200 and I don't know how. I need to respond with error code 405 so that requesting site can fallback to http POST method where i can do signature validation.
regards
Kumar
This comment has been removed by the author.
ReplyDeleteHi Amit, How to perform this rest callout from Postman with Basic Auth.
ReplyDelete