OpportunityAccessLevel Not Writable

March 17th, 2011

I was working on a project the other day where I needed to dynamically add users to an opportunity’s Sales Team (OpportunityTeamMember object) so that users who do not normally have access to an opportunity based upon Org-wide security settings can work on the opportunity with other team members. One of the advantages of Sales Teams is that you can specify the level of access that each team member has for the opportunity. Some team members may need read/write access while others may just need read-only access.

From the opportunity page layout you can add a new team member and specify their access level and role.

Sales team

However, by default when you create a new team member via Apex, the platform grants them read-only access. So I tried to specify grant “Edit” (read/write) access with the following code.

1
2
3
4
5
OpportunityTeamMember member = new OpportunityTeamMember();
member.OpportunityId = SomeOpp.Id;
member.UserId = SomeUser.Id;
mmember.TeamMemberRole = 'Sales Rep';
member.OpportunityAccessLevel = 'Edit';

Specifying the OpportunityAccessLevel threw the following error when saving my class:

Save error: Field is not writable: OpportunityTeamMember.OpportunityAccessLevel

To be fair, the docs state that OpportunityAccessLevel is not creatable but it used to be with earlier versions of the API so I really wanted this to work and it was screwing up my day.

So here’s the solution that I can up with in case anyone is looking for a answers. You need to add the team members to the opportunity and then update the sharing access to the opportunity for these users.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
OpportunityTeamMember member = new OpportunityTeamMember();
member.OpportunityId = SomeOpp.Id;
member.UserId = SomeUser.Id;
mmember.TeamMemberRole = 'Sales Rep';
 
insert member;
 
// get all of the team members' sharing records
List<OpportunityShare> shares = [select Id, OpportunityAccessLevel, 
  RowCause from OpportunityShare where OpportunityId IN :SomeSetOfOpptyIds 
  and RowCause = 'Team'];
 
// set all team members access to read/write
for (OpportunityShare share : shares) 
  share.OpportunityAccessLevel = 'Edit';
 
update shares;

VN:F [1.9.22_1171]
Rating: 7.8/10 (5 votes cast)
VN:F [1.9.22_1171]
Rating: +2 (from 2 votes)
OpportunityAccessLevel Not Writable, 7.8 out of 10 based on 5 ratings

Categories: Apex, Code Sample, Salesforce

Leave a comment

Comments Feed13 Comments

  1. Gary Breavington

    Bizarre! I had a similar issue with Cases, however, with Cases there are CaseTeamRoles, an object which is linked to a CaseTeamMember, rather than the picklist approach on opportunities…

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  2. Sid

    Just to add some things here. When assigning the sharing settings for old opportunities, if the opportunity owners are inactive, the code throws an error. The trick here is either use Database.update or transfer all opportunities to some active user.

    Was stuck on this problem for a long time.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  3. Harshesh Shah

    @Sid
    Yes it throws error of Inactive_owner_or_user when the opportunity owner is inactive. The reason being when you add OpportunityTeamMember in that Opportunity, salesforce at its end tries to create sharing row that gives access of Opportunity team member to inactive user. This is what system doesn’t understand as to why inactive user should have share. I think this is the reason it throws error. AS you said we ca either transfer the user or may be activate the user for time being then deactivate it.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  4. Andy Ognenoff

    I’ve run into an issue like this but with Account Teams. We had to flip the order of execution to do the share table insert first then the team insert because the row cause was changing from team to manual. Doesn’t seem to be doing that for the sales team but it was an interesting behavior to track down.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  5. Paul Sterne

    Jeff, any chance you would be interested in doing a review of Match My Email.

    Paul Sterne
    Match My Email

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  6. Jason

    Ya, that inactive owner issue is a pain. The dumb thing is if you add a user through the UI and the opp owner is inactive it works fine but fails with API/Apex operations. I opened a bug ticket 3 years ago with support about this and it is still not fixed.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  7. Jason

    Wow, I so I just realized, with the help of @aogenoff ,http://twitter.com/aognenoff/status/56399804460306432 , this is actually very slick. What I didn’t realize is that when you updated the Opportunity Share it also updates the Team Memeber access. For some crazy reason I thought it would not.

    This is great! There are a few gotchas I see though. One you have to burn more DML/SOQL statements. Two, you can’t assume all users should have edit access so you would probably need to query the sales team of the opp owner and set permissions accordingly.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  8. Kevin

    Jeff, thanks for all the great posts. As on off beat question, what wordpress plug-in do you use for your code snippets?

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  9. Bankie

    Nice, thanks. Just ran into this one so you’ve saved me the hassle of using my brain again.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  10. Gayathri

    it throws error in my developer organisation.
    Error: Compile Error: Field is not writeable: OpportunityShare.OpportunityAccessLevel”

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  11. Gayathri

    found the reason why it throws compile error
    “Field is not writeable: pportunityShare.OpportunityAccessLevel”
    when OWD sharing is set as Public Read/write for opportunity there is no need to give sharing rule .

    when i make it as private, then my trigger code is saved

    VA:F [1.9.22_1171]
    Rating: 5.0/5 (1 vote cast)
    VA:F [1.9.22_1171]
    Rating: +1 (from 1 vote)
  12. Elly Bockley

    Great! I just hit this issue but with Accounts and AccountTeamShares. Thanks for the guidance.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  13. Syam

    Thank you @Gayathri

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)

Leave a comment

Feed

http://blog.jeffdouglas.com / OpportunityAccessLevel Not Writable