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
Garrett SamuelsGarrett Samuels 

Apex class or trigger to update Opportunity Last Modified Date when related Task or Event is updated/created

I have a requirement to have the Opportunity record Last Modified Date update when a related task or event is created/updated. I am looking for advice on how to achieve this. Is this possible through an Apex Class or Trigger? Please advise. 
Big EarsBig Ears

Garrett,

This would be possible with a trigger. something like an after update/insert/delete trigger that had some code like this:
 

list<id> oppIds = new list<id>();   

for(Task t : system.new){
   if(string.valueOf(t.what.id).startsWith('006')){
      oppIds.add(t.whatId);
   }
}

if(oppIds .size() > 0){
   updated oppIds;
}

This iterates through the tasks in a trigger and checks the "WhatId" field to see if it's linked to an Opportunity. Any Opportunities linked to a Task in this trigger will be updated.

Andy
Garrett SamuelsGarrett Samuels
Hi Andy,

Thank you for your response. I created the Trigger in Sandbox Developer Console. One thing I noticed was in Developer Console the Problems tab resulted in a row that states Problem: "unexpected token: list". That said, I saved the trigger (on Task sObject). Then I went to an opportunity that was last modified on 3/7/15. I added a related task to the opportunity then checked the opportunity last modified date and it did not change. What do you think is the next step? Thank you again.

Garrett
Leslie  KismartoniLeslie Kismartoni
I think you might need to query for the opportunities using the list, then call the update...
if (oppIds.size() > 0) {
  List<Opportunity> oppList = [SELECT Id FROM Opportunity WHERE Id IN :oppIds];
  update oppList;
}