function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Susanta ChakrabortySusanta Chakraborty 

Record type restriction through apex class & triggers

Hi Team
I have one object called Staff contract which linked with one another object called Staff. There is one apex class which restrcit all staff contracts from normal user except admin. Logic is..
if UserID!=OwnerID then no one can see other's staff contracts.
Now problem is, in staff contract objects, there are 2 record types, one is Staff and another one is Field staff. I want to open all field staff contracts for each and every profile.
My question is how to restrict staff contracts in respect of record type. I am attaching the entire class, it will be helpful if anyone can modify the class as per my criteria.

Here is the class...
// Comments:    [Controller for Insert Staff Contract Sharing records.]
Public Class ctrlAddStaffContractSharing{
        
    //List for Staff Contract sharing records.
    List<Staff_Contract__Share> lstStaffContShares = new List<Staff_Contract__Share>();
    
    //List of Staff Contract records.
    List<Staff_Contract__c> lstStaffCont = new List<Staff_Contract__c>();   
     
    //lstGroup will enclose user record which has name ED and HR Head.
    List<Group> lstGroup = [SELECT ID FROM GROUP WHERE DeveloperName = 'ED_and_HR_Head' LIMIT 1];
    
    Public map<Id,User> mapUser = new map<Id,User>();
    Public map<String,String> mapStaff = new map<String,String>();
      
    
    Public void AddStaffContSharing(SET<ID> setStaffContIds){
    
      //Fill Performance Evaluation list.
        lstStaffCont = new List<Staff_Contract__c>([SELECT ID,Name,OwnerId,Staff__c FROM Staff_Contract__c where id in :setStaffContIds]);

        //Delete existing sharing records of Performance Evaluations
        List<Staff_Contract__Share> lstStaffContShareRemove = [SELECT ID,ParentId,RowCause 
                                                                FROM Staff_Contract__Share 
                                                                WHERE RowCause = 'Manual' AND ParentId in :setStaffContIds];
                                                                
        System.debug(lstStaffContShareRemove);                                                        
                                                                
        If(lstStaffContShareRemove.size() > 0)
         Delete lstStaffContShareRemove;
        
        Set<Id> setGroupMemberUserId = New Set<Id>();
        Map<Id,GroupMember> MapGroupMembers;
        if(lstGroup.size()>0){            
            List<GroupMember> lstGroupMember = [Select UserOrGroupId, GroupId From GroupMember Where GroupId =: lstGroup[0].Id];
            if(lstGroupMember.size() > 0){
                for(GroupMember objGM: lstGroupMember){
                    setGroupMemberUserId.Add(objGM.UserOrGroupId);
                }
            }            
        }
        
        for(Staff_Contract__c objStaffCont:lstStaffCont){
            
            Set<Id> setShareTo = New Set<Id>();
           if(lstGroup.size()>0 && setGroupMemberUserId.size()> 0 && setGroupMemberUserId.contains(objStaffCont.OwnerId) == false)
            setShareTo.Add(lstGroup[0].ID);
             
             //Sharing with Staff Owner
            setShareTo.Add(mapStaff.get(objStaffCont.Staff__c));
            
            //Sharing with Staff Owners Manager
            if(mapStaff.containskey(objStaffCont.Staff__c))
             if(mapUser.containskey(mapStaff.get(objStaffCont.Staff__c)))
               if(mapUser.get(mapStaff.get(objStaffCont.Staff__c)).ManagerId != null)
                 setShareTo.Add(mapUser.get(mapStaff.get(objStaffCont.Staff__c)).ManagerId);
                         
                                 
          for(Id objSharetoid : setShareTo){
                //Create a new Performance_Evaluation__Share record to be inserted in to the lstPEShares.
             Staff_Contract__Share objStaffContshare = new Staff_Contract__Share(
                    ParentId = objStaffCont.Id,
                    UserOrGroupId = objSharetoid,
                    AccessLevel = 'Edit');
                lstStaffContShares.ADD(objStaffContshare);
            }
        }
    
        //Insert all of the newly created Share records and capture save result
        Database.SaveResult[] StaffContShareInsertResult = Database.insert(lstStaffContShares,false);
       
            
    }  
    
    Public ctrlAddStaffContractSharing(){
    
      for(User usr : [Select Id, Name, ManagerId,DelegatedApproverId from User])
       mapUser.put(usr.Id,usr);
       
      for(Staff__c staff : [Select Id, Name, OwnerId from Staff__c])
        mapStaff.put(staff.Id,staff.OwnerId);
             
    }   
    
   
}