+ Start a Discussion
saad mechiche alamisaad mechiche alami 

Trigger field is not Writeable

hello everyone;

In the code below I am trying to put a contract owner in the opportunity team record and i want to grand him read/write permission in that record 
My issue is that when i try to save that record, I get an error message saying 

Error: Compile Error: Field is not writeable: OpportunityTeamMember.OpportunityAccessLevel at line 27 column 1


can you please help, many thanks 

 
trigger OpportunityBeforeUpdate1 on Opportunity (before update) {
string oppName;
opportunity oppId;
SVMXC__Service_Contract__c smc= new SVMXC__Service_Contract__c();
for (opportunity opp: trigger.new)
oppName=opp.name;
oppId=[select Id, name from opportunity where name=:oppname limit 1];
integer res=oppName.indexof(' - ');
String ConName=oppName.left(res);
smc=[select name, OwnerId from SVMXC__Service_Contract__c where name=:ConName limit 1];
OpportunityTeamMember oppt = new OpportunityTeamMember();
oppt.OpportunityId=oppId.Id;
oppt.userid=smc.ownerid;
oppt.OpportunityAccessLevel='edit';
oppt.TeamMemberRole='Service Sales Contributor';

insert oppt;
}


 
Best Answer chosen by saad mechiche alami
Balaji BondarBalaji Bondar
Hi Saad,

I have rewritten the trigger as below:
trigger OpportunityBeforeUpdate1 on Opportunity (before update) {
SVMXC__Service_Contract__c smc= new SVMXC__Service_Contract__c();
	for(Opportunity opportunityObj : [select Id, Name from  Opportunity where Id IN: Trigger.New]){
		Integer res=opportunityObj.Name.indexof(' - ');
		String ConName=opportunityObj.Name.left(res);
		smc=[select Id, name, OwnerId from SVMXC__Service_Contract__c where name=:ConName limit 1];
		
		OpportunityTeamMember member = new OpportunityTeamMember();  
		member.OpportunityId = opportunityObj.Id;  
		member.UserId = smc.ownerid;  
		member.TeamMemberRole = 'Sales Rep'	
		insert member;
		
		// get all of the team members' sharing records
		List<OpportunityShare> shares = [select Id, OpportunityAccessLevel,  
		  RowCause from OpportunityShare where OpportunityId = :opportunityObj.Id
		  and RowCause = 'Team'];

		// set all team members access to read/write
		for (OpportunityShare share : shares)  
		  share.OpportunityAccessLevel = 'Edit';

		update shares;
	}
}
Note: update the code to handle the bulk records.

Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.

All Answers

Balaji BondarBalaji Bondar
Hi Saad,
You need to add the team members to the opportunity and then update the sharing access to the opportunity for these users.
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;

Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
saad mechiche alamisaad mechiche alami
Hello and thanks for your answer Balaji ,

I have updated the code using your part as posted below, I have 2 questions 

1/when I try to put opp.id in the code (see bold line in the code)
member.OpportunityId = opp.Id;

it is not working and I get the error:
Variable does not exist: opp.Id

that's why I am putting the query : oppId=[select Id, name from opportunity where name=:oppname limit 1];
is there a way to get the ID of the current oppotunity (in each iteration of the for loop)

2/ the user inserted in the opportunity team in my code is still having read only in the Opportunity Access
while in the code:
share.OpportunityAccessLevel = 'Edit'


Code:
 
trigger OpportunityBeforeUpdate1 on Opportunity (before update) {
string oppName;
opportunity oppId;
SVMXC__Service_Contract__c smc= new SVMXC__Service_Contract__c();
for (opportunity opp: trigger.new)
oppName=opp.name;
oppId=[select Id, name from opportunity where name=:oppname limit 1];
integer res=oppName.indexof(' - ');
String ConName=oppName.left(res);
system.debug('saad--------->   ' + oppName);
system.debug('saad-----------> contract name is  '+oppName.left(res));
smc=[select name, OwnerId from SVMXC__Service_Contract__c where name=:ConName limit 1];
system.debug('the owner is -------------------->          '+smc.ownerid);
//------------code added---------------//
OpportunityTeamMember member = new OpportunityTeamMember();  
member.OpportunityId = oppId.Id;  
member.UserId = smc.ownerid;  
member.TeamMemberRole = 'Service Sales Contributor';
insert member;
// get all of the team members' sharing records
List<OpportunityShare> shares = [select Id, OpportunityAccessLevel,
RowCause from OpportunityShare where OpportunityId=:oppId.id and RowCause = 'Sales Team'];
// set all team members access to read/write
for (OpportunityShare share : shares)  
  share.OpportunityAccessLevel = 'Edit';

update shares;
}

 
Balaji BondarBalaji Bondar
Hi Saad,

I have rewritten the trigger as below:
trigger OpportunityBeforeUpdate1 on Opportunity (before update) {
SVMXC__Service_Contract__c smc= new SVMXC__Service_Contract__c();
	for(Opportunity opportunityObj : [select Id, Name from  Opportunity where Id IN: Trigger.New]){
		Integer res=opportunityObj.Name.indexof(' - ');
		String ConName=opportunityObj.Name.left(res);
		smc=[select Id, name, OwnerId from SVMXC__Service_Contract__c where name=:ConName limit 1];
		
		OpportunityTeamMember member = new OpportunityTeamMember();  
		member.OpportunityId = opportunityObj.Id;  
		member.UserId = smc.ownerid;  
		member.TeamMemberRole = 'Sales Rep'	
		insert member;
		
		// get all of the team members' sharing records
		List<OpportunityShare> shares = [select Id, OpportunityAccessLevel,  
		  RowCause from OpportunityShare where OpportunityId = :opportunityObj.Id
		  and RowCause = 'Team'];

		// set all team members access to read/write
		for (OpportunityShare share : shares)  
		  share.OpportunityAccessLevel = 'Edit';

		update shares;
	}
}
Note: update the code to handle the bulk records.

Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
This was selected as the best answer
saad mechiche alamisaad mechiche alami
Thanks so much , that works exactly as expected