• Home
  • About
    • Leadership
    • Partners
    • Blogroll
  • Force-Squared Blog
    • Tips and Tricks
    • Configuration
    • Development
  • Support
    • Knowledge Base
    • Submit a Case
  • Is It Dreamforce Yet?

X-Squared On Demand

Salesforce solutions delivered

  • Home
  • About
    • Leadership
    • Partners
    • Blogroll
  • Force-Squared Blog
    • Tips and Tricks
    • Configuration
    • Development
  • Support
    • Knowledge Base
    • Submit a Case
  • Is It Dreamforce Yet?
  • Tips and Tricks
  • Configuration
  • Development
You are here: Home / Salesforce CRM / Development / Apex / Activity Type Field – Do Not Use

Activity Type Field – Do Not Use

March 21, 2011 by David Schach Leave a Comment

The field Event.Type (which is sort of the same as Task.Type) is a difficult field to use.  Here are a few reasons:
  • 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.
A tweet by Andy Ognenoff of Manpower (@aognenoff) revealed another limitation: Type is not available in Custom Report Types. That does it!  I'm done with this field.  Here's my solution.
  1. Work in a sandbox (duh)
  2. Create a picklist field on Activity (ActivityType__c).
  3. Disable all Activity workflow rules, Chatter feeds, triggers, etc.
  4. Using DataLoader, copy values from Type to ActivityType__c.
  5. Using Field Level Security, hide Type from all profiles.  It's just not necessary.
  6. Edit/reactivate all your workflow, feeds, triggers, etc.
  7. Use/adapt the code below.
  8. Test thoroughly and deploy to production.
The Salesforce Mobile application (at least for Blackberry) defaults to a Subject "Call: (212) 555-1212" style.  This will account for that.  It also handles emails sent with Email-To-Salesforce, which starts the subject with "Email:"
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 this:

  • Twitter
  • LinkedIn
  • Facebook
  • Pocket
  • Email

Related

Filed Under: Apex, Salesforce, Tips and Tricks Tagged With: Apex

← Which has the most potential for enterprise-wide adoption: iPad or Cr-48? Chatter BINGO Released Into The Wild →

Share Your ThoughtsCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Is it Dreamforce Yet?

Find out!

Find us on the AppExchange

Mass Update Contact Address

Get It Now


Recent Posts

  • Duplicate Record Item Enrichment and Auto-Deletion Code
  • Lightning Component With Running User Information
  • ChatterBINGO is Now Open-Source
  • Display Only My Role’s Records on a Report
  • Chatter Publisher Actions (Part 2): Object Custom Action

Post Categories

Salesforce Blogs

  • TehNerd
  • MK Partners
  • Vertical Coder
  • Technology, Coding and Bears… OH MY!
  • Embracing the Cloud
  • The Silver Lining
  • Decoding Salesforce
  • Arrowpointe
  • Salesforce Source
  • Force Monkey

Popular Tags

#df09 #df10 Akismet Apex AppBuilder AppExchange Appirio Astadia Blogs Chatter Configuration Content DreamFactory Dreamforce Eclipse IDE Enterprise Force.com Builder Force.com Platform Google Infowelders Integration Just for fun Lightning New Developments New Features Partner Program PersonAccount Projects Publisher Salesforce Salesforce1 Salesforce for Google Apps sfdcverse Sites Visualforce Web-to X-Squared On Demand

Find Us Online

  • Twitter
  • Facebook
  • LinkedIn
  • RSS

Subscribe

RSS Feed Comments

Subscribe via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 5,091 other subscribers

Copyright © 2008–2023 X-Squared On Demand · Genesis Framework by StudioPress · WordPress · Log in