2

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!

  • Does it work, if you try the same from anonymous apex in Developer console? Because everything looks fine. – javanoob Sep 14 '15 at 21:19
  • @javanoob, I haven't tried that yet. It compiles without error and it looked right to me, so I've just been trying it in the sandbox with an active Process Builder flow. It's been slightly frustrating. – justinpdove Sep 14 '15 at 23:27
3

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 .

  • opps sorry spoke to early before seeing the limit 1 on the task. Interesting that there is only one task per contact :/ – Rao Sep 14 '15 at 21:49
  • Its order by Activity date .Picking latest I guess my dear friend – Mohith Shrivastava Sep 14 '15 at 21:51
  • Yeah, so our sales team wants to view the next activity date on the contact detail page without having to scroll all the way down to the activity related list. I know, it's an arduous process. The SOQL query is to choose the next activity date, which I am choosing by selecting the first task with an ActivityDate greater than today. – justinpdove Sep 14 '15 at 23:25
  • @MohithShrivastava, quick question. In the second if statement, shouldn't the conditional be if(con.Tasks.size() > 0)? If tempCon is a newly instantiated Contact object, its Task size should always be 0 correct? – justinpdove Sep 15 '15 at 11:17
  • 1
    @MohithShrivastava faaaaaantastic! I got one more error after making the change because I didn't pass an Id into the tempCon object. So I added tempCon.Id = (Id)con.get('Id') and it worked beautifully. Thank you much for your help! – justinpdove Sep 15 '15 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.