Creating, Updating, deleting, patching and
putting case record using rest api :-
Following is the same code for all the above actions,
@RestResource(urlMapping='/Cases/*')
global with sharing class KnowledgeCaseManager {
@HttpGet
global static Case getCaseById() {
RestRequest request
= RestContext.request;
// grab the caseId
from the end of the URL
String caseId =
request.requestURI.substring(
request.requestURI.lastIndexOf('/')+1);
Case result =
[SELECT CaseNumber,Subject,Description,Status,Origin,Priority
FROM Case
WHERE Id = :caseId];
return result;
}
@HttpPost
global static ID
createCase(String subject, String status,
String origin,
String priority) {
Case thisCase = new
Case(
Subject=subject,
Status=status,
Origin=origin,
Priority=priority);
insert thisCase;
return thisCase.Id;
}
@HttpDelete
global static void deleteCase() {
RestRequest request
= RestContext.request;
String caseId =
request.requestURI.substring(
request.requestURI.lastIndexOf('/')+1);
Case thisCase =
[SELECT Id FROM Case WHERE Id = :caseId];
delete thisCase;
}
@HttpPut
global static ID upsertCase(String
Subject,String Description,String Status,
String Origin,String
Priority,String id) {
Case thisCase = new
Case(
Id=id,
Subject=Subject,
Description=Description,
Status=Status,
Origin=Origin,
Priority=Priority);
// Match case by Id,
if present.
// Otherwise, create
new case.
upsert thisCase;
// Return the case
ID.
return thisCase.Id;
}
@HttpPatch
global static ID
updateCaseFields() {
RestRequest request
= RestContext.request;
String caseId =
request.requestURI.substring(
request.requestURI.lastIndexOf('/')+1);
Case thisCase =
[SELECT Id FROM Case WHERE Id = :caseId];
// Deserialize the
JSON string into name-value pairs
Map<String,
Object> params = (Map<String,
Object>)JSON.deserializeUntyped(request.requestbody.tostring());
// Iterate through
each parameter field and value
for(String fieldName
: params.keySet()) {
// Set
the field and value on the Case sObject
thisCase.put(fieldName, params.get(fieldName));
}
update thisCase;
return thisCase.Id;
}
}
Retrieve Data with GET Method using Workbench:
In Workbench--Workbench-->>select GET -->>
Enter the URI /services/apexrest/Cases/<Record
ID>, replacing <Record ID> with the ID of the record you created in
the previous step.
Click Execute.
This invocation calls the method associated with the GET HTTP method, namely the getCaseById method.
To view the response returned, click Show Raw Response.
The returned response looks similar to this response. The response contains the fields that the method queried for the new case record.
For posting data to record using workbench: -->> reset explorer -->> Post -->>
replace the default URI with /services/apexrest/Cases/
For the request body, insert the following JSON string representation of the object to insert.
{
"subject" : "Bigfoot
Sighting!",
"status" : "New",
"origin" : "Phone",
"priority" : "Low"
}
To access it from other systems like java and .net we should provide the entire url path like, https://yourInstance.salesforce.com/services/apexrest/<Cases>/
Here "https://yourInstance.salesforce.com/services/apexrest/" is the base endpoint for REST API and followed by URL mapping whatever we provided to rest class in our example it is "/Cases/" mentioned on top.
Annotation Action Details
@HttpGet Read Reads or retrieves records.
@HttpPost Create Creates records.
@HttpDelete Delete Deletes records.
@HttpPut Upsert Typically used to update
existing records or create records.
@HttpPatch Update Typically used to update
fields in existing records.
No comments:
Post a Comment