You need to sign in to do that
Don't have an account?
Arghyadeep Sur
"First error: Malformed JSON: Expected '{' at the beginning of object" How do i resolve this error??
public class UpdatePostCallout11 {
@InvocableMethod(label='Update Post details based on Post ID')
public static void updatePost(List<Id> postLIst){
if (postLIst != null && postLIst.size() > 0){
getCalloutResponse(postLIst);
}
}
@future(callout = true)
public static void getCalloutResponse(List<Id> postLIst){
postWrapper postWrapperObj;
List<postInformation> postInfoList = new List<postInformation>();
Map<Integer, String> postTitleMap = new Map<Integer, String>();
// Perform Rest Callout
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://jsonplaceholder.typicode.com/posts');
request.setMethod('GET');
HttpResponse response = http.send(request);
String responseBody = null;
if (response.getStatusCode() == 200){
responseBody = response.getBody();
System.debug('Response Received: '+ responseBody);
}
// If the REST API Response is not null, deserialize the Response and store id, postTitle in map
if (String.isNotBlank(responseBody)){
postWrapperObj = (postWrapper)System.JSON.deserialize(responseBody, postWrapper.class);
if (postWrapperObj != null) {
postInfoList = postWrapperObj.data;
}
if (postInfoList.size() > 0){
for(postInformation obj : postInfoList){
postTitleMap.put(obj.id, obj.postTitle);
}
}
}
List<Post__c> PstListToUpdate = new List<Post__c>();
List<Post__c> PostRecords = [Select Id, Id__c, Title__c from Post__c where Id IN: (postLIst)];
if (PostRecords != null && PostRecords.size() > 0){
for(Post__c PstObj : PostRecords){
if (postTitleMap != null && postTitleMap.containsKey(Integer.valueOf(PstObj.Id__c))){
PstObj.Title__c = postTitleMap.get(Integer.valueOf(PstObj.Id__c));
PstListToUpdate.add(PstObj);
}
}
}
if (PstListToUpdate.size() > 0)
update PstListToUpdate;
}
public class postWrapper{
public List<postInformation> data;
}
public class postInformation{
public Integer id;
public String postTitle;
}
}
@InvocableMethod(label='Update Post details based on Post ID')
public static void updatePost(List<Id> postLIst){
if (postLIst != null && postLIst.size() > 0){
getCalloutResponse(postLIst);
}
}
@future(callout = true)
public static void getCalloutResponse(List<Id> postLIst){
postWrapper postWrapperObj;
List<postInformation> postInfoList = new List<postInformation>();
Map<Integer, String> postTitleMap = new Map<Integer, String>();
// Perform Rest Callout
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://jsonplaceholder.typicode.com/posts');
request.setMethod('GET');
HttpResponse response = http.send(request);
String responseBody = null;
if (response.getStatusCode() == 200){
responseBody = response.getBody();
System.debug('Response Received: '+ responseBody);
}
// If the REST API Response is not null, deserialize the Response and store id, postTitle in map
if (String.isNotBlank(responseBody)){
postWrapperObj = (postWrapper)System.JSON.deserialize(responseBody, postWrapper.class);
if (postWrapperObj != null) {
postInfoList = postWrapperObj.data;
}
if (postInfoList.size() > 0){
for(postInformation obj : postInfoList){
postTitleMap.put(obj.id, obj.postTitle);
}
}
}
List<Post__c> PstListToUpdate = new List<Post__c>();
List<Post__c> PostRecords = [Select Id, Id__c, Title__c from Post__c where Id IN: (postLIst)];
if (PostRecords != null && PostRecords.size() > 0){
for(Post__c PstObj : PostRecords){
if (postTitleMap != null && postTitleMap.containsKey(Integer.valueOf(PstObj.Id__c))){
PstObj.Title__c = postTitleMap.get(Integer.valueOf(PstObj.Id__c));
PstListToUpdate.add(PstObj);
}
}
}
if (PstListToUpdate.size() > 0)
update PstListToUpdate;
}
public class postWrapper{
public List<postInformation> data;
}
public class postInformation{
public Integer id;
public String postTitle;
}
}
Maybe you are not getting the response you need.