+ Start a Discussion
grandmastergrandmaster 

Is it possible to update the UserId of an AccountTeamMember

Hi All

 

I got stuck with this so I thought of asking it here. Any help would be highly appreciated.

 

Well, I am trying to write a test class wherein an AccountTeamMember record is created. I am not able to assign a value to the UserId. The following is the error:

 

Field is not writeable: AccountTeamMember.UserId

 

The following is the code:

 

@isTest
private class TestFox {
static testMethod void testTriggerContactMAMDforCO() {
Map<String, Id> recordTypes = new Map<String, Id>();
for (RecordType rt : [SELECT Id, SObjectType, DeveloperName FROM RecordType]) {
recordTypes.put('' + rt.SObjectType + rt.DeveloperName, rt.Id);
}


// Create an Entity
Account acct = new Account(name='Test Corporation',Sales_Report_Code__c='5000',RecordTypeId = recordTypes.get('AcctBr'));
insert acct;

// Create an Entity Team
AccountTeamMember atmMD = new AccountTeamMember(AccountId=acct.Id,TeamMemberRole='Marketing Dir', UserId='005E0000000KojD');
insert atmMD;

AccountTeamMember atmMA = new AccountTeamMember(AccountId=acct.Id,TeamMemberRole='Marketing Ass', UserId='005E0000000JaQD');
insert atmMA;


// Create a Contact and assign it an account
Contact con = new Contact(AccountId=acct.Id,lastname='lasty',firstname='firsty',BAD__c=true,RecordTypeId = recordTypes.get('CAD'));

//Now insert data ......
Test.startTest();
insert con;

// Now change the Account's Sales_Report_Code__c and Account Channel
acct.Sales_Report_Code__c='1234';
acct.Channel__c='Company Offices';
update acct;

// Update the Account Team Member users
atmMD.UserId='005E0000000JF9m';
update atmMD;

atmMA.UserId='005E0000000K9aE';
update atmMA;

// Update the contact , any field other than Marketing Dir & Marketing Ass
con.FirstName='firstyyyy';
con.BAD__c=false;
update con;
System.assertEquals(con.Marketing_Dir__c,'005E0000000JF9m');
System.assertEquals(con.Marketing_Ass__c,'005E0000000K9aE');

Test.stopTest();
}
}

 

Any suggestion what can be done better here to eliminate the error? 

 

Thank you

g

Best Answer chosen by Admin (Salesforce Developers) 
Rahul_sgRahul_sg
firstly dont hardcode the Ids in your code.
Insert USers or query them and use thier ids.

then see this link :http://boards.developerforce.com/t5/General-Development/Cant-create-AccountTeamMember-with-AccountAccessLevel-quot-Edit/td-p/341299

Basically you need to add 1 teammember record and 1 team share record

All Answers

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

You cannot update the UserId of accountteammember, Instead delete the accountteammember and create a new one with the UserId you wanted to update.

grandmastergrandmaster

Hi Saikishore

 

Thanks for your suggestion. What do I need to change in the code?

 

Thanks

g

grandmastergrandmaster

I tried to delete the accountteammember, but it still gives the same error that I cant update the UserId of the Account TeamMember.

 

// Create an Entity
Account acct = new Account(name='Test Corporation',Sales_Report_Code__c='5018',RecordTypeId = recordTypes.get('AccountBranch'));
insert acct;
// Create an MD and MA user
User uMD = new User(Firstname='JoeFirstTest', Lastname='JoeLastTest');
User uMA = new User(Firstname='JackFirstTest', Lastname='JackLastTest');

AccountTeamMemeber atmMDtoDel = [Select Id from AccountTeamMember where UserId='005E0000000K9aE' Limit 1];
System.Debug('atmMDtoDel'+atmMDtoDel);
delete atmMDtoDel[0];
AccountTeamMemeber atmMAtoDel = [Select Id from AccountTeamMember where UserId='005E0000000JbBv' Limit 1];
delete atmMAtoDel[0];

// Create an Entity Team
AccountTeamMember atmMD = new AccountTeamMember(AccountId=acct.Id,TeamMemberRole='Marketing Director', UserId='005E0000000K9aE');
insert atmMD;
AccountTeamMember atmMA = new AccountTeamMember(AccountId=acct.Id,TeamMemberRole='Marketing Associate', UserId='005E0000000JbBv');
insert atmMA;

 

Any suggestion what can be done better?

 

Thanks so much.

g

grandmastergrandmaster

Any suggestions?

grandmastergrandmaster

Any suggestions  ?

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

Can you post the error you are getting?

grandmastergrandmaster
14:34:35.746 (2746135000)|FATAL_ERROR|System.SObjectException: Field is not writeable: AccountTeamMember.UserId

Class.TestFox.testTriggerContactMAMDforCO: line 90, column 1
14:34:35.746 (2746151000)|FATAL_ERROR|System.SObjectException: Field is not writeable: AccountTeamMember.UserId

 

Rahul_sgRahul_sg
firstly dont hardcode the Ids in your code.
Insert USers or query them and use thier ids.

then see this link :http://boards.developerforce.com/t5/General-Development/Cant-create-AccountTeamMember-with-AccountAccessLevel-quot-Edit/td-p/341299

Basically you need to add 1 teammember record and 1 team share record
This was selected as the best answer
grandmastergrandmaster

I visited the link you shared. I wasn't able to understand what is this variable:'userAccountTeamMembers'

in the code out there?

 

thanks,

gm

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

While writing test classes you should not directly give some id, means don't use Hardcode value of id.

 

Do the following and refer this u.id in inserting the accountTeamMember userID field, then run the test class again,

 

  user u = [select id,name from user where id=:UserInfo.getUserId() limit 1];

 

Hope this will help you...!

 

Please don't forget to give kudos by clicking on the Star icon and mark this as a solution, if this works out.

 

grandmastergrandmaster

This is resolved so I'm closing it myself.

 

Had to create an accountshare. The code went like this:

 

AccountTeamMember atmX= new AccountTeamMember(AccountId=acct.Id,TeamMemberRole='Boss', UserId=uid);     
insert atmX;   
Accountshare ashX=new Accountshare(AccountId=acct.Id,UserOrGroupId=atmX.UserId,AccountAccessLevel='Read',OpportunityAccessLevel='Read');
insert ashD;

 

So the gist is that when you cant insert the userid in accountteammember, create an accountteamshare and set its UserOrGroupID=atxX.UserID  

... and then insert the accountteamshare

 

Kind of longer but thats how it works

Ravi Shankar ReddyRavi Shankar Reddy

Can you give me the complete code. i have simillar requirement but getting same error while updating and deleting Account team member

USER_DEBUG [121]|DEBUG|Exception getting at RemoveTeammembers: Field is not writeable: AccountTeamMember.UserId