简体   繁体   English

Salesforce:无法使用触发器更新查找字段值

[英]Salesforce: Cannot update lookup field value with trigger

I'm just trying to update a lookup field value (Campagna_Account__c) on Account object using a trigger that should update it getting the value from a custom object (Sottocampagna__c). 我只是试图使用触发器更新Account对象上的查找字段值(Campagna_Account__c),该触发器应该更新它以从自定义对象(Sottocampagna__c)获取值。 I think the query is right because it runs well on Force.com Explorer, so I think the problem is somewhere else. 我认为查询是正确的,因为它可以在Force.com Explorer上很好地运行,所以我认为问题出在其他地方。

Here's the error line displayed when trigger fires: 这是触发触发器时显示的错误行:

data changed by trigger for field Campagna: id value of incorrect type: a06250000004FDNAA2 字段Campagna的触发器更改的数据:ID类型值错误:a06250000004FDNAA2

Here's my code: 这是我的代码:

trigger PopolaCampagna on Account (before insert, before update) {

    Sottocampagna__c var = [

        SELECT Campagna__r.Id 
        FROM Sottocampagna__c
        WHERE Name='Segugio'

        ];

for (Account a: Trigger.new) {

        a.Campagna_Account__c = var.Id;
    }    
}

Thank you for helping me! 感谢你们对我的帮助!

You are trying to assign Sottocampagna__c record's id into a.Campagna_Account__c using this code. 您正在尝试使用此代码将Sottocampagna__c记录的ID分配给a.Campagna_Account__c。 You should change it to be: 您应该将其更改为:

for (Account a: Trigger.new) {

    a.Campagna_Account__c = var.Campagna__r.Id;
}    

} }

Another thing which I would recommend is to change this query to: 我建议的另一件事是将此查询更改为:

Sottocampagna__c var = [

    SELECT Campagna__r.Id 
    FROM Sottocampagna__c
    WHERE Name='Segugio' limit 1

    ];

It will prevent exceptions if there are more than 1 record which suits query criteria. 如果有多个符合查询条件的记录,它将防止异常。 If you are not comfortable with this behaviour and potentially you could have multiple records which suits criteria it is better to use List<>. 如果您对此行为不满意,并且可能会有多个符合条件的记录,则最好使用List <>。 Let me know if it helps. 让我知道是否有帮助。

Also some tips on naming conventions: https://salesforce.stackexchange.com/questions/890/what-is-a-good-set-of-naming-conventions-to-use-when-developing-on-the-force-com 还有一些有关命名约定的提示: https : //salesforce.stackexchange.com/questions/890/what-is-a-good-set-of-naming-conventions-to-use-when-developing-on-the-force -com

Actually this should also work: 实际上,这也应该起作用:

    for (Account a: Trigger.new) {

    a.Campagna_Account__c = var.Campagna__c;
}    

} }

Sottocampagna__c var = [

    SELECT Campagna__c 
    FROM Sottocampagna__c
    WHERE Name='Segugio' limit 1

    ];

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

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