Toggle Side Panel

  • Home
  • Articles
    • All Articles
    • Blogs
    • Videos
    • Infographics
  • Consultants
    • Salesforce Product Expertise
      • Top Salesforce ConsultantsTop Salesforce Consultants
      • Marketing Cloud ConsultantsMarketing Cloud Consultants
      • Service Cloud ConsultantsService Cloud Consultants
      • Experience Cloud ConsultantsExperience Cloud Consultants
      • Analytics Cloud ConsultantsAnalytics Cloud Consultants
    • Salesforce Industry Expertise
      • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
      • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
      • Health Cloud ConsultantsHealth Cloud Consultants
      • Commerce Cloud ConsultantsCommerce Cloud Consultants
      • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
    • Salesforce Experts by Location
      • USATop Salesforce Consultants in USA
      • IndiaTop Salesforce Consultants in India
      • AustraliaTop Salesforce Consultants in Australia
      • United KingdomTop Salesforce Consultants in UK
      • CanadaTop Salesforce Consultants in Canada
  • Webinars
  • Contact Us
  • Discussions
More options
    Sign in Sign up
    • Home
    • Articles
      • All Articles
      • Blogs
      • Videos
      • Infographics
    • Consultants
      • Salesforce Product Expertise
        • Top Salesforce ConsultantsTop Salesforce Consultants
        • Marketing Cloud ConsultantsMarketing Cloud Consultants
        • Service Cloud ConsultantsService Cloud Consultants
        • Experience Cloud ConsultantsExperience Cloud Consultants
        • Analytics Cloud ConsultantsAnalytics Cloud Consultants
      • Salesforce Industry Expertise
        • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
        • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
        • Health Cloud ConsultantsHealth Cloud Consultants
        • Commerce Cloud ConsultantsCommerce Cloud Consultants
        • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
      • Salesforce Experts by Location
        • USATop Salesforce Consultants in USA
        • IndiaTop Salesforce Consultants in India
        • AustraliaTop Salesforce Consultants in Australia
        • United KingdomTop Salesforce Consultants in UK
        • CanadaTop Salesforce Consultants in Canada
    • Webinars
    • Contact Us
    • Discussions
    Close search

    Dynamic Sharing of Standard & Custom Objects through Salesforce Apex

    BLUE FLAME LABS PRIVATE LIMITED Feb 26, 2018
    17,311  Views

    Apex Sharing is sharing record access through a program using Salesforce Apex through which can grant access to users or groups of users.

    We can use the Salesforceuser interface and Force.com for Apex Sharing.

    Apex sharing for partner account supports scenarios such as Account sharing, Case Sharing, Opportunity sharing, Lead sharing, and Custom Object Record sharing. Sharing sets are not available for partner communities, only customer communities. This code allows for standard and custom objects to be shared with a partner account.

    Apex sharing for standard object:

    Let’s take an example: Account

    In standard objects, the field ParentId and AccessLevel have object name prefixed. As an example in AccountShare object, these fields are called AccountId and AccountAccessLevel respectively.

    The following code finds all accounts owned by a user and shares them with their respective portal user.

    Below you can see the code to create sharing rule for Account object through Apex. Each object has its own sharing object. In this case, it is “AccountShare”

    For (User u : [SELECT Id FROM User where User.Profile.UserLicense.Name = 'Partner Community']) 
    {
        AccountShare a = new AccountShare();
        a.AccountId = acc.id;  // record to which  we need to give access to.
        a.UserOrGroupId = u.Id; //Set the portal user Id to share the accounts with
        a.AccountAccessLevel = accesslevel;
        a.OpportunityAccessLevel = accesslevel;
        a.RowCause = 'Manual';
        shrobjlst.add(a);
    }
    Insert shrobjlst;

    Apex sharing for custom object:

    Let’s take an example: Employee Custom Object

    The fields of share object for custom objects that should be filled in are explained below:

    Field name in
    Custom Object

    Description

    ParentId

    The Id of the object to which access is being granted

    UserOrGroupId

    The id of user or group to which access is being granted

    AccessLevel

    “Read” or “Edit” or “All”

    As an example, for custom object Employee__c, share object will be called  Employee __share (note that c after the custom object name is removed).

    In the first example, we create a share for a custom object.

    shrobjname  = Employee__share;

    For (User u : [SELECT Id FROM User where User.Profile.UserLicense.Name = 'Partner Community']) 
    {
        sObject empShare = Schema.getGlobalDescribe().get(shrobjname.toLowerCase()).newSObject(); 
        empShare.put('ParentId',empID.Id) ;
        empShare.put('UserOrGroupId',u1.Id) ; //Set the portal user Id to share the accounts with
        empShare.put('AccessLevel',accesslevel) ;  
        empShare.put('RowCause','Manual');  
        shrobjlst.add(empShare);  
    }
    Insert shrobjlst;
    Categories: Salesforce Apex, Salesforce Implementation
    Tagged: Coding and Scripting, Dynamic Sharing, Object Sharing, Salesforce Apex, Salesforce Apex Code, Salesforce Apex Page, Salesforce Apex Trigger, Salesforce Custom Objects, Salesforce Development, Salesforce Implementation, Standard Object in Salesforce
    salesforce consultants
    Footer Forcetalks logo

    support@forcetalks.com

    • twitterx

    Quick Links

    Advertise with Us

    Salesforce® Articles

    Dreamforce 2023

    Top Salesforce® Bloggers 2023

    Top Salesforce Consultants

    Get Listed

    Company

    Contact Us

    About Us

    Privacy Policy

    Terms & Conditions

    InsightHub

    Salesforce Blogs

    Salesforce Videos

    Salesforce Groups

    Salesforce Jobs

    © 2026 - Forcetalks ● All Rights Reserved

    Salesforce® is a trademark of Salesforce® Inc. No claim is made to the exclusive right to use “Salesforce”. Any services offered within the Forcetalks website/app are not sponsored or endorsed by Salesforce®.

    Try AuditMyCRM - It is a Salesforce CRM Audit tool which comprehensively scans your Salesforce org and gives you the list of errors or warnings you need to take care of.
    We use cookies to enhance your browsing experience. Please see our privacy policy if you'd like more information on our use of cookies.