Triggers 使用salesforce中的触发器阻止删除最后一个联系人
我是salesforce的新手,我有一个要求,我需要如何达到这个要求的建议 我有4个与一个帐户相关的联系人,当有人删除联系人时,他应该无法删除与该帐户相关的最后一个联系人。例如:在帐户A1中,我有4个联系人,有人从该帐户中删除了3个联系人,然后应将其删除,在此之后,将只有1个联系人与该帐户相关,并且有人试图删除最后一个联系人,但不应删除该联系人Triggers 使用salesforce中的触发器阻止删除最后一个联系人,triggers,salesforce,Triggers,Salesforce,我是salesforce的新手,我有一个要求,我需要如何达到这个要求的建议 我有4个与一个帐户相关的联系人,当有人删除联系人时,他应该无法删除与该帐户相关的最后一个联系人。例如:在帐户A1中,我有4个联系人,有人从该帐户中删除了3个联系人,然后应将其删除,在此之后,将只有1个联系人与该帐户相关,并且有人试图删除最后一个联系人,但不应删除该联系人 如何使用触发器实现此目的?您可以在没有触发器的情况下解决此问题。您可以在帐户上创建一个汇总摘要字段,对联系人记录(ContactCount___c)进行
如何使用触发器实现此目的?您可以在没有触发器的情况下解决此问题。您可以在帐户上创建一个汇总摘要字段,对联系人记录(ContactCount___c)进行计数,并在帐户验证规则中对该计数进行评估,如下所示:
ContactCount__c = 0 && PRIORVALUE(ContactCount__c) > 0
在触发器中,对与帐户相关的所有联系人运行查询。如果您试图删除此触发器中的所有触发器,则不允许删除。我不知道你想如何处理人们同时删除多个联系人的问题,但假设你只是不允许整个删除,用户必须用更少的联系人重试。如果你想想出一些逻辑来删除除1个联系人以外的所有联系人,这取决于你。 比如:
Trigger OnContactDelete on Contact (before delete) {
Set<ID> accountIds = new Set<ID>(); //all accounts that contacts are being deleted from
for (Contact contact : Trigger.old) {
accountIds.add(contact.AccountId);
}
List<Contact> contacts = [SELECT Id, AccountId FROM Contact WHERE AccountId IN :accountIds]; //get all of the contacts for all of the affected accounts
Map<ID, Set<ID>> deleteMap = new Map<ID, Set<ID>>(); //map an account ID to a set of contact IDs being deleted
Map<ID, Set<ID>> foundMap = new Map<ID, Set<ID>>(); //map an account ID to a set of contact IDs that were found by the query
for (Contact deleteContact : Trigger.old) {
Set<ID> idSet = deleteMap.get(deleteContact.AccountId);
if (idSet == null) {
idSet = new Set<ID>();
}
idSet.add(deleteContact.Id);
deleteMap.put(deleteContact.AccountId, idSet);
}
for (Contact foundContact : contacts) {
Set<ID> idSet = foundMap.get(foundContact.AccountId);
if (idSet == null) {
idSet = new Set<ID>();
}
idSet.add(foundContact.Id);
foundMap.put(foundContact.AccountId, idSet);
}
for (ID accountId : accountIds) { //go through each affected account
Set<ID> deleteIds = deleteMap.get(accountId);
Set<ID> foundIds = foundMap.get(accountId);
if (deleteIds != null && foundIds != null && deleteIds.containsAll(foundIds)) {
for (Contact contact : Trigger.old) {
if (deleteIds.contains(contact.Id)) { //this is one of the contacts being deleted
contact.addError('This contact is potentially the last contact for its account and cannot be deleted');
}
}
}
}
}
在联系人上触发OnContactDelete(删除前){
Set accountIds=new Set();//正在从中删除联系人的所有帐户
for(联系人:Trigger.old){
AccountId.add(contact.AccountId);
}
List contacts=[选择Id,AccountId FROM Contact WHERE AccountId IN:AccountId];//获取所有受影响帐户的所有联系人
Map deleteMap=new Map();//将帐户ID映射到正在删除的一组联系人ID
Map foundMap=new Map();//将帐户ID映射到查询找到的一组联系人ID
对于(联系人删除联系人:Trigger.old){
设置idSet=deleteMap.get(deleteContact.AccountId);
if(idSet==null){
idSet=新集合();
}
idSet.add(deleteContact.Id);
deleteMap.put(deleteContact.AccountId,idSet);
}
对于(联系人foundContact:contacts){
设置idSet=foundMap.get(foundContact.AccountId);
if(idSet==null){
idSet=新集合();
}
idSet.add(foundContact.Id);
foundMap.put(foundContact.AccountId,idSet);
}
对于(ID accountId:accountId){//检查每个受影响的帐户
设置deleteId=deleteMap.get(accountId);
Set foundid=foundMap.get(accountId);
if(deleteIds!=null&&foundIds!=null&&deleteIds.containsAll(foundIds)){
for(联系人:Trigger.old){
如果(deleteIds.contains(contact.Id)){//这是正在删除的联系人之一
contact.addError(“此联系人可能是其帐户的最后一个联系人,无法删除”);
}
}
}
}
}
注意,我只是输入了这段代码,实际上根本没有测试代码,即使是缺少分号或大括号之类的语法错误。从理论上讲,这应该是可行的,但可能有更好的方法