具有特定配置文件的 Salesforce 用户只能删除基于 Account.field__c 的联系人,并且 user.field__c 是相同的

问题描述 投票:0回答:1

具有特定配置文件的 Salesforce 用户只能删除基于 Account.field__c 的联系人,并且 user.field__c 是相同的。

你们对如何做到这一点有什么想法吗?如果联系人记录 Account.field__c 与用户 field__c 相同,我需要用户删除记录。我尝试过验证规则:

我还为用户的个人资料授予了对联系人对象的删除访问权限。

AND(
OR(
$Profile.Name = "HA Customer Care - External/Agents",
$Profile.Name = "HA Customer Care - Internal"
),
TEXT($User.ps_BookingOfficeID__c) != Account.ps_BookingOffice__r.Name
)
salesforce admin salesforce-lightning salesforce-service-cloud
1个回答
0
投票

验证规则不会在删除时触发,只会在插入/更新时触发。您需要一个

before delete
触发器。或者流动。

类似这样的东西(除了这只是为了回答目的,正确的“专业”解决方案将有一个单独的触发器处理程序类)

trigger ContactTrigger on Contact(before delete){
    if(Trigger.isBefore && Trigger.isDelete){
        validateDeletes(trigger.old);
    }
    
    public static validateDeletes(List<Contact> contacts){
        User u = [SELECT Profile.Name, ps_BookingOfficeID__c
            FROM User
            WHERE Id = :UserInfo.getUserId()];
        // These 2 profiles need extra checks when deleting.
        // Anybody else can delete whatever they need.
        Set<String> restricted = new Set<String>('HA Customer Care - External/Agents', 'HA Customer Care - Internal');
        if(!restricted.contains(u.Profile.Name){
            return;
        }

        Map<Id, Account> accs = new Map<Id, Account>([SELECT Id
            FROM Account
            WHERE Id IN 
                (
                    SELECT AccountId 
                    FROM Contact 
                    WHERE Id IN :contacts
                ) 
                AND ps_BookingOffice__r.Name != :u.ps_BookingOfficeID__c]);
        for(Contact c : contacts){
            if(c.AccountId == null){
                c.addError('You can\'t delete this private contact because we can\'t determine which office they would belong to');
            } else if(accs.containsKey(c.AccountId){
                c.addError('This contact does not belong to your office');
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.