function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Arghyadeep SurArghyadeep 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;
}
}
Agustin BAgustin B
HI, what is the value of your debug : System.debug('Response Received: '+ responseBody);
Maybe you are not getting the response you need.
Arghyadeep SurArghyadeep Sur
Hi Agustin, can you provide me with the code to run in the execute anonymous window?