Posted in Apex, Uncategorized

Apex Sharing

Hi Guys, Today we came up with very exciting topic . Do you know what ?? Lets guess . Its none other than Apex Sharing.

Have you ever shared your records through Apex Sharing. ??

If not you are at right place to learn.

Lets get into the content.

Lets take the scenario like , Your OWD settings are Private on Custom Object (Job__c). If we like to share this record with Community User we have to do it at Profile / Permission Set Level.

Lets try to make it and come back to the next point

Have you find is there any way to share this with View All / Modify All ??

Ultimately not.

This is where you need this blog post.

In order to share the record with any Community / Community Plus User We have to share that record with Apex. 🙂

Have to ever heard about ” __Share ” in Salesforce. If not we have to learn it first in order to go further.

For every Custom Object we are creating , Salesforce will create a share object along with your Custom object.

I can understand the sentence it bit tricky . Lets form it with Example.

Lets take you are trying to create New object named as “Expense__c”.

While you creating this object, salesforce will give you other object also called as “Expense__Share”.

We have to make use of this “Expense_Share” object in our Apex Sharing.

Lets get into the Coding part.

Lets take We have to share one of the Record from “Expense__c” with Community User .

Lets take the User Id is “005XXXXXXXXXXXX”

In order to share this record with the above user , make use of this code.

List<Expense_Share> shareList = new List<Expense_Share>();

Expense__c getAllExpense= [select id from Expense__c limit 100];

for(Expense__c eachExpense : getAllExpense){

Expense_Share sharingInst = new Expense_Share();

sharingInst.ParentId =;

sharingInst.UserOrGroup = ‘005XXXXXXXXXXXX’;




insert shareList;

}catch(Exception e){}


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s