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
irlrobinsirlrobins 

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;
        }
    }
}
Shoby Abdi.ax910Shoby Abdi.ax910

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. 

fgwarb_devfgwarb_dev

From my experience you only get the "top-level" detail from records in triggers.

 

 

for(Object o : trigger.new()){
 o.OwnerId; //accessible
 o.Owner.Name; //NOT accessible
 o.Field__c; //accessible
 o.Field__r.Name; //NOT accessible
}

 

 

irlrobinsirlrobins

Ok that makes sense. Thanks guys

irlrobinsirlrobins

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?

 

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){ //Owner changed
            Case c =[Select owner.type from Case where id = :newCase.ID];
             if(c.Owner.type=='User'){ //only create task is new owner is a user
                Task act = new Task();
                String subject = 'Case '+newCase.casenumber has been assigned to you';
                act.Subject =  subject;
                String description = subject + '. Click Related To link to view case details.';               
                act.Description = description;
                act.ownerId = newCase.ownerID;
                act.Status = 'Not Started';
                act.whatId = newCase.id;
                insert act;
           }
        }
        
    }
}

 

fgwarb_devfgwarb_dev

 

trigger tgrCaseAfterUpdate on Case (After update) {
List<Task> newTasks = new List<Task>();
    for (Case newCase : Trigger.new){        
        Case oldCase = System.Trigger.oldMap.get(newCase.Id); //get old Case
        if(oldCase.ownerID!=newCase.ownerID){ //Owner changed
            Case c =[Select owner.type from Case where id = :newCase.ID];
             if(c.Owner.type=='User'){ //only create task is new owner is a user
                Task act = new Task();
                String subject = 'Case '+newCase.casenumber has been assigned to you';
                act.Subject =  subject;
                String description = subject + '. Click Related To link to view case details.';               
                act.Description = description;
                act.ownerId = newCase.ownerID;
                act.Status = 'Not Started';
                act.whatId = newCase.id;
                newTasks.add(act);
           }
        }
    }
if(newTasks.size() > 0) insert newTasks;
}

 

All I've got is best coding practices:

 

  1. Don't do queries inside loops
  2. Don't do DML inside loops
And debugging suggestions:
  1. Turn on debug logging for your user account, replicate the error.  This should confirm whether the trigger is firing twice (you'll have 2 debug logs).  
  2. What are the IDs of the two tasks and their createddate values?  
  3. You could add a new field to cases "Transfer Task Sent", then check it inside your loop, see if this stops the second task...
-Curious to learn the answer

 

 

irlrobinsirlrobins

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

 

if(!newOwnerString.startsWith('00G')){ //not a queue, therefore user

 

to determine if the new task owner is a user. This removes the select from the for loop.