0
public static void hireContactForm(List < HireForm__c > HireList) {
 List < Contact > contList = new List < Contact > ();
 List < Case > CaseList = new List < Case > ();
 //List<HireForm__c> hireInsertList = new List<HireForm__c>();

 for (HireForm__c hireForm: HireList) {
  if (hireForm.status__c == 'In Progress') {
   Contact con = new Contact();
   con.FirstName = hireForm.First_Name__c;
   con.LastName = hireForm.Last_Name__c;
   con.Phone = hireForm.Phone__c;
   con.Email = hireForm.Email__c;

   contList.add(con);
   //hireForm.Candidate__c = con.id;
  } else {
   hireForm.addError('Select "In Progress" first, then try again');
  }
 }
 insert contList;
 Integer i = 0;
 for (HireForm__c hr: hireList) {
  if (hr.status__c == 'In Progress') {
   hr.Candidate__c = contList[i].id;
  }
 }
 for (Contact cont: contList) {
  Case cases = new Case();
  cases.Status = 'New';
  cases.ContactId = cont.Id;
  caseList.add(cases);
 }
 insert caseList;
}

public static void hireContactFormForUpdate(List < HireForm__c > HireListNew) {
 set < id > contactIdSet = new set < id > ();
 for (HireForm__c hire: HireListNew) {
  contactIdSet.add(hire.Candidate__c);
 }
 List < Case > CaseList = [select id, ContactId from
  case where ContactId in: contactIdSet
 ];
 for (HireForm__c hire: HireListNew) {
  for (Case caseStatus: CaseList) {
   if (hire.Candidate__c == caseStatus.ContactId && hire.status__c == 'Completed') {
    caseStatus.Status = 'Closed';
   }
   update caseStatus;
  }
 }
}

public static void caseUpdate(List < Case > caseList) {
 Set < id > caseIds = new Set < id > ();
 Set < id > candIds = new Set < id > ();

 for (Case c: caseList) {
  caseIds.add(c.ContactId);
 }
 for (HireForm__c hireList: [Select candidate__c
   from HireForm__c
   where candidate__c in: caseIds
   and status__c = 'Completed'
  ]) {

  candIds.add(hireList.Candidate__c);
 }
 for (Case updateCaseList: caseList) {
  if (updateCaseList.ContactId != NULL) {
   if (!candIds.contains(updateCaseList.ContactId)) {
    if (updateCaseList.Status == 'Closed') {
     updateCaseList.addError('You can not close the case until hire form is completed');
    }
   }
  }
 }
}

//Trigger
trigger ContactFormTrigger on HireForm__c(before insert, before update) {
 if (Trigger.isBefore && Trigger.isInsert) {
  AccountContact.hireContactForm(Trigger.new);
 }
 if (Trigger.isBefore && Trigger.isUpdate) {
  AccountContact.hireContactFormForUpdate(Trigger.new);
 }
}

trigger HireCaseForm on Case(before update) {
 if (Trigger.isBefore && Trigger.isUpdate) {
  AccountContact.caseUpdate(Trigger.new);
 }
}
 Error:Apex trigger ContactFormTrigger caused an unexpected exception, contact your administrator: ContactFormTrigger: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 5002v00002aKMAXAA4; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, You can not close the case until hire form is completed: []: Class.AccountContact.hireContactFormForUpdate: line 246..

这是我在将字段更新为“已完成”时遇到的错误,所以它为什么要调用其他方法的触发器。我已经坚持了三天多。

4

1 回答 1

1

5002v00002aKMAXAA4 将是一个案例 ID(任何以 500 开头的都是案例)

您的错误消息包括失败的提示 ( Class.AccountContact.hireContactFormForUpdate)。

在那种方法中,你有

for (HireForm__c hire: HireListNew) {
  for (Case caseStatus: CaseList) {
   if (hire.Candidate__c == caseStatus.ContactId && hire.status__c == 'Completed') {
    caseStatus.Status = 'Closed';
   }
   update caseStatus;
  }
 }

你写了“当我将字段更新为'完成'时出现错误”。看起来 Salesforce 正在做它被告知的事情。雇用完成 -> 您的代码尝试关闭所有案例 -> 有人在 Case 上写了一个验证来阻止您。

是的,验证,它来自一个 val。规则,而不是来自触发器。您应该检查验证规则。如果您使用调试日志运行代码,您应该会看到验证规则,并且在它旁边是 SF 用来评估它的值。它看起来有点像这样,你想要粗体的部分:

在此处输入图像描述

我的猜测是验证会触发,因为此时hire.Status__c尚未“完成”。您可能选择了错误的触发关键字。这个方法是在“更新前”还是“更新后”运行的?

逻辑上应该是“之后”。“之前”用于字段预填充,各种过于复杂而无法使用验证规则的检查等。“之后”用于副作用(更新相关记录,向外部系统发送通知)。大规模关闭相关案件对我来说是一个副作用。

于 2019-11-21T22:11:33.923 回答