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

Share the records in an account with all the contacts in it

hi everyone,


I have a class which shares all the records that are in an account with all the contacts in that particular account. Meaning, Account A has contacts c1, c2, c3. If account A has lets say 10000 records then the same number of records should be shared with contacts c1, c2 and c3. Also, if a new contact c4 is created under that account A, then that contact c4 should also have 10000 records. Anytime a new record is added to that account, all these contacts should have that updated records.


The code I have works if the record size is less, but I have an account with 78K records so the code hits governor limits.

The records are being fetched to that account from another class Order, Items, Price. I have triggers in all these classes to this class that is supposed to share the records.


Does anyone have any suggestion on what can be done to resolve this issue. Any suggestion is helpful.


Platy ITPlaty IT

I'm not entirely sure I understand your use case, but if you don't see a way to optimize your code, you could try putting your logic into a @future method that gets called from the trigger.  This largely expands the governor limits available.  Just be aware that the future method is run in a separate call after the trigger, so users may see a slight delay in the changes happening and if any errors are hit, they won't be aware of it- it's worth including a Catch statement that emails you if there are any issues.

Kiran  KurellaKiran Kurella

Are these 78K records on a custom object and related to a Account record? If so, are you using a Master Detail or a lookup relationship between the custom object and Account object? If you are using a Master Detail relationship then you can enable/create Accounts Team and share the records to all contacts with few lines of code. You basically need to add code to add a Contact to the Accounts Team with the appropriate team user role.


The records are shared using a lookup relationship. Any idea on that.


I am actually using @future method for this class. Do you think I should change this to batch apex? would that work or is there any alternative solution to that.

Kiran  KurellaKiran Kurella

I recommend a batch apex for this process as the future method is subject to the same governor limits and may fail, if the number of records increases significantly. Also the Future method has few limitations. see the link below.


Salesforce also imposes a limit on the number of future method invocations: 200 method calls per full Salesforce user license, Salesforce Platform user license, or - One App user license, per 24 hours. This is an organization-wide limit. Chatter Only, Chatter customer users, Customer Portal User, and partner portal User licenses aren’t included in this limit calculation. For example, suppose your organization has three fullSalesforce licenses, two Salesforce Platform licenses, and 100 Customer Portal User licenses. Your entire organization is limited to only 1,000 method calls every 24 hours, calculated as 200 * (3+2), not 200 * (3+2+100).