[SalesForce] System.sObjectException: Invalid Field – trying to use a nested SOQL query

I am getting a System.sObjectException: Invalid field error when trying to assign a value across objects. Also, I'm trying calling my method via Process Builder using the @InvocableMethod annotation (if that makes a difference). I'm in a sandbox that has been upgraded to Winter '16. (On a side note, I must say the error messages out of Process Builder are much more helpful than in the current release, though not helpful enough to get me to resolve my issue.)

I've researched and have seen solutions for this type of error, but I haven't seen one, yet, that uses a nested SOQL query.

First, my SOQL query:

// Retrieve next activity date and type
   List<Contact> conResult = [SELECT Id, (SELECT Id, Type, ActivityDate 
                                          FROM Tasks
                                          WHERE ActivityDate >= :thisDay
                                          AND Reason_for_Contact__c != NULL
                                          ORDER BY ActivityDate ASC LIMIT 1)
                              FROM Contact
                              WHERE Id IN :contactIds];

I've tried changing the list data type from Contact to sObject, but that didn't work. This is the code when I try to assign the variables from Task onto the Contact record:

// Set Contact next activity date and type
   if(!conResult.isEmpty()){
       for(Contact con : conResult) {
           Contact tempCon = new Contact();

           tempCon.Next_Action__c = (String)con.get(String.valueOf('Type'));
           tempCon.Next_Action_Date__c = (Date)con.get('ActivityDate');

           updateContact.add(tempCon);
        }
    }

I've also tried assigning the value of Task to a variable and then assigning the Contact.Next_Action__c field to the variable, but I keep getting the same error.

Oh, this is the error:

Error Occurred: An Apex error occurred: System.SObjectException: Invalid field Type for Contact

Any help is appreciated. Please let me know if you would like more information.


Update

Working code per the correct answer below with a slight modification (passing the Contact Id into the tempCon object):

// Set Contact next activity date and type
    if(!conResult.isEmpty()){
        for(Contact con : conResult) {
            Contact tempCon = new Contact();

            if(con.Tasks.size() > 0){
                tempCon.Id = (Id)con.get('Id');
                tempCon.Last_Action__c = (String)con.Tasks[0].get(String.valueOf('Type'));
                tempCon.Last_Action_Date__c = (Date)con.Tasks[0].get('ActivityDate');

                updateContact.add(tempCon);
            }
        }
    }

Thank you @MohithShrivastava!

Best Answer

Change your code to below

// Set Contact next activity date and type
  if(!conResult.isEmpty()){
   for(Contact con : conResult) {
       Contact tempCon = new Contact();
     if(Con.Tasks.size()>0){
       tempCon.Next_Action__c = (String)con.Tasks[0].get(String.valueOf('Type'));
       tempCon.Next_Action_Date__c = (Date)con.Tasks[0].get('ActivityDate');

       updateContact.add(tempCon);
     }
    }
}

You are retrieving value from Task record not contact and hence field needs to be accessed from Task instead .

Related Topic