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
uma52551.3972270309784705E12uma52551.3972270309784705E12 

Create Task on Case object

Hi All,

Please correct my trigger. When a case is created it should insert a task.The "Assigned to"(Owner) field should be the lookup to Account that is on the case and from there to another custom object called underwriters inside this field called UW_User__c.

trigger TriggerToInsertTask on Case (after insert) {
List<RecordType> rdType = [Select Id from RecordType where SObjectType = 'Case' and Name = 'Bintech' Limit 1];
List<Case> ca = [Select Id,Type,RecordType.Name,Account.Commercial_Lines_Underwriter__r.UW_User__c,CreatedDate From Case Where RecordType.Name = 'Bintech' and Type = 'Loss Runs' Limit 1];
List<Task> tk = new List<Task>();
for(Case c: Trigger.new){
Task ts = new Task();
if(c.Account!=NULL && c.Policy__c!=NULL &&c.RecordType.Name=='Bintech')
ts.WhatId = ca[0].Id;
//ts.RecordType.Id = '012E000000022ZQ';
ts.Subject = 'Loss Run Case Follow-Up';
ts.Priority = 'Medium';
ts.ActivityDate = Date.valueOf(ca[0].CreatedDate)+7;
ts.OwnerId  =  ca[0].Account.Commercial_Lines_Underwriter__r.UW_User__c;
       }
  insert ca;
}

Thanks!
AlSawtoothAlSawtooth
What compile error are you running into?
uma52551.3972270309784705E12uma52551.3972270309784705E12
 Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger TriggerToInsertTask caused an unexpected exception, contact your administrator: TriggerToInsertTask: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Assigned To ID: owner cannot be blank: [OwnerId]: Trigger.TriggerToInsertTask: line 18, column 1
AlSawtoothAlSawtooth
I think that Assigned To (Owner) can only be linked with a User (standard SF object), and you're trying to link it to a custom object (UW_User__c).