4

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

    //BELOW IS WHERE I RUN INTO THE PROBLEM
    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;
            taskList.add(t);
        }
    }

    //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.

1 Answer 1

12

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
}
2
  • Thank you, FYI, you needed one more = on your evaluation. :)
    – Matt M
    Commented Apr 27, 2016 at 19:56
  • @Matt D'oh! Fixed.
    – Adrian Larson
    Commented Apr 27, 2016 at 20:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .