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

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

问题描述:

当谈到集成时,我有点SFDC新手,但是有什么方法可以查询与给定用户共享的联系人,并考虑到共享可能发生的所有方式?基本上只是看到用户在平台中看到的相同联系人?SFDC - 查询与给定用户共享的所有联系人?

我认为这是你正在寻找。我添加了一些内嵌评论来解释每个步骤正在做什么。最终结果应该是组织中指定用户可以读取的所有联系人。

// 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);