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

Trigger to update AccountShare object on insert only



I am a complete newbie to Apex triggers so hopefully someone can help.



Each day our SF org auto-imports Account records plus their related Account Team members (added to AccountTeamMember object) from an external source. When each related Account Team member is inserted it also automatically creates a record in the AccountShare table. This table has 3 "AccessLevel" fields that are given the following values:

 - AccountAccessLevel = 'Read'

 - OpportunityAccessLevel = 'None'

 - CaseAccessLevel = 'None'

* Note - These 3 values are set by default from our OWD settings which are Account, Opportunity and Private all = 'Private' and it has to stay this way.



But I need these 3 fields to have the following values:

 - AccountAccessLevel = 'Edit'

 - OpportunityAccessLevel = 'Read'

 - CaseAccessLevel = Edit'



What I do then each day to fix the problem is export any newly inserted records (via apex data loader) to a CSV file using this query:


   Select Id, AccountAccessLevel, OpportunityAccessLevel,

   CaseAccessLevel FROM AccountShare WHERE RowCause in ('Team') AND

   LastModifiedById in ('00590000001CEQQAA4') AND AccountAccessLevel in

   ('Read') AND CaseAccessLevel in ('None') AND OpportunityAccessLevel in ('None')


Then I manually change the 3 AccessLevel values in the CSV file to the values that  I need them to be (ie 'Edit' 'Read' 'Edit') and then I update the records back into SF using the apex data loader.



How would I write an Apex Trigger that does this update on insert automatically whenever a record is inserted into the AccountShare object  from the external source (ie LastModifiedById in ('00590000001CEQQAA4') ) and thus allow me to avoid doing this daily manual "export>modify>import" work-around?






Hello Matt,


Create an Account after insert trigger to perform the following steps:

  1. Query the AccountShare object using the account ids from
  2. Loop through the AccountShare records and update the appropriate fields.
  3. Add updated AccountShare records to a list.
  4. Perform an update call on the udpated AccountShare list.

If the trigger solution outlined above does not work because the AccountShare records are not yet created, you can use steps 1-4 to create an apex class and schedule it to run daily after the account auto-imports process.