You need to sign in to do that
Don't have an account?
Activity to Account & Opportunity Update Class
Hello
I have a scenario when I need to upate Account and Opportunity fields (Next Step and Next Step Due Date) with Open Task Details (Next Step = Subject, Next Step DD = Due Date). if I have several tasks, the earlist one is displayed. If a task is closed, the next open task is displayed on Account / Opportunity.
Originaly, I had 2 triggers located under activities (one for accounts and one for activities). I wasn't sure if this was efficient so I changed that to 1 trigger that fires based on condition and launch a class.
trigger TasksToObjects on Task (after insert, after update) { Set<String> acctIdsSet = new Set<String>(); for(Task t: Trigger.new) { String wId = t.WhatId; if (wId != null && wId.startsWith('001') ) { acctIdsSet.add(wId); TaskToObject.Account(acctIdsSet); } if (wId != null && wId.startsWith('006') ) { acctIdsSet.add(wId); TaskToObject.Opportunity(acctIdsSet); } } }
public class TaskToObject { public static void Account(Set<String> acctIdsSet ) { List<Account> acctList = [SELECT Id, Next_Step__c, Next_Step_Due_Date__c, (SELECT Task.id, Task.Subject, Task.ActivityDate FROM Tasks WHERE Task.ActivityDate != NULL AND Task.Status != 'Completed' ORDER BY Task.ActivityDate ASC limit 1) FROM Account WHERE Id IN :acctIdsSet ]; if (acctList.size() > 0) { for(Account acct: acctList){ if (acct.Tasks.size() != 0) { acct.Next_Step__c = acct.Tasks[0].Subject; acct.Next_Step_Due_Date__c = acct.Tasks[0].ActivityDate; } else { acct.Next_Step__c = 'No current activity'; acct.Next_Step_Due_Date__c = null; } } try { update acctList; } catch(Exception ex) { System.debug('Problem updating the Accounts!'); } } } public static void Opportunity(Set<String> acctIdsSet ) {
List<Opportunity> oppList = [SELECT Id, NextStep, Next_Step_Due_Date__c, (SELECT Task.id, Task.Subject, Task.ActivityDate FROM Tasks WHERE Task.ActivityDate != NULL AND Task.Status != 'Completed' ORDER BY Task.ActivityDate ASC limit 1) FROM Opportunity WHERE Id IN :acctIdsSet ]; if (oppList.size() > 0) { for(Opportunity opp: oppList){ if (opp.Tasks.size() != 0) { opp.NextStep = opp.Tasks[0].Subject; opp.Next_Step_Due_Date__c = opp.Tasks[0].ActivityDate; } else { opp.NextStep = 'No current activity'; opp.Next_Step_Due_Date__c = null; } } try { update oppList; } catch(Exception ex) { System.debug('Problem updating the Opportunities!'); } } } }
The trigger & class work, I'm trying to determine if it is written in the most effieicnt way to write this (as this is my 1st time writing Apex Class).
Thanks!
Itay
did someone reviewed this? Is it written in efficently?