0

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());
    }

}
9
  • I do not really understand what do you do. If you have 2 lightning components on a different orgs and trying from one lightning component to access the other - you need to authenticate first, also I think you need to set your methods as global, please tell us what are you trying to achieve. Jul 23, 2017 at 5:49
  • @Itai Shmida : thanks for your response. Basically i have 2 orgs orgA and orgB. I have created a lightning component in orgB which displays the tasks created in orgB (using SOQL query) and it also displays the tasks from orgA (using REST based webservices). Further what i am trying to do is when the tasks are updated and if they are from orgA then i am invoking PUT operation from orgB that would update the record in orgA. however when trying to do so am getting this error i.e unable to read sobject
    – SFDC coder
    Jul 31, 2017 at 10:38
  • I understand, this is an authentication issue, how do you use rest? please share more relevant code Jul 31, 2017 at 10:56
  • @ Itai Shmida Please check my question. i have added the code there which explains how i have used rest. Check from line -----------New Update as on 31st july-----------
    – SFDC coder
    Jul 31, 2017 at 11:35
  • Can you please check the below thread : salesforce.stackexchange.com/questions/112636/… Jul 31, 2017 at 11:53

0

You must log in to answer this question.