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
rsenthil86rsenthil86 

Apex Sharing

how to give acces to the profile through apex sharing? 

bob_buzzardbob_buzzard

Apex sharing works for users and groups, not profiles I'm afraid.

 

There's an excellent wiki article on this at: 

 

http://wiki.developerforce.com/page/Using_Apex_Managed_Sharing_to_Create_Custom_Record_Sharing_Logic

rsenthil86rsenthil86

Hi,

Thanks for your reply. I have a field name called " Status". if the status "Open". Then I wanna give access to Users. How can we do that???

bob_buzzardbob_buzzard

That sounds like you'd have a trigger for insert/update.

 

When the status transtions to Open, create the sharing rules.

When the status transitions from Open, remove them.

rsenthil86rsenthil86

Hi,

 

Thanks for your reply. We have to create a group for the users or wat???

bob_buzzardbob_buzzard

It does depend on your use case, but I tend to favour creating a public group for the users.  I haven't had to do this for a while, but I seem to recall that I needed to remove users from the public group when they were deactivated or the code threw an error.

rsenthil86rsenthil86

Thanks for your reply. If I am using groups then how can i use this group in trigger? how to query to get the group id through SOQL?. Once again thanks for your reply.

softectonicsoftectonic

Here is an example of how you can query the Group Id using SOQL:

 

SELECT Id FROM Group WHERE RelatedId = : [SELECT Id FROM UserRole WHERE Name = '<Insert Role Name Here>'];

 

You can then set the Group Id to 'UserOrGroupId' attribute of the Share Object you are after.

 

Good luck!

rsenthil86rsenthil86

Hi,

 

Thanks for your reply. I got it. I am using more than one group, so please tell me how to use in list. Once again thanks for your reply.

softectonicsoftectonic

Where you have more than one role, one option is to assign the Roles to a Public Group. Then set the Group Id for the Public Group to the UserOrGroupId attibute of the Share object.

bob_buzzardbob_buzzard

I'm a bit confused now - this started off asking about sharing with groups but now seems to have drifted into a way to put roles into groups.

 

To clarify, what I was suggesting was:

 

  • Create a public group for the users in question
  • Write insert/update triggers that react to changes in the status and add/remove sharing rules for the group

With regard to identifying the group from apex code, I usually store this information in a custom setting.