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