1

Hi I have written an Apex trigger to add the Opportunity owner name to Account team Member if the opportunity probability is equals to 50%.

However Iam getting errror called Field is not Writeable.

Please find the trigger code and error message below.

trigger addAccountTeam on Opportunity (before insert) {

List<AccountShare> acc_share = new List<AccountShare>();
List<AccountTeamMember> acc_Team = new List<AccountTeamMember>();

for(Opportunity opp: Trigger.New)
{
    if(opp.Probability == 50)
    {
        AccountTeamMember team = new AccountTeamMember();
        team.AccountId = opp.AccountId;
        team.UserId = opp.OwnerId;

        AccountShare share = new AccountShare();
        share.AccountId = opp.AccountId;
        share.AccountAccessLevel = 'Read/Write';
        share.OpportunityAccessLevel = 'Read Only';
        share.CaseAccessLevel = 'Read Only';
        acc_share.add(share);
        acc_Team.add(team);
    }
}
if(acc_Team != null)
{
    insert acc_Team;
}
if(acc_share != null && acc_share.size()>0)
{
    insert acc_share;
}

}

please find the error message

enter image description here

2
  • What version is your trigger running in?
    – sfdcfox
    Commented Sep 21, 2017 at 3:28
  • @sfdcfox version 40 Commented Sep 21, 2017 at 3:29

1 Answer 1

4

Two main problems exist here.

First, your org wide defaults are already full-open. You can't access AccountShare fields when they're set this way, because the system won't save them. Either don't create any AccountShare records, or reduce your access levels.

Second, if you decide to reduce your access levels so you can create AccountShare records, be advised that the legal values for the Access fields are "None", "Read", "Edit", and "All." You can't use the values that are displayed in the UI.

Finally, not a problem, but as a matter of optimization, please note that lists cannot magically become null, so the if-not-null checks at the end are not necessary:

insert acc_Team;
insert acc_share;
7
  • I tried with not creating the Account share record in the trigger ,but the trigger did not Added the probability owner in Account team member Commented Sep 21, 2017 at 3:47
  • @SamuelRobert I also forgot to mention that percentage fields are internally represented as decimals, so 50% is really 0.50.
    – sfdcfox
    Commented Sep 21, 2017 at 3:56
  • so it should be some thing like i should use different field for condition instead of Probability ? Commented Sep 21, 2017 at 4:03
  • @SamuelRobert Nothing wrong with using Probability, just remember that it is a decimal value, not a whole number.
    – sfdcfox
    Commented Sep 21, 2017 at 4:38
  • ok, but still the trigger was not successful, I could not see the owner of opportunity added to the Account team member Commented Sep 21, 2017 at 4:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .