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
Tristan JonesTristan Jones 

opportunity__r.Name coming up blank in task from Apex trigger

Hi, 

I'm trying to have the subject field of a task that is triggered in a custom object using an Apex trigger populate with the the name of the opportunity the object is related to. I can get the unique ID to show  when i reference the lookup field in the custom object ('opportunity__c') but when I use 'object__r.name' it comes up blank. Does anyone know what could be causing this? My trigger code is:

trigger TaskAllocation on Action__c (after insert) {    
    List<Task> taskList = new List <Task>();
    for (Action__c a:Trigger.New)
        
    {
      
    {
    Task t = new Task();
    
        t.OwnerID = a.Action_owner__c;
        t.WhatId = a.Id;
        t.Subject =  a.opportunity__r.Name;
        t.description = a.action__c;
        t.Status = 'Not Started';
        t.ActivityDate = a.deadline__c;
        t.priority = 'Normal';
    taskList.add(t);
    }
    
    }
    if(taskList.size()>0)
    insert taskList;
   
    
}


Cheers, 

Tristan
Best Answer chosen by Tristan Jones
Dilip_VDilip_V
Hi Tristan,

You have to query opportunity__r.Name using SOQL.Trigger.new list doesn't contain opportunity__r.Name(Action object nested related information) information.It contains only Action object information.Please use below code.
trigger TaskAllocation on Action__c (after insert) {    
    List<Task> taskList = new List <Task>();
    for (Action__c a:[select  Action_owner__c,Id,opportunity__r.Name,action__c,    deadline__c from  Action__C where Id in :trigger.newmap.keyset()])
        
    {
      
    {
    Task t = new Task();
    
        t.OwnerID = a.Action_owner__c;
        t.WhatId = a.Id;
        t.Subject =  a.opportunity__r.Name;
        t.description = a.action__c;
        t.Status = 'Not Started';
        t.ActivityDate = a.deadline__c;
        t.priority = 'Normal';
    taskList.add(t);
    }
    
    }
    if(taskList.size()>0)
    insert taskList;
   
    
}

Regards,
Dilip.

All Answers

Dilip_VDilip_V
Hi Tristan,

You have to query opportunity__r.Name using SOQL.Trigger.new list doesn't contain opportunity__r.Name(Action object nested related information) information.It contains only Action object information.Please use below code.
trigger TaskAllocation on Action__c (after insert) {    
    List<Task> taskList = new List <Task>();
    for (Action__c a:[select  Action_owner__c,Id,opportunity__r.Name,action__c,    deadline__c from  Action__C where Id in :trigger.newmap.keyset()])
        
    {
      
    {
    Task t = new Task();
    
        t.OwnerID = a.Action_owner__c;
        t.WhatId = a.Id;
        t.Subject =  a.opportunity__r.Name;
        t.description = a.action__c;
        t.Status = 'Not Started';
        t.ActivityDate = a.deadline__c;
        t.priority = 'Normal';
    taskList.add(t);
    }
    
    }
    if(taskList.size()>0)
    insert taskList;
   
    
}

Regards,
Dilip.
This was selected as the best answer
Tristan JonesTristan Jones
That worked great, thanks Dilip.