You need to sign in to do that
Don't have an account?

Owner Type is null in Apex Triger
Trying to get the code below to work. I want to create a task when a case owner is changed and assign that task to the new owner using a trigger on the Case object.
As the new case owner can be a user or a queue I want to check the new owner's type before creating the task. i.e. if Case Owner is now a queue, then don't create a task.
But for some reason newCase.Owner.Type returns null, when I'm expecting 'User'. Any ideas?
trigger tgrCaseAfterUpdate on Case (after update) { for (Case newCase : Trigger.new){ Case oldCase = System.Trigger.oldMap.get(newCase.Id); //get old Case if(oldCase.ownerID!=newCase.ownerID) &&(newCase.Owner.Type=='User')){ Task act = new Task(); act.Subject = 'Case '+newCase.casenumber+' has been assigned to you'; act.Description = 'Case '+newCase.casenumber + ' has been assigned to you. Click Related To link to view case details.'; act.ownerId = newCase.ownerID; act.Status = 'Not Started'; act.whatId = newCase.id; insert act; } } }
Hi irirobins,
You need to query for this value: newCase.Owner.Type. Since this is a trigger on the Case object and the Owner.Type field exists on the User object you will not have context to that depth of data from the trigger's sobject.
Hope that helps.
From my experience you only get the "top-level" detail from records in triggers.
Ok that makes sense. Thanks guys
Ok I now have the trigger working using the code below. But it appears to be inserting the task twice. My initial thought was that a case workflow rule was updating the case and therefore running the after update trigger twice. But after checking the debug log, there's no record of a workflow criteria being met. I even deactivated all the workflow rules on Case and I'm still getting the duplicate task. And looking at the debug log I only see the call to the after update trigger once.
any ideas?
All I've got is best coding practices:
Apologies for not replying sooner, I was off work on leave for a few weeks and only getting back to this now.
I'll update the trigger using the list to gather all tasks and inserting once. I've also updated the code with
to determine if the new task owner is a user. This removes the select from the for loop.