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
SS KarthickSS Karthick 

Sharing Record via Apex

Hi folks,
My  custom object (Test) which contains (name ,phone number and email fields)and the records inside the test object only visible to the my team leader and his manager..other user,leader and manager cannot view those record in test objects. for that I've created Apex trigger:

trigger Test_Share on Test__c (after insert) {

Id i=UserInfo.getUserId();

//ID groupId = [select id from Group where Type = 'Organization'].id;

if(trigger.isInsert){

List<Test__Share> testShare= new List<Test__Share>();
     for(Test__c t : trigger.new){

        Test__Share ts = new Test__Share();


        ts.ParentId = t.Id;
        ts.UserOrGroupId = ParrentIdClass.getParrentId(i);



        ts.AccessLevel = 'edit';

        ts.RowCause = Schema.Test__Share.RowCause.Test_Access__c;
        testShare.add(ts);
    }
Database.SaveResult[] TestShareInsertResult = Database.insert(testShare,false);
}
}

Apex Class: public class ParrentIdClass {

public static Id getParrentId(Id i){
    String ParentUserName;
    Id ParentUserId;
    User u = new User(id=i);
    u = [select u.UserRoleId from User u where u.Id =: UserInfo.getUserId()];

    System.debug('UserRoleId='+u.UserRoleId);


    UserRole ur = new UserRole(id=u.UserRoleId);
  ur = [select ur.Id, ur.Name, ur.ParentRoleId from UserRole ur where ur.Id =: u.UserRoleId];


    System.debug('UrId='+ur.Id);
System.debug('UrName='+ur.Name);
System.debug('UrParentRoleId='+ur.ParentRoleId);


    if (ur.ParentRoleId != null){
        //  check if there's a ParentRole
      UserRole ur1 = new UserRole(id=ur.ParentRoleId);
      ur1 = [select ur1.Id, ur1.Name, ur1.ParentRoleId from UserRole ur1 where ur1.Id =: ur.ParentRoleId];
    System.debug('UrId='+ur1.Id);
  System.debug('UrName='+ur1.Name);
  System.debug('UrParentRoleId='+ur1.ParentRoleId);
      if(ur1.ParentRoleId != null){
          //  last level in the hierarchy
          UserRole ur2 = new UserRole(id=ur1.ParentRoleId);
          ur2 = [select ur2.Id, ur2.Name, ur2.ParentRoleId from UserRole ur2 where ur2.Id =: ur1.ParentRoleId];
          System.debug('UrId='+ur2.Id);
    System.debug('UrName='+ur2.Name);
    System.debug('UrParentRoleId='+ur2.ParentRoleId);
            ParentUserId=ur2.Id;
            return ParentUserId;
      }
         else{
          // no more ParentRoleId
          ParentUserId=ur1.Id;
           return ParentUserId;
        }
     }
    else{
      // no ParentRoleId
      ParentUserId=ur.Id;
        return ParentUserId;
  }
}
}

But it doesnt work for my scenario..

Please Help!
Best Answer chosen by SS Karthick
Ranjeet Singh (SFDC Developer)Ranjeet Singh (SFDC Developer)
Hi Karthick,

Can i know what error you are receiving?

I will suggest you:
1. Check the OWD, OWD for Test__C object must be either Private Or Public Read Only

Code looks without error, Please share the error message.

Thanks & Regards,
Ranjeet Singh.

All Answers

Ramu_SFDCRamu_SFDC
Review the below article that has few examples on apex sharing. Hope it will give you some clue as to where you might have gone wrong

http://www.shivasoft.in/blog/salesforce/apex-based-sharing-in-salesforce/
Ranjeet Singh (SFDC Developer)Ranjeet Singh (SFDC Developer)
Hi Karthick,

Can i know what error you are receiving?

I will suggest you:
1. Check the OWD, OWD for Test__C object must be either Private Or Public Read Only

Code looks without error, Please share the error message.

Thanks & Regards,
Ranjeet Singh.
This was selected as the best answer
SS KarthickSS Karthick
Please Help