Sample Web Service With Workbench
Being a fresh learner of Salesforce this blog is helpful to guide you about writing a web service using Rest and calling the web service through Workbench.
Let's have a quick start by following the few steps below :
Task : Use Account sobject for creating the record and fetching information of any record in terms of their Id.
- First write an apex class by using RestExplorer and HTTP methods as given below
@RestResource(URLMapping='/v1/account/*') global with sharing class WebServiceClass { @HTTPGet global static Account getAccountById(){ RestRequest req=RestContext.request; String acId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1); Account results = [select id,name,description from account where id=:acId]; return results; } @HTTPPost global static String createAccount(String name, String description){ Account acc=new Account(Name=name, Description=description); insert acc; return acc.id; } }
- Login to Workbench either as production or sandbox, for users with developer edition login with production. And after entering to workbench go to Utilities then to select Rest Explorer.
- Now for getting the information of existing records by their id's as per written in the above class. Select Get method and append URL by sobjects/account/record Id and press Execute by following the below screenshot.
- Similarly for posting or creating the records of Account choose Post method and in the body put the field details required in the apex class as arguments in JSON format and remove the previously written ID from the URL following the below screenshot.
A record is created and in return the ID of that record displayed on front. Through this whole an individual get a chance to play around with Salesforce and workbench both.
Thanks For Reading!!
Happy Salesforce!!