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
Michael HaaseMichael Haase 

Field update on Contact record when new activity with Record Type 'Log Call' was created

Hi everyone. I am new to APEX and need some help.

I need to update the date field "Last_Reseach_Call__c) on the contact record always when a new task with record type "Log Call" was created. This field update cannot be done via workflow, therefore I am trying to solve the problem with an Apex trigger.

Here is the code I have so far:

trigger UpdateContactLastActivity on Task (after insert, after update)
 {

// The set used to hold the Ids of the Contacts that need to be updated
Set <Id> ContactIds = new Set <Id> ();

// The actual Contacts to save
List <Contact> ContactsToUpdate = new List <Contact> ();

// Now we go through and if we find Tasks that are marked as Research Call, then update the Contact with "Last Research Contact" date
for (Task t : Trigger.new)
{
if (t.RecordType)
{
ContactIds.add (t.Contact);
}
}

// Now have the list of Contacts, fetch the associated Contact records and updatethem
if (!ContactIds.isEmpty () )
{
for (Contact c : [select Id, LastName from Contact where Id in :ContactIds])
{
contact.Last_Research_Contact__c = Today();
ContactsToUpdate.add (c);
}

update ContactsToUpdate;
}
}


I currently cannot save the code as I get an error ЭCompile Error: Invalid field Contact for SObject Task at line 15 column 17".

Could please someone advise where the problem is and how the correct code should look like?

Thanks an advance!
SonamSonam (Salesforce Developers) 
On the Task object, Contact is referred a s the whoID and related to (Account/Opp etc) as whatID. Try updating and run the code.
Deepak BalurDeepak Balur
Michael, Run this query in Developer Console, the field is WhoID that you are loooking for.
Select t.Who.Name, t.Who.FirstName, t.Who.LastName, t.WhoId, t.Id, t.description From Task t where t.Who.Id <> '' -- hope this will help in understanding

You need to use WhoId in this line for (Contact c : [select Id, LastName from Contact where Id in :ContactIds])
siddarth rajsiddarth raj

Michael

FYI No standard filed Contact on Object Task. So that is obivious error.

I would suggest use look up relation field 'Who' (whoid) to identify the contact for  TASK. Wish this will help you.

ALL THE BEST

---Siddarth

Michael HaaseMichael Haase
Thanks for the advise so far.  Unfortunately it led to a new error message. This is my new code:

trigger UpdateContactLastActivity on Task (after insert, after update)
 {

// The set used to hold the Ids of the Contacts that need to be updated
Set <Id> ContactIds = new Set <Id> ();

// The actual Contacts to save
List <Contact> ContactsToUpdate = new List <Contact> ();

// Now we go through and if we find Tasks that are marked as Research Call, then update the Contact with "Last Research Contact" date
for (Task t : Trigger.new)
{
if (t.RecordType)
{
ContactIds.add (t.Contact);
}
}

// Now have the list of Contacts, fetch the associated Contact records and updatethem
if (!ContactIds.isEmpty () )
{
for (Contact c : [select t.Who.Name from Contact where t.Who.Id in :ContactIds])
{
contact.Last_Research_Contact__c = Today();
ContactsToUpdate.add (c);
}

update ContactsToUpdate;
}
}

I get now the error:
Error: Compile Error: Didn't understand relationship 't' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 22 column 18

Any idea where the problem is now?
SonamSonam (Salesforce Developers) 
You should be using the SOQL as below:

Contact c : [select Name from Contact where ID in :ContactIds])
Michael HaaseMichael Haase
Hi @sonam! I really appreciate your help. However, I cannot get any further. This is my first try to write an Apex Trigger. I have absolutely minimum knowledge. ((

This is my new code:

trigger UpdateContactLastActivity on Task (after insert, after update)
 {

// The set used to hold the Ids of the Contacts that need to be updated
Set <Id> ContactIds = new Set <Id> ();

// The actual Contacts to save
List <Contact> ContactsToUpdate = new List <Contact> ();

// Now we go through and if we find Tasks that are marked as Research Call, then update the Contact with "Last Research Contact" date
for (Task t : Trigger.new)
{
if (t.RecordType)
{
ContactIds.add (t.Contact);
}
}

// Now have the list of Contacts, fetch the associated Contact records and updatethem
if (!ContactIds.isEmpty () )
{
for (Contact c : [select Name from Contact where ID in :ContactIds])
{
contact.Last_Research_Contact__c = Today();
ContactsToUpdate.add (c);
}

update ContactsToUpdate;
}
}


Question: Why we dont use the previously suggested WhoID in line 22?

After using line 22 as you suggested I get the following error: Error: Compile Error: Invalid field Contact for SObject Task at line 15 column 17
Michael HaaseMichael Haase
After some seacrh I found a piece of code that workes fine after some minor amendments. I will publish this here in case somebody else comes over the same problem.

The trigger is:

trigger UpdateInTouchDate on Task (after insert, after update) {
Set<String> whoIds = new Set<String>();

    for (Task t : Trigger.new) {
         whoIds.add(t.WhoId);
    }

    List<Contact> cons = [SELECT Id, Last_Research_Contact__c FROM Contact WHERE Id =: whoIds];
   
    Map<String, Task> taskMap = new Map<String, Task>();

    for (Task t : Trigger.new){
        taskMap.put(t.WhoId, t);
    }
        
    for (Contact c : cons) {
    if (taskMap.containsKey(c.Id)) {
         c.Last_Research_Contact__c = taskMap.get(c.Id).ActivityDate;
    }
}
update cons;
}


The test class for it is:

@isTest
private class TestUpdateTouchDate {

static testMethod void testUpdateInTouch() {

  Contact c = new Contact(FirstName = 'Test', LastName = 'Test');
  insert c;

  Date d = Date.newInstance(2014, 5, 29);
  Task t = new Task(ActivityDate = d, WhoId=c.id, Status='Not Started',Priority='Normal'); //Associating with contact
  System.debug('Date before updating in touch date: ' + t.ActivityDate );
  insert t;

  // Query on Contact as the Trigger is populating Contact field
  List<Contact> con = [SELECT Last_Research_Contact__c FROM Contact WHERE Id =:c.Id limit 1];

  if (con.size() > 0) {
       c = con.get(0);
  }

      System.debug('Price after trigger fired: ' + t.ActivityDate);

     System.assertEquals(t.ActivityDate, con[0].Last_Research_Contact__c);
   }
}