Need a little help. I can't figure out the correct syntax to utilize Task What.Type in my trigger. Can someone tell me what I am doing wrong?

trigger ININRelatedToFix on Task (after Insert, after Update) {

//Get Record Type    
RecordType Five9Rec = [select id from RecordType where name = 'Five9 Call' AND sobjecttype = 'Task' limit 1];
system.debug('RecordType= '+Five9Rec.Id);

//Create List for bulkified update
List<Task> taskList = new List<Task>();

//Loop through each task handed to trigger
for (Task T : Trigger.new) {

    //If Call is related to Employee Session instead of Employee

    If(T.What.Type.equals('Employee_Session__c') && T.RecordTypeId == Five9Rec.Id){

            //Get the employee
            Employee_Session__c Emp = [Select Employee__c FROM Employee_Session__c WHERE ID = :T.WhatId];

            //Fix Relationship
            T.WhatId = Emp.Employee__c;

    //Builify Update
    update tasklist;

The dev console isn't telling me I have anything wrong but via testing I know the trigger is not working.


You don't get cross-object fields in the trigger context. You would need to query for them. However, you can get the SObjectType instead and spare yourself the query.

if (T.WhatId != null && T.WhatId.getSObjectType() == Employee_Session__c.sObjectType)
    // logic here
  • Thank you, FYI, you needed one more = on your evaluation. :) – Matt M Apr 27 '16 at 19:56
  • @Matt D'oh! Fixed. – Adrian Larson Apr 27 '16 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.