• Antoine
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

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;

         }

       }

   }

   

 

Hello All,
I'm trying to install B2b commerce in developer org. Anyone had a success with this.