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

时间:2016-05-12 16:21:04

标签: api salesforce

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

1 个答案:

答案 0 :(得分:0)

我认为这就是你要找的东西。我添加了一些内联注释来解释每个步骤的作用。最终结果应该是组织中指定用户可以读取的所有联系人。

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