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
aparna d 1aparna d 1 

Apex sharing access based on owner changed

I have a requirement case is assigned to queue .If queue member is changed owner to any one of queue member then giving acces to public group memebrs. Am getting issues please help on this 
Public static void ShareReq(List<Case> listCase,Map<Id,Case> caseOldMap){
        Set<id> qmemberId = new Set<Id>();
        Set<id> gpmemId = new Set<Id>();
        Map<Id,Id> mapid = new Map<Id,Id>();
        list<CaseShare> casesharelist= new list<CaseShare>();
              Id devRecordTypeId = Schema.SObjectType.case.getRecordTypeInfosByName().get('Proposal case').getRecordTypeId();
        Id queuegp = [select Id, type from Group where type='Queue' AND Name=' Request Queue'].id;
          
        List<GroupMember> queuemem = [SELECT GroupId,Id,UserOrGroupId FROM GroupMember WHERE GroupId =:queuegp]; 
        for(GroupMember qmem : queuemem){
            
            qmemberId.add(qmem.UserOrGroupId);
        }
        Id gpid = [select Id, type from Group WHERE Name = 'test group'].id;
        List<GroupMember> gplst = [SELECT GroupId,Id,UserOrGroupId FROM GroupMember WHERE GroupId =:gpid];
       
        for(case newcase:listCase){
            Case oldcase= caseOldMap.get(newcase.id);
            for(GroupMember gpmem : gplst){
                if(newcase.RecordTypeId == devRecordTypeId && qmemberId.contains(newcase.OwnerId) && newcase.OwnerId != oldcase.OwnerId) {
                    CaseShare caseSharingRules= new CaseShare();
                    caseSharingRules.UserOrGroupId = gpmem.UserOrGrou
                   caseSharingRules.CaseAccessLevel = 'Edit';        
                   casesharelist.add(caseSharingRules);
                }
           }
        }
        
        if(casesharelist.size()>0)
            insert casesharelist;
    }
        
calling this method in trigger 
public override void bulkAfter(){
if(trigger.isUpdate){
CSCaseHandler.ShareReq(trigger.new,(Map<Id,Case>)Trigger.oldMap);
}
}
Deepali KulshresthaDeepali Kulshrestha
Hi Aparna,

I've gone through your requirement and you can see below code for creating sharing setting for particular user:

Note(Replace Account sobject from Case sobject)


public class ShareAccountWithHRRoleClass 
{
public static void ShareAccountWithHRRole()
{
    try
    {
        
        List<Initiative__c> allInitiative=new List<Initiative__c>([select id,Name,Is_HR__c from Initiative__c where Is_HR__c=true]);
        
        
        System.debug('allInitiative---->'+allInitiative);
        
        if(allInitiative.size()>0)
        {
            UserRole role=[SELECT Id,Name FROM UserRole where Name='HR' LIMIT 1];//getting the Role of HR
            List<User> HRRoleUserList=new List<User>([select id,UserRoleId,Name from User where UserRoleId=:role.Id ]);
            
            List<Initiative__Share> allInitiativeForSharingList=new List<Initiative__Share>();//create all accounts that are sharable with HR ROle
            
            for(Initiative__c acc:allInitiative)
            {
                for(User us:HRRoleUserList)
                { 
                    Initiative__Share initiativeForShare=new Initiative__Share();
                    initiativeForShare.ParentId=acc.Id;
                    initiativeForShare.UserOrGroupId=us.Id;
                    initiativeForShare.AccessLevel='Edit';//you can give access as per your requirement
                  
                    
                    allInitiativeForSharingList.add(initiativeForShare);
                }
            }
            
            if(allInitiativeForSharingList.size()>0)
                insert allInitiativeForSharingList;
            
            system.debug('allInitiativeForSharingList--->'+allInitiativeForSharingList);
            
            
            
        }
        
    }
    catch(Exception e)
    {
        System.debug('Error in--->'+e.getLineNumber()+' and Error is--->'+e.getMessage()+' --->(inside:ShareAccountWithHRRoleClass)');
    }
    
}

}



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com