[英]Receiving sObject error: Error message does not fire and receiving a System error instead
Created a Contact Trigger and a ContactTriggerHandler class to produce an error if the Contact is related to a relationship that contains 'Pricing Letters' and any item in the Address or Email Address is blank.创建了一个联系人触发器和一个 ContactTriggerHandler class 以在联系人与包含“定价信函”的关系相关并且地址或 Email 地址中的任何项目为空白时产生错误。 Code compiles but receiving the error below.
代码编译但收到以下错误。 Any help is appreciated.
任何帮助表示赞赏。
ERROR: Review the errors on this page.错误:查看此页面上的错误。 ContactTrigger: execution of BeforeUpdate caused by: System.FinalException: SObject row does not allow errors Class.ContactTriggerHandler.IsPricingLetter: line 9, column 1 Trigger.ContactTrigger: line 5, column 1
ContactTrigger:执行 BeforeUpdate 引起:System.FinalException:SObject 行不允许错误 Class.ContactTriggerHandler.IsPricingLetter:第 9 行,第 1 列 Trigger.ContactTrigger:第 5 行,第 1 列
TRIGGER扳机
trigger ContactTrigger on Contact (before update) {
if(trigger.isbefore && trigger.isupdate){
Contact checkcontact = [SELECT id,Email,MailingStreet,MailingCity,MailingState,MailingPostalCode,MailingCountry
FROM Contact WHERE id IN : Trigger.new];
ContactTriggerHandler.IsPricingLetter(checkcontact);
}//End If isBefore && isUpdate
}//End Class
TRIGGER HANDLER触发器处理程序
public class ContactTriggerHandler {
public static void IsPricingLetter(Contact con){
//Get list of relationship records that contain Pricing Letters role on the Contact ID being triggered.
for(AccountContactRelation c : [SELECT ContactId FROM accountcontactrelation WHERE roles INCLUDES ('Pricing Letters') AND Contactid = :con.Id]){
//If triggering Contact ID is present, check for missing mailing address components or missing email address.
if(con.MailingStreet==null){
con.addError('Mailing street on a Pricing Letter Contact cannot be null.');
}//End If Mailing Street
if(con.MailingCity==null){
con.addError('Mailing City on a Pricing Letter Contact cannot be null.');
}//End If Mailing City
if(con.MailingPostalCode==null){
con.addError('Mailing City on a Pricing Letter Contact cannot be null.');
}//End If Mailing Postal
if(con.MailingState==null){
con.addError('Mailing State on a Pricing Letter Contact cannot be null.');
}//End If Mailing State
if(con.MailingCountry==null){
con.addError('Mailing Country on a Pricing Letter Contact cannot be null.');
}//End If Mailing Country
if(con.Email==null){
con.addError('Email Address on a Pricing Letter Contact cannot be null.');
}//End If Mailing Country
}//End For Loop
}//End IsPricingLetter Method
}//End Class
Don't query.不要查询。 Last time I told you that if you query in a "before update" you'll get state of database before the edit, without values changed by the user.
上次我告诉过你,如果你在“更新前”查询,你将在编辑前得到 state 的数据库,用户没有更改值。
You can't just addError
on something you queried, potentially totally unrelated to the edit.您不能
addError
对您查询的内容添加错误,这可能与编辑完全无关。
ContactTriggerHandler.IsPricingLetter(trigger.new[0]);
it's bit dangerous (you check only 1st instead of all, problem if anybody does mass edit from a listview for example) but should be enough and "errorable".这有点危险(你只检查第一个而不是全部,例如,如果有人从列表视图中进行大量编辑,就会出现问题)但应该足够了并且“错误”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.