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
Abhishek Tiwari 25Abhishek Tiwari 25 

manual sharing of Case Record

Insertion is done successfully but the record is not shared to the corresponding user.Please help 
 
 
//This class is used for manual sharing of Case Record
public class ManualSharingRecord {
   
    //This method takes two parameter first the record ID which you want to share
    //and second one to whom you want to share the record(Either user or group)
    public static Boolean successfullCaseSharing(ID recordId,ID userORGroup)
    {
        //create the object of caseshare object type
        CaseShare csShare=new CaseShare();
       
        //set the record level of sharing
        csShare.CaseAccessLevel='edit';
        //put the recordId which you want to share
        csShare.CaseId=recordId;
        system.debug('Record Id'+ csShare.CaseId);
        //put the userORGroup id to whom you want to share the record
        csShare.UserOrGroupId=userORGroup;
                system.debug('userId'+csShare.UserOrGroupId);
       csShare.RowCause=schema.caseShare.RowCause.manual;
       
        Database.SaveResult save=Database.insert(csShare,false);
        {
            if(save.issuccess())
            {
                return true;
            }
            else
            {
                Database.Error err=save.getErrors()[0];
                system.debug('Error'+save.getErrors()[0].getMessage());
                if(err.getStatusCode()==statuscode.FIELD_FILTER_VALIDATION_EXCEPTION &&err.getMessage().contains('Accesslevel'))
                {
                    return true;
                }
                else
                {
                    return false;
                }
                   
            }
        }
       
        
      
    }
 
}
 
 
 
case cs=new case(status='new',origin='phone');
    insert cs;
ID recordId=cs.id;
User us=[select id from user where name like '%abc%'];
ID userORGroup=us.id;
boolean rs=ManualSharingRecord.successfullCaseSharing(recordId, userORGroup);
system.debug('result'+rs);
 
Best Answer chosen by Abhishek Tiwari 25
Steven NsubugaSteven Nsubuga
how did you check that the record was not shared by the user? I just ran your code and it works perfectly. Maybe your user does not have system permissions to view cases. The problem is not your code but the configuration, maybe of the user's profile.

All Answers

Steven NsubugaSteven Nsubuga
how did you check that the record was not shared by the user? I just ran your code and it works perfectly. Maybe your user does not have system permissions to view cases. The problem is not your code but the configuration, maybe of the user's profile.
This was selected as the best answer
Abhishek Tiwari 25Abhishek Tiwari 25
Yes you are correct