创建了一个ContactTrigger和一个ContactTriggerHandler类,以便在Contact与包含'Pricing Letters'的关系相关并且Address或Email Address中的任何项为空时产生错误。代码编译,但收到下面的错误。如有任何帮助,不胜感激。
错误:查看本页上的错误。ContactTrigger:执行由:System引起的BeforeUpdate。类. contacttriggerhandler . ispricingletter:第9行,第1列触发。ContactTrigger:第5行,第1列
触发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
触发器处理程序
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
请勿查询。上次我告诉过你们,如果你在"before update"您将在编辑前获得数据库的状态,而不需要用户更改值。
你不能只在你查询的东西上addError
,可能与编辑完全无关。
ContactTriggerHandler.IsPricingLetter(trigger.new[0]);
这有点危险(你只检查第一个而不是所有,如果有人从列表视图进行大量编辑,问题就来了),但应该足够了,并且"errorable"