+ Start a Discussion
John GerhardJohn Gerhard 

trigger task email wont fire

Hello,

I have a trigger on the task object before insert and before update. It works on the vast majority of my tasks, however it seems to not fire when a user uses the send email button on a contact. I can click edit and save and it will update fine but it will not fire on the insert of the task. Are the email tasks inserted in a special way compared to other tasks?
 
trigger TaskBeforeInsertBeforeUpdate on Task (before insert, before update) {

	// Initialize our lists we will use to query the related accounts and contacts later
	List<ID> accIDList = new List<ID>();
	List<ID> conIDList = new List<ID>();

	// Initialize our lists we will use to store our object querys
	List<Account> accList = new List<Account>();
	List<Contact> conList = new List<Contact>();


	// Loop through our tasks and assign the LCT variables, at the end we will build our lists of objects to query
	for (Task taskTrigger : Trigger.new) {
		String accIDString = String.Valueof(taskTrigger.WhatID);
		String conIDString = String.Valueof(taskTrigger.WhoID);

		if(conIDString == NULL){
			conIDString = '0';
		}

		if(accIDString == NULL){
			accIDString = '0';
		}

		if(taskTrigger.Call_Direction__c != NULL){
			taskTrigger.CallDisposition = taskTrigger.Call_Result__c;
			taskTrigger.CallType = taskTrigger.Call_Direction__c;
		}
		if(accIDString.startsWith('001')){
			taskTrigger.AccountLookup__c = taskTrigger.WhatID;
		}
		if(conIDString.startsWith('003')){
			taskTrigger.ContactLookUpField__c = taskTrigger.WhoID;
		}

		if(taskTrigger.qbdialer__Call_Date_Time__c != NULL && taskTrigger.status == 'Completed' && taskTrigger.AccountLookup__c != NULL){
			accIDList.add(taskTrigger.WhatID);
		}

		if(taskTrigger.ContactLookUpField__c != NULL && taskTrigger.status == 'Completed' && taskTrigger.qbdialer__Call_Date_Time__c != NULL){
			conIDList.add(taskTrigger.WhoID);
		}
	}

	// Query and update our contacts
	if(conIDList.size()>0){
		conList = [SELECT ID, qbdialer__Dials__c, AccountID, qbdialer__LastCallTime__c, OpportunityStage__c FROM Contact WHERE ID IN: conIDList];

		for(Task taskTrigger : Trigger.new){
			for(Contact con : conList){
				if(taskTrigger.WhoID == con.Id && (taskTrigger.qbdialer__Call_Date_Time__c > con.qbdialer__LastCallTime__c || con.qbdialer__LastCallTime__c == NULL)){
					con.qbdialer__LastCallTime__c = taskTrigger.qbdialer__Call_Date_Time__c;
					if(con.qbdialer__Dials__c == NULL){
						con.qbdialer__Dials__C = 0;
					}
					con.qbdialer__Dials__C += 1;
					if(taskTrigger.AccountLookup__c != con.AccountID){
						taskTrigger.AccountLookup__c = con.AccountID;
						taskTrigger.WhatID = con.AccountID;
						accIDList.add(con.AccountID);
					}
				}
			}
		}
		update conList;
	}

	// Query and update our accounts
	if(accIDList.size()>0){
		accList = [SELECT ID, qbdialer__Dials__c, qbdialer__LastCallTime__c, Type, Status__C FROM Account WHERE ID IN: accIDList];

		for(Task taskTrigger : Trigger.new){
			for(Account acc : accList){
				if(taskTrigger.AccountLookup__c == acc.Id && (taskTrigger.qbdialer__Call_Date_Time__c > acc.qbdialer__LastCallTime__c || acc.qbdialer__LastCallTime__c == NULL)){
				//if(taskTrigger.AccountLookup__c == acc.Id){
					acc.qbdialer__LastCallTime__c = taskTrigger.qbdialer__Call_Date_Time__c;
					if(acc.qbdialer__Dials__c == NULL){
						acc.qbdialer__Dials__c = 0;
					}
					acc.qbdialer__Dials__C += 1;
				}
			}
		}
		update accList;

	}

 
Best Answer chosen by John Gerhard
Ryan GreeneRyan Greene
I believe emails are under a different Object. You would need to create a new trigger with the object "EmailMessage".

Documentation here: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_emailmessage.htm

All Answers

Ryan GreeneRyan Greene
I believe emails are under a different Object. You would need to create a new trigger with the object "EmailMessage".

Documentation here: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_emailmessage.htm
This was selected as the best answer
John GerhardJohn Gerhard
Perfect, thank you sir. So is it fair to say, it creates a record for EmailMessage which then creates a Task record but saves it to the database without firing any triggers on the task?
Ryan GreeneRyan Greene
Emails, Tasks, and Events are all separate objects, but fall under the category Activity in SF. So, in your case, emails come in and are placed under Activities, no actual Task is created. These are all still confusing to me! :) Good luck