Technology has always been a bridge, but today, it feels more like a mirror. With the rapid rise of AI , we are seeing things enter our lives and leave them at a pace we can barely track. To understand where this is going, we first have to understand how technology actually impacts the core of who we are. The Survivalist vs. The Ego Our minds are biologically wired for one thing: survival . We are designed to handle the worst-case scenario, an ancient instinct gifted to us by nature. We consider ourselves conscious decision-makers, but a critical question remains: Who is really making the call?
Rest APIs enable us to perform various operations such as creating and deleting, among others. In typical situations, when the data is passed as a parameter in the REST method, it becomes straightforward to manipulate, as demonstrated in the following example.
@RestResource(urlMapping='/api/CreateContact/*')
global with sharing class RestApexAPIClass
{
@HttpPost
global static String doPost(String name,String phone ) {
Contact con = new Contact();
con.name= name;
con.phone=phone;
insert con;
return con.id;
}
}The "doPost" method accepts a name and phone as input parameters and proceeds to assign them to the contact object, facilitating the creation of a fresh contact within the Salesforce platform.
The issue arises when data is received from an external system in the form of JSON. The question then arises: how can we obtain and parse that JSON data? Fortunately, this problem can be easily solved, as demonstrated in the following example:
@RestResource(urlMapping='/api/CreateAccount/*')
global with sharing class MyFirstRestAPIClass
{
@HttpPost
global static void doPost( ) {
RestRequest req = RestContext.request;
Blob body = req.requestBody;
String requestString = body.toString();
fromJSON ss = (fromJSON)JSON.deserialize(requestString,fromJSON.class);
system.debug(SS);
}
}
//Json Parse class
global class fromJSON{ global cls_invoiceList[] invoiceList; global class cls_invoiceList { public string totalPrice; public String statementDate; public cls_lineItems[] lineItems; public string invoiceNumber; } global class cls_lineItems { public string UnitPrice; public string Quantity; public String ProductName; } global static fromJSON parse(String json){ return (fromJSON) System.JSON.deserialize(json, fromJSON.class); }
}
The provided code is performing the following actions:
- It retrieves the current REST request using
RestContext.requestand assigns it to thereqvariable. - It retrieves the request body as a
Blobusingreq.requestBodyand assigns it to thebodyvariable. - It converts the
Blobinto a string representation usingbody.toString()and assigns it to therequestStringvariable.
- It uses the
JSON.deserializemethod to convert therequestStringinto an instance of thefromJSONclass. ThefromJSONclass is expected to represent the structure of the JSON data.
- It assigns the deserialized object to the variable
ss.
- Finally, it logs the value of
ssusingSystem.debug().
In summary, the code is extracting the JSON data from the request body, deserializing it into an object of a specific class (
fromJSON), and then printing the resulting object for debugging purposes.