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

how to create AccountShare object with RowCause field value


I am trying to create an AccountShare and AccountTeam record.

Requirement is: When an Account is created, Account has to be assigned to the Territory of the user who is creating the Account record. Also the user has to be added to the the AccountTeam for that particular Account. We are trying to achieve this using a Trigger and we are not using Assignment rules for lack of some feasibility reasons.


I have following two queries:

1. Is it possible to create AccountShare object or just it can be updated? When we try creating an AccountShare object it requests for all values of AccessLevel(Account,Opp,Case,Contact) fields and when provided with these values says AccessLevel cannot be below OWD for those objects though i am providing access above or same level as OWD for these objects.


2. i am trying to set RowCause field on AccountShare to TerritoryManual which i am unsuccessful becos it is a read only field. How can i set value for this field as i want to add the account to the territory of the user. Can the RowCause fied be updated for an exisitng AccountShare object in case we can't create AccountShare record as whenever an AccountTeam is created an entry is created in AccountShare.


Any help with this regard is appreciated. This is urgent!!






Please check the following code snippet. Hope this would be of some help.

AccountShare AccShare = new AccountShare(AccountId = acc.Id, 
AccountAccessLevel = 'Edit', OpportunityAccessLevel = 'Read',                         CaseAccessLevel = 'Read', UserOrGroupId = UserInfo.getUserId());

Insert AccShare;





Rowcause is editable only for custom objects not for standard objects.


All you can do is, you can create a share record in the accshare table however you can't set value to the row cause.


In your case Row Cause will default to 'Manual'




Salesforce standard Share objects like AccountShare,OpportunityShare...ect will not have write access on RowCause field.


This column explains *why* the Account record is shared to the specified User or Group. 
Values are:
A. Owner (the specified User is the record owner).
B. ImplicitParent (a child record related to this Account is owned by the specified User).
C. Team (the specified User is an Account Team member).
D. Manual (sharing was manually granted to the specified User).
E. TerritoryManual (the Account record was manually assigned to a Territory).
F. Territory (a Territory assignment rule granted access for this Account record to the specified Group.  Note: records marked as Territory cannot be edited/deleted via DataLoader/API).


Depends on the above reason it will populate the RowCause.




How to change the territory of an account in apex?