+ Start a Discussion
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com 

Apex Sharing On Case Object- Please Help!!

Hi Everyone,
    I have a profile called "Business Development". Users (who are also owners of the case) of this profile have only Read access on the 2 existing record types (lets call them A and B) of Case Object. I want users (who are also owners of the case) of this profile to have Edit access on a new case record type (lets call it C)  . So Here is the situation 

Case OWD: Private
Case Acess : A and B Record Type: Read Only- Already Exisitng Record Types
                     C record Type : Read Write : New Record Type.

Now to grant Access only to C record Type I wrote an apex class., it is given below:

    public static void determineFDMCaseOwnersAccess(List<Case> newFDMCasesBD) {
        List<Id> OwnerIdList=new List<Id>();
        Map<Id,Id> UserMap=new Map<Id,Id>();
        List<CaseShare> CaseShareRecordList  = new List<CaseShare>();
        CaseShare CaseShareRecord=new CaseShare();
        for(Case css:newFDMCasesBD){
            OwnerIdList.add(css.OwnerId);
        }
        List <User> UserList=[Select Id,ProfileId from User where Id IN:OwnerIdList AND ProfileId=:'00eG0000000FnxC'];
        for(User uss:UserList){
            UserMap.put(uss.Id,uss.ProfileId);
        }
        for(Case css:newFDMCasesBD){
            if(UserMap.get(css.OwnerId)!=null && css.RecordTypeId==Label.Case_Record_Type_FDM){
              CaseShareRecord=new CaseShare();
              CaseShareRecord.CaseId=css.Id;
              CaseShareRecord.UserOrGroupId=css.OwnerId;
              CaseShareRecord.CaseAccessLevel='Edit';
              CaseShareRecord.RowCause ='Owner';
              CaseShareRecordList.add(CaseShareRecord);
            }
        }
        if(CaseShareRecordList.size()>0){
            Database.SaveResult[] lsr = Database.insert(CaseShareRecordList,false);
        }
    
    } 

This code works well and inserts CaseShare records , but on querying them it turns out that their access level is All, instead of Edit as given in the code. Also Users are unable to edit the case even post Case Share record creation. 

Is it because the sharing is being done by the case owner itself? Why cant user edit the records if he/she has CaseShare with All as access level?


Please help me with this!!!


Regards
Shrey Tyagi