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
tengeltengel 

Retrieve Activity.WhoId.RecordTypeId

Hello, Apex newbie here.

 

Can anyone help me along in the right direction? I would like to update a a custom Activity checkbox field to TRUE if the WhoId of an Activity equals a certain record type. In my non-Apex layman's formula terms, it's something like this:

 

IF Activity.WhoId.RecordTypeId = "01260000000LyEL", My_Checkbox__c = TRUE, FALSE

 

Any ideas?

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
trigger updateTaskCheckbox on Task(before insert, before update) {
  map<id,lead> leads = new map<id,lead>();
  for(task record:trigger.new)
    if(record.whoid!=null&&record.whoid.getsobjecttype()==lead.sobjecttype)
    leads.put(record.whoid,null);
  leads.putAll([select id,recordtype.developername from lead where id in :leads.keyset()]);
  for(task record:trigger.new)
    record.My_Checkbox__c = leads.containskey(record.whoid) && leads.get(record.whoid).recordtype.developername=='Record_Type_Name';
}

Replace all instance of "lead" with "contact" if that is the object you're using instead. Don't use an ID value, because that will make your work more challenging later. Use the Developer Name instead.

 

This is a basic aggregate-query-update pattern. First, I gather all record IDs that are leads. Then, I query those leads for their record type. Finally, I update the tasks with the information. Note that I can assign a boolean result directly to a checkbox, which removes the need to say "if(...) ... checkbox = true; else checkbox = false;" This comes in handy when you're trying to bump up your code coverage.

 

Edit: Fixed typo.

All Answers

sfdcfoxsfdcfox
trigger updateTaskCheckbox on Task(before insert, before update) {
  map<id,lead> leads = new map<id,lead>();
  for(task record:trigger.new)
    if(record.whoid!=null&&record.whoid.getsobjecttype()==lead.sobjecttype)
    leads.put(record.whoid,null);
  leads.putAll([select id,recordtype.developername from lead where id in :leads.keyset()]);
  for(task record:trigger.new)
    record.My_Checkbox__c = leads.containskey(record.whoid) && leads.get(record.whoid).recordtype.developername=='Record_Type_Name';
}

Replace all instance of "lead" with "contact" if that is the object you're using instead. Don't use an ID value, because that will make your work more challenging later. Use the Developer Name instead.

 

This is a basic aggregate-query-update pattern. First, I gather all record IDs that are leads. Then, I query those leads for their record type. Finally, I update the tasks with the information. Note that I can assign a boolean result directly to a checkbox, which removes the need to say "if(...) ... checkbox = true; else checkbox = false;" This comes in handy when you're trying to bump up your code coverage.

 

Edit: Fixed typo.

This was selected as the best answer
tengeltengel

Thanks, @sfdcfox!

 

I moved your code into my sandbox and tested it out on lead, but I keep getting this error:

 

Error: Compile Error: Method does not exist or incorrect signature: [MAP<Id,Lead>].puts(Id, NULL) at line 5 column 5

 Sorry, I am a newb or else I'd probably be better at troubleshooting this myself :(

sfdcfoxsfdcfox
It should have been "put" instead of "puts". I managed to add it in by accident. That's what I get for not proofreading.
tengeltengel

Wow, thank you @sfdcfox, that trigger ROCKS. Can I be a complete mooch and ask for a test class? If it's any consolation, I understood the trigger so clearly that I should be able to also understand the test and then never have to bother wizards like you on these boards again (or at least won't have to for a while!).

sfdcfoxsfdcfox
@isTest(SeeAllData=true)
class TestTrigger {
  static testMethod void testTrigger() {
    RecordType testType = [SELECT Id FROM RecordType WHERE SObjectType='Lead' AND DeveloperName='Record_Type_Name'];
    Lead[] leadRecords = new Lead[] {
      new Lead(LastName='Test',Company='Test',RecordTypeId=testType.Id), // Test Checkbox
      new Lead(LastName='Test',Company='Test',RecordTypeId=null)};      // Test unchecked
    insert leadRecords;
    Task[] t = new Task[] {
      new Task(WhoId=leadRecords[0].Id,Subject='Test',MyCheckbox__c=false), // Should be checked later
      new Task(WhoId=leadRecords[1].Id,Subject='Test',MyCheckbox__c=true); // should be unchecked later
    insert t;
    t = [SELECT Id,MyCheckbox__c FROM Task WHERE Id = :t.Id ORDER BY Id ASC];
    System.assert(true,t[0].MyCheckbox__c); // Should now be checked
    System.assert(false,t[1].MyCheckbox__c); // Should now be unchecked
  }
}

Your milage may vary (validation rules, required fields, etc), but this is the basic framework I'd use.

 

Don't worry about hassling us; we do this voluntarily.