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
AntoineAntoine 

Problem writing Apex Class

Hi,

I'm having a hard time trying to write an Apex Class (I'm a complete newbie), so hopefully somebody here will have some advice.

 

I wanna share records automatically using Apex.

I have a custom object called "Weekly Update", within this object I have a lookup field towards a User.

Ideally, this code will automatically share the record with the user that's been selected with the lookup.

 

When I tried this code I got an error that says : 

Error: Compile Error: Illegal variable declaration: Sales_Rep__r.Id at line 3 column 52

Below is the code. Does anybody have an idea how to make this work?

Thanks a lot,

 

Antoine

 

--------

 

 

public class WeeklyUpdateSharing {

   

   static boolean manualShareRead(Id recordId, Id  Sales_Rep__r.Id ){

      // Create new sharing object for the custom object Job. 

    

      WU__Share WuShr  = new WU__Share();

   

      // Set the ID of record being shared. 

    

      WuShr.ParentId = recordId;

        

      // Set the ID of user or group being granted access. 

    

      WuShr.UserOrGroupId = Sales_Rep__r.Id;

        

      // Set the access level. 

    

      WuShr.AccessLevel = 'Read';

        

      // Set rowCause to 'manual' for manual sharing. 

    

      // This line can be omitted as 'manual' is the default value for sharing objects. 

    

      WuShr.RowCause = Schema.WU__Share.RowCause.Manual;

        

      // Insert the sharing record and capture the save result.  

    

      // The false parameter allows for partial processing if multiple records passed  

    

      // into the operation. 

    

      Database.SaveResult sr = Database.insert(WuShr,false);

 

      // Process the save results. 

    

      if(sr.isSuccess()){

         // Indicates success 

    

         return true;

      }

      else {

         // Get first save result error. 

    

         Database.Error err = sr.getErrors()[0];

         

         // Check if the error is related to trival 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')){

            // Indicates success. 

    

            return true;

         }

         else{

            // Indicates failure. 

    

            return false;

         }

       }

   }

   

 

Starz26Starz26

I assume you will be running this via schduled apex? That is probably another discussion....

 

try:

 

public class WeeklyUpdateSharing {

   

   static boolean manualShareRead(Id recordId, Id  Sales_Rep_Id ){

      // Create new sharing object for the custom object Job. 

    

      WU__Share WuShr  = new WU__Share();

   

      // Set the ID of record being shared. 

    

      WuShr.ParentId = recordId;

        

      // Set the ID of user or group being granted access. 

    

      WuShr.UserOrGroupId = Sales_Rep_Id;

        

      // Set the access level. 

    

      WuShr.AccessLevel = 'Read';

        

      // Set rowCause to 'manual' for manual sharing. 

    

      // This line can be omitted as 'manual' is the default value for sharing objects. 

    

      //WuShr.RowCause = Schema.WU__Share.RowCause.Manual;

        

      // Insert the sharing record and capture the save result.  

    

      // The false parameter allows for partial processing if multiple records passed  

    

      // into the operation. 

    

      Database.SaveResult sr = Database.insert(WuShr,false);

 

      // Process the save results. 

    

      if(sr.isSuccess()){

         // Indicates success 

    

         return true;

      }

      else {

         // Get first save result error. 

    

         Database.Error err = sr.getErrors()[0];

         

         // Check if the error is related to trival 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')){

            // Indicates success. 

    

            return true;

         }

         else{

            // Indicates failure. 

    

            return false;

         }

       }

   }

 

I believe the only thing wrong was the variable name for the sales rep ID. You should be passing sales_rep__r.id into the variable I renamed sales_rep_id