前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >添加Contact表的共有情报(三)

添加Contact表的共有情报(三)

原创
作者头像
repick
发布2022-05-16 23:12:55
5160
发布2022-05-16 23:12:55
举报
文章被收录于专栏:SalesforceSalesforce

今天接着使用Trigger做成共有情报,首先Contact数据登录时,在BeforeInsert中变更OwnerId(所有者),因为需要登录Account信息,Account的OwnerId(所有者)也需要变更,因为所有者发生变更,如果不插入共有情报,当前User是不能继续访问该数据,所以在AfterInsert中插入变更前所有者对应的Group情报的共有数据。

1.Public Group

2.测试用的两个User

3.GroupMember

4.Trigger做成

ContactTrigger.trigger

代码语言:javascript
复制
trigger ContactTrigger on Contact (before insert, after insert) {
    if(Trigger.isinsert && Trigger.isBefore ){
        ContactHelper.BeforeInsert(Trigger.new);
    }
    if(Trigger.isinsert && Trigger.isAfter ){
        ContactHelper.AfterInsert(Trigger.new);
    }
}

ContactHelper.cls

代码语言:javascript
复制
public without sharing class ContactHelper {
    public static Map<String, Id> contactId2OwnerMap = new Map<String, Id>();
    /**
     * @name BeforeInsert
     * @description
     * @param List<Contact> newList
     * @return void
    **/
    public static void BeforeInsert(List<Contact> newList){

        User userItem = new User();
        List<User> userList =[SELECT Id,Name FROM User WHERE Name = 'Chavez Julie'];
        if(userList != null && userList.size() > 0) {
            userItem = userList.get(0);
        }

        String currentProfileId = UserInfo.getProfileId();
        Set<String> availableProfileNameSet = new Set<String>{'Standard User'};
        List<Profile> profileList = [SELECT Id,Name
                                    FROM Profile
                                    WHERE Id = :currentProfileId];
        Profile currentProfile = new Profile();

        if(profileList != null && profileList.size() > 0) {
            currentProfile = profileList.get(0);
        }

        if (availableProfileNameSet.contains(currentProfile.Name)) {
            List<Account> accList = new List<Account>();
            for (Contact con : newList) {
                if (con.AccountId == null) {
                    String accName = getContactName(con);
                    Account accItem = new Account();
                    accItem.Name = accName;
                    accList.add(accItem);
                }
            }

            Set<Id> accIds = new Set<Id>();
            Database.insert(accList, false);
            List<Account> accNewList =[SELECT Id,Name,OwnerId FROM Account WHERE Id IN :accIds];
            List<Account> accUpdateList = new List<Account>();
            Map<String, Id> accName2IdMaps = new Map<String, Id>();
            if (accNewList != null && accNewList.size() >0) {
                for (Account accItem : accNewList) {
                    accName2IdMaps.put(accItem.Name, accItem.Id);
                    Account accUpdate = accItem;
                    accUpdate.OwnerId = userItem.Id;
                    accUpdateList.add(accUpdate);
                }
            }
            Database.update(accUpdateList, false);
            for (Contact con : newList) {
                contactId2OwnerMap.put(getContactName(con), con.OwnerId);
                con.OwnerId = userItem.Id;
                if (con.AccountId == null && !accName2IdMaps.isEmpty()) {
                    String accName = getContactName(con);
                    con.AccountId = accName2IdMaps.get(accName);
                }
            }
        }
    }
    private static String getContactName(Contact con) {
        String accName = '';
        if (con.LastName != null && con.FirstName != null) {
            accName = con.LastName + ' ' + con.FirstName;
        } else {
            if (con.LastName != null) {
                accName = con.LastName;
            }
            if (con.FirstName != null) {
                accName = con.FirstName;
            }
        }
        return accName;
    }

    public static void AfterInsert(List<Contact> newContactList){

        if (contactId2OwnerMap != null && !contactId2OwnerMap.isEmpty()) {
            List<GroupMember> groupList = [SELECT Id, GroupId, UserOrGroupId, SystemModstamp FROM GroupMember];

            Map<Id, Id> userId2GroupMap = new Map<Id, Id>();
            if (groupList != null && groupList.size() > 0) {
                for (GroupMember groupMemberItem : groupList) {
                    userId2GroupMap.put(groupMemberItem.UserOrGroupId, groupMemberItem.GroupId);
                }
            }

            List<ContactShare> conShareList = new List<ContactShare>();
            for (Contact con : newContactList) {
                if (userId2GroupMap != null && !userId2GroupMap.isEmpty()) {
                    Id ownerId = contactId2OwnerMap.get(getContactName(con));
                    if (ownerId != null) {
                        ContactShare conShare = new ContactShare();
                        conShare.ContactId = con.Id;
                        conShare.UserOrGroupId = userId2GroupMap.get(ownerId);
                        conShare.RowCause = 'Manual';
                        conShare.ContactAccessLevel = 'Edit';
                        conShareList.add(conShare);
                    }
                }
            }
            Database.insert(conShareList, false);
        }
    }
}

5.测试

用户Laura登录,并做一条数据

OwnerId(所有者)变更成功

【Manual】的ContactShare做成

用户Laura可以继续对该条数据有编辑权限

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.Public Group
  • 2.测试用的两个User
  • 3.GroupMember
  • 4.Trigger做成
  • 5.测试
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档