public without sharing class SubAuthorizedHandler extends Oly_TriggerHandler { private Map newMap; private Map oldMap; private List newList; private List oldList; public SubAuthorizedHandler() { this.newMap = (Map) Trigger.newMap; this.oldMap = (Map) Trigger.oldMap; this.newList = (List) Trigger.new; this.oldList = (List) Trigger.old; } protected override void beforeInsert() { CheckValue(); } protected override void beforeUpdate() { CheckValue(); } // 检查被授权人是否是角色层级的上司 private void CheckValue() { for (SubAuthorized__c tempSub : newList) { //Set parentIDset = getRoleSuperiorUsers(tempSub.ownerid); if (!tempSub.IsAuthorize__c && getsubUsers(tempSub.Authorizer__c).size() > 0) { tempSub.addError('您不能再次转授权'); } //boolean isError = true; //for (ID tempID : parentIDset) { // if (tempSub.Authorizee__c == null || tempID == tempSub.Authorizee__c) { // isError = false; // break; // } //} //if (isError) { // tempSub.Authorizee__c.addError('只能选择您的领导!'); //} } } public static Set getsubUsers(Id userId) { Map allSubUserMap = new Map ( [select id from user where ZongjianApprovalManager_copy__c = : userId or TongkuoZongjian_copy__c = : userId or BuchangApprovalManager_copy__c = : userId or BuchangApprovalManagerSales_copy__c = : userId or JingliApprovalManager_copy__c = : userId or SalesManager_copy__c = : userId //SWAG-BEM9EP start or ManagerId_copy__c = : userId or JingliEquipmentManager_copy__c = : userId or Buzhang_Equipment_Manager_copy__c = : userId //SWAG-BEM9EP end ]); return allSubUserMap.keySet(); } //public static Set getRoleSuperiorUsers(Id userId) { // // get requested user's role // Id roleId = [select UserRoleId from User where Id = :userId].UserRoleId; // // get all of the roles above the user // Set allSubRoleIds = getAllUpRoleIds(new Set {roleId}); // // get all of the ids for the users in those roles // Map users = new Map([Select Id, Name From User // where UserRoleId IN :allSubRoleIds]); // // return the ids as a set so you can do what you want with them // return users.keySet(); //} //private static Set getAllUpRoleIds(Set roleIds) { // Set currentRoleIds = new Set(); // // get all of the roles above the passed roles // for (UserRole userRole : [select Id, ParentRoleId from UserRole where id // IN :roleIds AND ParentRoleID != null]) // currentRoleIds.add(userRole.ParentRoleId); // // go fetch some more rolls! // if (currentRoleIds.size() > 0) { // currentRoleIds.addAll(getAllUpRoleIds(currentRoleIds)); // } // return currentRoleIds; //} }