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
Ashutosh Tripathi 9Ashutosh Tripathi 9 

Apex Managed Sharing ?

Hi,
I am trying to implements the apex managed sharing on the custom object Job__c, I am using the standard code provided. But I am getting the 
Error: Compile Error: Entity is not org-accessible at line 1 column 1

trigger JobApexSharing on Job__c (after insert) {
    
    if(trigger.isInsert){
        // Create a new list of sharing objects for Job
        List<Job__Share> jobShrs  = new List<Job__Share>();
        
        // Declare variable hiring manager sharing
       
        Job__Share hmShr;
        
        for(Job__c job : trigger.new){
            // Instantiate the sharing object
           
            hmShr = new Job__Share();
            
            // Set the ID of record being shared
           
            hmShr.ParentId = job.Id;
            
            // Set the ID of user or group being granted access
          
            hmShr.UserOrGroupId = job.Hiring_Manager__c;
            
            // Set the access level
           
            hmShr.AccessLevel = 'read';
            
            // Set the Apex sharing reason for hiring manager
           
            hmShr.RowCause = Schema.Job__Share.RowCause.Hiring_Manager__c;
            
            // Add objects to list for insert
        
            jobShrs.add(hmShr);
        }
        
        // Insert sharing records and capture save result 
        // The false parameter allows for partial processing if multiple records are passed 
        // into the operation 
        Database.SaveResult[] lsr = Database.insert(jobShrs,false);
        
        // Create counter
        Integer i=0;
        
        // Process the save results
        for(Database.SaveResult sr : lsr){
            if(!sr.isSuccess()){
                // Get the first save result error
                Database.Error err = sr.getErrors()[0];
                
                // Check if the error is related to a trivial access level
                // Access levels equal or more permissive than the object's default 
                // access level are not allowed. 
                // These sharing records are not required and thus an insert exception is 
                // acceptable. 
                if(!(err.getStatusCode() == StatusCode.FIELD_FILTER_VALIDATION_EXCEPTION  
                                               &&  err.getMessage().contains('AccessLevel'))){
                    // Throw an error when the error is not related to trivial access level.
                    trigger.newMap.get(jobShrs[i].ParentId).
                      addError(
                       'Unable to grant sharing access due to following exception: '
                       + err.getMessage());
                }
            }
            i++;
        }   
    }
    
}


Please advise.
 
Neetu_BansalNeetu_Bansal
Hi Ashutosh,

While iterating over Jobs, you have taken the variable name as Job and your object name is Job__c, so it must be conflicting. It is advised to use a different name to prevent conflictings like the below:
trigger JobApexSharing on Job__c( after insert )
{
	if( trigger.isInsert )
	{
        // Create a new list of sharing objects for Job
        List<Job__Share> jobShrs  = new List<Job__Share>();
        
        // Declare variable hiring manager sharing
        Job__Share hmShr;
        
        for( Job__c jobObj : trigger.new )
		{
            // Instantiate the sharing object
            hmShr = new Job__Share();
            
            // Set the ID of record being shared
            hmShr.ParentId = jobObj.Id;
            
            // Set the ID of user or group being granted access
            hmShr.UserOrGroupId = jobObj.Hiring_Manager__c;
            
            // Set the access level
            hmShr.AccessLevel = 'read';
            
            // Set the Apex sharing reason for hiring manager
            hmShr.RowCause = Schema.Job__Share.RowCause.Hiring_Manager__c;
            
            // Add objects to list for insert
            jobShrs.add(hmShr);
        }
        
        // Insert sharing records and capture save result 
        // The false parameter allows for partial processing if multiple records are passed 
        // into the operation 
        Database.SaveResult[] lsr = Database.insert( jobShrs, false );
        
        // Create counter
        Integer i=0;
        
        // Process the save results
        for(Database.SaveResult sr : lsr)
		{
            if(!sr.isSuccess())
			{
                // Get the first save result error
                Database.Error err = sr.getErrors()[0];
                
                // Check if the error is related to a trivial access level
                // Access levels equal or more permissive than the object's default 
                // access level are not allowed. 
                // These sharing records are not required and thus an insert exception is 
                // acceptable. 
                if(!(err.getStatusCode() == StatusCode.FIELD_FILTER_VALIDATION_EXCEPTION  
                                               &&  err.getMessage().contains('AccessLevel')))
				{
                    // Throw an error when the error is not related to trivial access level.
                    trigger.newMap.get(jobShrs[i].ParentId).
                      addError(
                       'Unable to grant sharing access due to following exception: '
                       + err.getMessage());
                }
            }
            i++;
        }   
    }
    
}
Let me know, if you need any other help.

Thanks,
Neetu
Ashutosh Tripathi 9Ashutosh Tripathi 9
Hi Neetu,

Thanks for the reply. I tried with by changing the object but still facing the same issue.