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.
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.
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.
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
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???
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.
Hi,
Thanks for your reply. We have to create a group for the users or wat???
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.
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.
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!
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.
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.
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:
With regard to identifying the group from apex code, I usually store this information in a custom setting.