I am writing a task’s trigger and getting an error in salesforce Illegal polymorphic assignment from polymorphic domain [SOBJECT:User, SOBJECT:Calendar]
trigger Status_Change on Task (after update) {
List<Task>updated_tasks=trigger.new;
List<Task> tt=trigger.old;
Task_History__c history=new Task_History__c();
Integer i=0;
for(i=0;i<updated_tasks.size();i++)
{
history.Name=tt.get(i).Subject;
history=new Task_History__c();
history.OldValue__c=tt.get(i).Status;
history.NewValue__c=updated_tasks.get(i).Status;
history.User__c=updated_tasks.get(i).Owner;
insert history;
}
}
error is on line
history.User__c=updated_tasks.get(i).Owner;
When I write history.User__c=updated_tasks.get(i).owner.id then there is no error
but when I tried to get a User’s email address from this id then its showing no user corresponding to this id. How do I get Owner’s user id from Task SObject’s owner field. I think error is due to Owner is a lookup to [SObject.User,SObject.Calender].so owner’s id should be different from User’id .but how to get only User’s id from Owner’s field in Task object?
You are so close. The syntax is:
You were correct. the task.Owner field is an SObject, task.Owner.Id is valid, but the value being referenced is not populated in the trigger context.
Your trigger is not very well written, it has a dml statement in a loop, and there doesn’t appear to be a lookup from the task history to the task, I have referenced one in the updated example below.