- Type cannot be the controlling field for a dependent picklist.
- On an Event list view (/007), even when specifying record type, the Type field cannot be edited.
- Salesforce requires you to select a default value for this picklist, as well as a default value for inbound emails... yet when I bcc my Email-To-Salesforce address, the resulting tasks have no Type value.
- Work in a sandbox (duh)
- Create a picklist field on Activity (ActivityType__c).
- Disable all Activity workflow rules, Chatter feeds, triggers, etc.
- Using DataLoader, copy values from Type to ActivityType__c.
- Using Field Level Security, hide Type from all profiles. It's just not necessary.
- Edit/reactivate all your workflow, feeds, triggers, etc.
- Use/adapt the code below.
- Test thoroughly and deploy to production.
trigger TaskTrigger on Task (before insert, before update) { ActivityHelper.UpdateTaskType(Trigger.new); }
trigger EventTrigger on Event (before insert, before update) { ActivityHelper.UpdateEventType(Trigger.new); }This class isn't perfect, but it gets the job done. I should add try/catch statements in my test code (though I have heard recently that it's not a good idea to do so, as one may WANT the code to fail with an error if something doesn't go as expected).
// Written by David Schach, X-Squared On Demand public with sharing class ActivityHelper { public static void UpdateEventType(Event[] events){ for (Event e : events){ if(e.ActivityType__c == '' || e.ActivityType__c == null){ if(e.Subject.contains('Email')) e.ActivityType__c = 'Email'; else if(e.Subject.contains('Call')) e.ActivityType__c = 'Call'; else if (e.Subject.contains('Meet') || e.Subject.contains('Webinar')) e.ActivityType__c = 'Meeting'; } } } public static void UpdateTaskType(Task[] tasks){ for (Task t : tasks){ if(t.ActivityType__c == '' || t.ActivityType__c == null){ if(t.Subject.contains('Email ') || t.Subject.contains('Email:')) t.ActivityType__c = 'Email'; else if(t.Subject.contains('Call')) t.ActivityType__c = 'Call'; else if (t.Subject.contains('Meet') || t.Subject.contains('Webinar')) t.ActivityType__c = 'Meeting'; } } } private static Event newTestEvent(string subject){ Event e = new Event(); e.Subject = subject; e.startdatetime = datetime.now(); e.DurationInMinutes = 60; e.ActivityType__c = ''; // To account for default value. return e; } private static Task newTestTask(string subject){ Task t = new Task(); t.Subject = subject; t.Priority = 'Medium'; t.Status = 'New'; t.ActivityType__c = ''; // To account for default value. return t; } static TestMethod void TestEvents(){ test.starttest(); List<Event> ourevents = new List<Event>(); ourevents.add(newTestEvent('e1')); ourevents.add(newTestEvent('Email: Thank you')); ourevents.add(newTestEvent('Call: (212) 555-1212')); ourevents.add(newTestEvent('Webinar tomorrow')); ourevents.add(newTestEvent('Meet Jack')); ourevents.add(newTestEvent('Sales time')); insert ourevents; update ourevents; for (Event e : [SELECT id, Type from Event WHERE id in :ourEvents]){ system.debug('EVENT TYPE: ' + e.ActivityType__c); } test.stoptest(); } static TestMethod void TestTasks(){ test.starttest(); List<Task> ourTasks = new List<Task>(); ourTasks.add(newTestTask('e1')); ourTasks.add(newTestTask('Email: Thank you')); ourTasks.add(newTestTask('Call: (212) 555-1212')); ourTasks.add(newTestTask('Webinar tomorrow')); ourTasks.add(newTestTask('Meet Jack')); ourTasks.add(newTestTask('Sales time')); insert ourTasks; update ourTasks; for (Task t : [SELECT id, Type from Task WHERE id in :ourTasks]){ system.debug('TASK TYPE: ' + t.ActivityType__c); } test.stoptest(); } static testMethod void IndividualActivityTest() { Event e = newTestEvent('e1'); Task t = newTestTask('t1'); test.starttest(); insert e; insert t; e.subject = 'Meeting'; update e; e.subject = 'Call: Geoff Peterson'; update e; e.subject = 'Email: Here we go'; update e; e.subject = 'Webinar'; update e; t.subject = 'Meeting'; update t; t.subject = 'Call'; update t; t.subject = 'Email'; update t; t.subject = 'Webinar'; update t; test.stoptest(); } }
Share Your Thoughts