简体   繁体   English

SFDC - 查询与给定用户共享的所有联系人?

[英]SFDC - Query all contacts shared with a given user?

I'm somewhat of a SFDC novice when it comes to integration, but is there any way I can query just the contacts shared with a given user, taking into account all the ways the share can occur? 在集成方面,我有点像SFDC新手,但有什么方法可以查询与给定用户共享的联系人,考虑到共享可以发生的所有方式? Essentially just see the same contacts the user would see in within the platform? 基本上只是看到用户在平台内看到的相同联系人?

I think this is what you are looking for. 我想这就是你要找的东西。 I added some inline comments to explain what each step is doing. 我添加了一些内联注释来解释每个步骤的作用。 The end result should be all the contacts that can be read by a specified user in your org. 最终结果应该是组织中指定用户可以读取的所有联系人。

// add a set with all the contact ids in your org
List<contact> contacts = new List<contact>([Select id from Contact]);
Set<ID> contactids = new Set<ID>();
for(Contact c : contacts)
    contactids.add(c.id);

// using the user record access you can query all the recordsids and the level of access for a specified user
List<UserRecordAccess> ura = new List<UserRecordAccess>([SELECT RecordId, HasReadAccess, HasTransferAccess, MaxAccessLevel
     FROM UserRecordAccess
     WHERE UserId = 'theuserid'
     AND RecordId in: contactids
      ] );   

// unfortunatelly you cannot agregate your query on hasReadAccess=true so you'd need to add this step
Set<id> readaccessID = new Set<ID>();
for(UserRecordAccess ur : ura)
{
    if(ur.HasReadAccess==true)
    {
        readaccessID.add(ur.RecordID);
    }
}

// This is the list of all the Contacts that can be read by the specified user
List<Contact> readAccessContact = new List<Contact>([Select id, name from contact where id in: readaccessID]);

// show the results
system.debug( readAccessContact);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM