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
Marco PinderMarco Pinder 

Task Trigger - Help Required Please

Hi all,

 

I have written a very basic trigger that assigns a Type value to a Task that is created through the Email Bcc functionality that Salesforce provides.

 

I have tested the trigger and it works when sending a single Email into Salesforce. The code for this trigger is:

 

trigger EmailTask on Task (before insert, before update) {
    
        for (Task t : trigger.new){
            if (t.Subject.contains('Email:')){
                if (t.Type == 'Other - see subject'){
                    t.Type = 'Email';
                }
            }
        }
}

 However, from working on a previous trigger, I know that in order to meet Salesforce limits I must handle for updating large data sets.

 

So I have tried to modify this trigger in order to comply with these limits and I have come up with the following:

 

trigger EmailTask2 on Task (before insert, before update){

    Set<Id> taskId = new Set<Id>();
    List<Task> updated = new List<Task>();
    for (Task t : trigger.new){
        taskId.add(t.Id);
    }
    List<Task> tasks = [SELECT Id, Subject, Type FROM Task WHERE Id in: taskId];
        for (Task t : trigger.new){
            for (Task task : tasks){
                if (task.Subject.contains('Email:')){
                    if (task.Type == 'Other - see subject'){
                        task.Type = 'Email';
                        updated.add(task);
                    }
                }
            }
        }
        if (updated.size()>0){
            upsert updated;
        }
}

 However this does not work as any Emails I send to Salesforce are recorded with their default Type value ('Other - see subject') not the value of 'Email' that I intend.

 

I am not a coder and have stumbled my way through this, so would really appreciate a bit of guidance on how to tweak my code in order to work.

 

Thanks,

 

Marco

Best Answer chosen by Admin (Salesforce Developers) 
Marco PinderMarco Pinder

Thanks for all of your suggestions. I have decided to stick with the original trigger and am happy to report that after writing a test class and deploying to production it all works perfectly.

 

Regards,

 

Marco

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

Below is your modified trigger, hope this will work for you

 

trigger EmailTask2 on Task (before insert, before update)
{
                Set<Id> taskId = new Set<Id>();
                List<Task> updated = new List<Task>();
                for (Task t : trigger.new)
                {
                                taskId.add(t.Id);
                }
                List<Task> tasks = [SELECT Id, Subject, Type FROM Task WHERE Id in: taskId];
                for (Task t : tasks)
                {
                                if (t.Subject.contains('Email:'))
                                {
                                                if (t.Type == 'Other - see subject')

                                                {
                                                                t.Type = 'Email';
                                                                updated.add(t);
                                                }
                                }
                }
                if (updated.size()>0)
                {
                                upsert updated;
                }
}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

Marco PinderMarco Pinder

Hi S Jain,

 

Thansk for your feedback, however I noticed that your first response said that the trigger was 'bulkified' already. What did you mean by that? I just tried inserting 5000 new tasks into Sandbox that caused the original trigger to fire and I didn't run into any problems. Should I just use the original one if that's the case?

 

Thanks,

 

Marco

Navatar_DbSupNavatar_DbSup

Hi,

 

You should use second one because we need to use set and map for bulkyfication.

UVUV

Hi Macro,

Your first trigger is good enough to deal with heavy data and if you want to use map and sets then u can use trigger.newMap instead of iterating through each record and adding them in a list.


this way-

List<Opportunity> opptysClosedLost = [select id, name, closedate, stagename
                from Opportunity where
                accountId IN :Trigger.newMap.keySet()

 

You optimized trigger-

trigger EmailTask2 on Task (before insert, before update)
{
                List<Task> updated = new List<Task>();
                for (Task t : [SELECT Id, Subject, Type FROM Task WHERE Id in: Trigger.newMap.keySet()])
                {
                                if (t.Subject.contains('Email:'))
                                {
                                                if (t.Type == 'Other - see subject')

                                                {
                                                                t.Type = 'Email';
                                                                updated.add(t);
                                                }
                                }
                }
                if (updated.size()>0)
                {
                                upsert updated;
                }
}

 

Pls go through apex best practices-

http://wiki.developerforce.com/page/Apex_Code_Best_Practices

Marco PinderMarco Pinder

Thanks for all of your suggestions. I have decided to stick with the original trigger and am happy to report that after writing a test class and deploying to production it all works perfectly.

 

Regards,

 

Marco

This was selected as the best answer
UVUV

Great to hear this Macro..:-)

Pls mark that as a solution for other;s benifit.

jaw999jaw999

When I try your optimized trigger I get

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger EmailTask2 caused an unexpected exception, contact your administrator: EmailTask2: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.EmailTask2: line 4, column 1

 

Though I can get it to work with (after insert)

 

I'd like before insert a little better though - any thoughts how could I get that to work? thanks