I have a scenario where i am displaying tasks from 2 orgs. I am able to edit and save the tasks displayed from the self org. But the tasks edited for external org throws an error that unable to read sobject.
Why do I get this error? How can I fix it?
here is the code
<aura:component controller="ContactController" access="global">
<aura:attribute name="taskInstance" type="Task" default="{ 'sobjectType':'Task' }"/></aura:component>
Lightning Controller
doSave : function(component, event) {
var action = component.get("c.saveUpdatedTaskInfo");
var t = component.get("v.taskInstance");
action.setParams({ "taskInst": t});
}
Apex controller
@AuraEnabled
public static Task saveUpdatedTaskInfo(Task taskInst) {
if(taskInst.Org_Identifier__c=='Org 1')
{
update taskInst;
}
else
{
Task_Trigger_Handler.processTasks(new List<Task>{taskInst});
}
return taskInst;
}
Note: "processTasks" method invokes a future method that performs put operation. Is it because of this there might be a problem here?
Any help on this would be highly appreciated
-----------New Update as on 31st july-----------
Adding information about processTasks method that uses REST to update the task in ORG A
public class Task_Trigger_Handler
{
public static void processTasks(List<Task> tskLst)
{
for(Task tsk:tskLst)
{
if(tsk.Org_Identifier__c=='Org 1')
{
//invoke the callout method
invokeTaskCallout(tsk.id,tsk.Subject,.........);
}
}
}
@future(callout=true)
public static void invokeTaskCallout(String id,String .......)
{
//fetch from custom settings
CustomSettings__c customSettings=[SELECT password__c,rest_endpoint__c,token_endpoint__c,username__c
FROM customSettings__c LIMIT 1];
//establish connection and fetch the session id
String endpoint1= customSettings.token_endpoint__c;
Httprequest req = new HttpRequest();
req.setMethod('POST');
req.setHeader('Content-Type','application/x-www-form-urlencoded');
req.setBody('grant_type=password' +
'&client_id=' + customSettings.Consumer_key__c+
'&client_secret=' + customSettings.Consumer_Secret__c+
'&username=' + customSettings.username__c+
'&password=' + customSettings.password__c
);
req.setEndpoint(endpoint1);
Http http = new Http();
HttpResponse res;
String Access_Token;
try
{
res = http.send(req);
system.debug('body1:'+res.getBody());
JSONParser parser = JSON.createParser(res.getBody());
while (parser.nextToken() != null)
{
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'access_token'))
{
parser.nextToken();
Access_Token = parser.getText();
}
}
}
catch(system.CalloutException e)
{
system.debug('error'+e);
}
system.debug('access token'+Access_Token);
String endpoint2=customSettings.rest_endpoint__c;
HttpRequest req1 = new HttpRequest();
HttpResponse res1 = new HttpResponse();
Http http1 = new Http();
String str='{"id":"'+id+'","Subject":"'+Subject+'","ActivityDate":"'+String.valueOf(ActivityDate)+'","Category":"'+Category+'","Description":"+Description+","Status":"'+Status+'","Internal_Deadline":"'+String.valueOf(Internal_Deadline)+'","Actual_Deadline":"'+String.valueOf(Actual_Deadline)+'"}';
req1.setEndpoint(endpoint2);
req1.setMethod('PUT');
req1.setHeader('content-type', 'application/json');
req1.setHeader('Accept', 'application/json');
req1.setHeader('Authorization', 'OAuth '+Access_Token);
req1.setBody(str);
req1.setCompressed(true); // otherwise we hit a limit of 32000
try
{
res1 = http1.send(req1);
}
catch(System.CalloutException e)
{
System.debug(res1.toString());
}
}