0

To Replicate 'Default Opportunity Member' I created a Javascript Button and below is the Code:

{!REQUIRESCRIPT('/soap/ajax/33.0/connection.js')}
{!REQUIRESCRIPT('/soap/ajax/33.0/apex.js')} 

var opObj = new sforce.SObject("Opportunity");
var userTeamObj = new sforce.SObject("UserTeamMember");
opporObj = '{!Opportunity.Id}';
var getOwner = sforce.connection.query("select id, Ownerid from Opportunity where id = '{!Opportunity.Id}'");
var ownID = getOwner.records.OwnerId;

var usersWithTeam = [];
usersWithTeam = sforce.connection.query("SELECT Id, TeamMemberRole, OpportunityAccessLevel, userId  FROM UserTeamMember where OwnerId = '"+ownID+"'");

var ot = [];
for(var i = 0; i < usersWithTeam.size; i++){
    if(usersWithTeam.records[i].OpportunityAccessLevel != '' && usersWithTeam.records[i].TeamMemberRole != ''){
        console.log('user ---'+usersWithTeam.records[i]);
        var opTeamObj = new sforce.SObject("OpportunityTeamMember");
        opTeamObj.UserId = usersWithTeam.records[i].UserId;
        opTeamObj.OpportunityId = opporObj;
        opTeamObj.OpportunityAccessLevel = usersWithTeam.records[i].OpportunityAccessLevel;
        opTeamObj.TeamMemberRole = usersWithTeam.records[i].TeamMemberRole;
        ot.push(opTeamObj);
    }
}

var result = sforce.connection.create(ot); 
console.log('result--'+result);

But, it's giving me the below error:

message:'Unable to create/update fields: OpportunityAccessLevel. Please check the security settings of this field and verify that it is read/write for your profile or permission set.', statusCode:'INVALID_FIELD_FOR_INSERT_UPDATE'

I checked all the permission and they are fine. I did the same functionality using Apex Class in Anonymous Window and was able to do this easily. But I am unable to understand why this is giving me error in this case.

  • Please tell me what kind of permission you have in profile level? – SFDC FAN Jul 3 '18 at 12:24
  • Profile is Administrator, so that covers all. – Kamal Thakur Jul 3 '18 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.