简体   繁体   English

来自多态域的非法多态分配[SOBJECT:User,SOBJECT:Calendar]

[英]Illegal polymorphic assignment from polymorphic domain [SOBJECT:User, SOBJECT:Calendar]

I am writing a task's trigger and getting an error in salesforce Illegal polymorphic assignment from polymorphic domain [SOBJECT:User, SOBJECT:Calendar] 我正在编写任务的触发器,并从多态域中发现Salesforce中的错误多态分配[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; 错误是在线历史记录。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. 当我编写history.User__c = updated_tasks.get(i).owner.id时,没有错误,但是当我尝试从该ID获取用户的电子邮件地址时,则没有显示与此ID对应的用户。 How do I get Owner's user id from Task SObject's owner field. 如何从任务SObject的所有者字段中获得所有者的用户ID。 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? 我认为错误是由于所有者是对[SObject.User,SObject.Calender]的查找。因此所有者的ID应该与User'id不同。但是如何仅从Task对象的所有者字段中获取用户的ID?

You are so close. 你好亲近 The syntax is: 语法为:

history.User__c=updated_tasks.get(i).OwnerId;

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. task.Owner字段是一个SObject,task.Owner.Id有效,但是未在触发器上下文中填充所引用的值。

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. 您的触发器编写得不太好,它在循环中有一个dml语句,并且似乎没有从任务历史记录到任务的查找,我在下面的更新示例中引用了一个。

trigger Status_Change on Task (after update) {
    List<Task_History__c> histories = new List<Task_History__c>();
    Task oldValue;
    for(Task task : Trigger.new) {
        oldValue = Trigger.oldMap.get(task.Id);
        histories.add(new Task_History__c(
            Name=task.Subject,
            OldValue__c=oldValue.Status,
            NewValue__c=task.Status,
            User__c=task.OwnerId,
            //This should be created as well
            Task__c=task.Id
        ));
    }
    if(histories.size() > 0) {
        insert histories;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM