zoukankan      html  css  js  c++  java
  • Salesforce用Apex判断Role Hierarchy的简单代码示例

    由于role不同于Profile,带有阶层性质,所以有一些自定义功能要依赖于这种阶层的设定。这样就涉及到role hierarchy的判断问题。

    我是一个绝懒之人,所以去网上搜了一下,能找到的方案都或多或少有些缺陷 。

    我所提供的方案也是如此,但是想比于浪费太多SOQL查询次数来讲,role的数量不超过50000条已经是足够好了。
    // 这里Update一下,其实根本不会有那么多的Role,因为默认500,向Salesforce技术支持提票才能达到10000。

    Talk is cheap, show you the code.
    PS:最近正在建设个人代码库,本身也只是一个简单的示例,之后会放出完全体版本。
    也许也会放到Github上。

     1 // @Version 0.1 Author Keal. Email: brain_qiu@hotmail.com
     2 // @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
     3 public class RoleHierarchyHelper {
     4     Boolean isEnd = false;
     5     Boolean compareResult = false;
     6     public static List<UserRole> roleList = [SELECT Id, Name, DeveloperName, ParentRoleId FROM UserRole LIMIT 50000];
     7     
     8     public RoleHierarchyHelper() {
     9         // pretend Salesforce spasms LOL. It happened.
    10         if(roleList == null || roleList.size() == 0) {
    11             roleList = [SELECT Id, Name, DeveloperName, ParentRoleId FROM UserRole LIMIT 50000];            
    12         } else {
    13             system.debug('=======RoleList.size()========' + roleList.size());
    14         }
    15     }
    16     
    17     public Boolean isSubordinate(String currentRoleId, String compareRoleId){
    18                 
    19         List<UserRole> childRoleList = new List<UserRole>();
    20         Map<Id, UserRole> childRoleMap = new Map<Id, UserRole>();
    21         Set<Id> exeUserRoleIdSet = new Set<Id>();
    22         exeUserRoleIdSet.add(currentRoleId);
    23         
    24         isEnd = false;
    25         compareResult = false;
    26         while(!isEnd) {
    27             childRoleMap = new Map<Id, UserRole>();
    28             for(UserRole ur: roleList) {
    29                 if(ur.ParentRoleId != null && exeUserRoleIdSet.contains(ur.ParentRoleId)) {
    30                     childRoleMap.put(ur.Id, ur);
    31                 }
    32             }
    33             
    34             if(childRoleMap.size() > 0) {
    35                 if(childRoleMap.containsKey(compareRoleId)) {
    36                     isEnd = true;
    37                     compareResult = true;    
    38                 } else {
    39                     isEnd = false;
    40                     exeUserRoleIdSet = new Set<Id>();
    41                     exeUserRoleIdSet.addAll(childRoleMap.keyset());            
    42                 }
    43                 
    44             } else {
    45                 isEnd = true;
    46                 compareResult = false;
    47             }
    48         }
    49         return compareResult;
    50     }
    51     
    52     public Boolean isSuperior(String currentRoleId, String compareRoleId) {
    53         Map<Id, UserRole> idUserRoleMap = new Map<Id, UserRole>(roleList);
    54         Id exeUserRoleId = currentRoleId;
    55         UserRole exeUserRole = new UserRole();
    56         
    57         isEnd = false;
    58         compareResult = false;
    59         while(!isEnd) {
    60             exeUserRole = idUserRoleMap.get(exeUserRoleId);
    61             if(exeUserRole.parentRoleId == null) {
    62                 isEnd = true;
    63                 compareResult = false;
    64             } else {
    65                 if(exeUserRole.parentRoleId == compareRoleId) {
    66                     isEnd = true;
    67                     compareResult = true;
    68                 } else {
    69                     isEnd = false;
    70                     exeUserRoleId = exeUserRole.parentRoleId;
    71                 }
    72             }
    73         }
    74         return compareResult;
    75     }
    76     // TODO isSameLine()
    77 }
  • 相关阅读:
    USACO 3.1
    linux 逻辑卷管理 调整分区大小
    记录一下
    ADOX创建ACCESS数据库列名的数据类型
    使用jstack分析cpu消耗过高的问题
    fastadmin添加定时任务
    linux定时任务
    技术域
    IOS div上下滑动效果
    mysql根据时间统计数据语句
  • 原文地址:https://www.cnblogs.com/sfdc/p/6512505.html
Copyright © 2011-2022 走看看