Apex Code: Track Sales Rep Tasks and Set Follow Up Dates

public with sharing class SalesRepActivity  {
	
	public void updateSalesRepActivity(Set<Id> taskIds){
		try{
		
		for (Task task : [Select Id, Type, Subject, Status, AccountId, Account.Owner.Id, OwnerId, Detailed_Type__c from Task where Id IN : taskIds]){
			if (task.AccountId != null){
				if (task.Account.Owner.Id == task.OwnerId){
					Account accountToUpdate = new Account(Id=task.AccountId);

					if (task.type.equals('Sent Email') && task.subject.containsIgnoreCase('Mass Email:')){
						accountToUpdate.Last_Mass_Email__c = Date.today();
					}
					else if ((task.type.equals('Sent Email') || task.type.equals('Clearslide Email Pitch')) && task.subject.containsIgnoreCase('Email:')){
						accountToUpdate.Last_Non_Mass_Email__c = Date.today();
					}
					else if ((task.type.equals('Sent Email') && task.subject.containsIgnoreCase('re:')) && (task.Detailed_Type__c.equals('Connected') || task.Detailed_Type__c.equals('Meet') || task.Detailed_Type__c.equals('Meet outside'))){
						accountToUpdate.Last_Email_Connect__c = Date.today();
						accountToUpdate.Last_connected_date__c = Date.today();
					}
					else if (task.type.equals('Outbound Call') && task.status.equals('Completed') && task.Detailed_Type__c.equals('Connected')){
						accountToUpdate.Last_Call_Connect__c = Date.today();
						accountToUpdate.Last_connected_date__c = Date.today();
						accountToUpdate.Last_Call_attempt__c = Date.today();
					}
					else if (task.type.equals('Outbound Call')){
						accountToUpdate.Last_Call_attempt__c = Date.today();
					}
					else if (task.type.equals('Inbound Call')){
						accountToUpdate.Last_Call_attempt__c = Date.today();
						accountToUpdate.Last_connected_date__c = Date.today();
						accountToUpdate.Last_Call_Connect__c = Date.today();
					}

					update accountToUpdate;
				}
			}
		}

		}catch(Exception e){
			System.debug(e.getMessage());
		}
	}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: