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
Carrie FarnsworthCarrie Farnsworth 

INVALID_CROSS_REFERENCE_KEY, Assigned To ID: owner cannot be blank: [OwnerId] on trigger to create task

Hi All,

I made a simple trigger to create a task related to the Opportunity and assigned to the Opportunity Owner when a particular field on a related object is changed.  I'm getting Invalid Cross Reference: owner ID cannot be blank.  I read some other posts about the error and I somewhat know what the problem is, but I don't really understand how to fix it.  Can anyone help me please?

Here is my trigger:
trigger CreateTaskOnRed on project_cloud__Project__c (after update) {
	List<task> TasksToInsert = new List<task>();
    for (project_cloud__project__c pj: trigger.new) {
 		
        //only run if the project health is red
        if (pj.project_cloud__Health__c == 'Red') {
                      
        //compile data for affected project
			project_cloud__project__c[] pjs = [
                SELECT name, id, ccpe_ocp__Opportunity__c  
                FROM project_cloud__project__c 
                WHERE project_cloud__project__c.id 
                IN :Trigger.new];  
                 
        //create task for opportunity owner
        	task t = new task();
            t.RecordTypeId = '012i0000000xowf';
			t.ownerid = pj.ccpe_ocp__Opportunity__r.owner.id;
            t.whatID = pj.ccpe_ocp__Opportunity__r.id;
            t.ActivityDate = System.today();
            t.Subject = 'Other';
            t.Type__c = 'Other';
            t.Account_Type__c = 'Other';
            t.Drop_Type__c = 'Other';
            t.Broker_Care_Type__c = 'General Inquiries';
            t.Priority = 'High (High Priority)';
            t.Status = 'open';
            TasksToInsert.add(t);
        }            
            else{}
            if(TasksToInsert!=null && !TasksToInsert.isEmpty()){
                ProjectTriggerHelper.createTasks(TasksToInsert);
	     }
    }
}


And the helper:
 
public class ProjectTriggerHelper	{
	
	public static void createTasks(List<Task> TasksToInsert){
		try{
		       insert TasksToInsert;
		}
		catch(Exception err){
			system.debug('\n Error while creating tasks. \n'
                                  + err.getMessage() + ' \n'
                                  + err.getStackTraceString());			
		}
	}

}

 
SivaGSivaG
Hi,

Please try changing the line as shown below.

 t.ownerid = pj.ccpe_ocp__Opportunity__r.ownerId;

PS: If this solves your problem, please mark this as 'Best Answer'

Thanks
Kumar
Carrie FarnsworthCarrie Farnsworth
sorry, I thought I had fixed that before I posted the code.  I tried both ways (.ownerId and owner.id) and get the same error both ways

Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Assigned To ID: owner cannot be blank: [OwnerId] Class.ProjectTriggerHelper.createTasks: line 5, column 1 Trigger.CreateTaskOnRed: line 33, column 1