The below article will help you to create a basic Rest API class to insert an account record in salesforce by creating a method with @HttpPost annotation. Also this guide will help you to test the functionality by using using Advanced Rest Client chrome app.
Step 1:
Create a REST API class with @HttpPost annotation method.
Step 1:
Create a REST API class with @HttpPost annotation method.
- @RestResource(urlMapping='/AccountMapping/*')
- global with sharing class RESTAccountController {
- @HttpPost
- global static String createNewAccount(String accountName, String accountNumber) {
- System.debug('Account Name: '+accountName);
- System.debug('Account Number: '+accountNumber);
- try
- {
- Account acc = new Account();
- acc.Name = accountName;
- acc.AccountNumber = accountNumber;
- insert acc;
- return 'Account has been inserted succesfully: '+acc.Id;
- }
- catch(DMLException de)
- {
- return de.getDmlMessage(0);
- }
- return null;
- }
- }
Note:
The @RestResource annotation is used at the class level and enables you to expose an Apex class as a REST resource.
1. The URL mapping is relative to https://instance.salesforce.com/services/apexrest/
2. URL mapping is case-sensitive
Read this documentation well before setting up urlMapping.
Step 2:
Install Advanced Rest Client from the below link,
Note: You can also use Postman tool, if you like.
Step 3:
Create a "Connected App" by referring this documentation.
After you created the "Connected App", Go to Create --> Apps --> Connected App --> Select your connected app which you created.
Your connected app looks like below,
After you created a connected app ensure the app permission is given at profile level. Also Copy the "Consumer Key" and "Consumer Secret" code for authentication in Advance Rest Client.
Step 4:
Open Advanced REST Client app in Chrome and follow the below steps.
Steps to obtain an access token:
URL:
https://login.salesforce.com/services/oauth2/token
METHOD:
POST
BODY:
grant_type=password&client_id=3MVG9Ad93Bn17huFzcp2LseGauyeShr6X9TmixGMGQ5MuS2hoPxlEHAvyVQzZVzdhClK6WUFdMY8FzXqNxc4&client_secret=2536602822064617993&username=testuser@force.com&password=psmu97VQzZVzdhClK6WUFdMY8FzXqNxc
Client_id and Client_secret: Copied from connected app.
Username: Your salesforce user name.
Password: Your salesforce account password with security token.
(If you are in White list IP ranges, then you don’t need to add a security token with password).
CONTENT TYPE:
application/x-www-form-urlencoded
Sending Request:
Access Token:
After you send a request your will get an access token. This will looks like below,
Copy the access token from the response.
CALLING REST SERVICE:
REST URL:
https://na15.salesforce.com/services/apexrest/AccountMapping
Adding access token:
Authorization OAuth "Your access token"
JSON Body:
{"accountName":"Arunkumar",
"accountNumber":"128393"}
Response From Sever:
Thanks for reading this post...!
thank you very much! your tutorial helped me a lot after lot of hours spent on tries
ReplyDeletewhy is the Callback URL "www.facebook.com"???????
ReplyDeleteI have a Task which is, update the information(which i already in Salesforce Account object) from external application using Rest API
ReplyDeleteProblem: What i am facing is, can able to retrieve only 10 record per apex callout but when i am testing the API using ARC or Postman where i am getting actual records arround exactly 800 record.
Solution as Per my understand from this blog, can we update SF account object records using ARC or Postman
Already you people know about Oracle Huge growth. PLSQL is also one of the parts of oracle already. PLSQL students are moving to Advanced PL SQL now because they know about the importance and value of this course. Wanna be part of this PLSQL training in Chennai for more enquiries call 7502633633.Oracle Training with Placement | Infycle Technologies
ReplyDeleteThanks for sharing this informative article on How to test a Restful web services in Salesforce by using Advanced Rest Client chrome app. If you want to Salesforce Services for your project. Please visit us.
ReplyDelete