0

I want to move all the opportunity team members from one opportunity to another. In terms of the query, I'm able to query all of them with this query :

List<OpportunityTeamMember> l = [Select Name,TeamMemberRole from OpportunityTeamMember where opportunityId = '1234567'];

*The OpportunityId in my query is correct. Once I got the members, I attempted to use a for loop to iterate and create a new TeamMemberRole object assigning both the Name and Role in context of the iteration. But it says, that Name is not writeable so I'm wondering if there's another way around this

for(OpportunityTeamMember o : l) {
    OpportunityTeamMember otm = new OpportunityTeamMember();
    otm.Name = o.Name; //Issue
    otm.TeamMemberRole = o.TeamMemberRole;
    otm.OpportunityId = 'newId';
}
4
  • Gah DML in a for loop! What is it you think the Name field does, exactly?
    – Adrian Larson
    Aug 26 '16 at 0:50
  • lol I know ...I just put it there so everyone knows that I want to insert it. I'll just remove it. Its a lookup field, right? I mean looking through the Salesforce Inspector I see it as a String value Aug 26 '16 at 0:52
  • No, Name is not a lookup field...it is indeed a String. Are you perhaps looking for UserId? Here's the OpportunityTeamMember documentation for reference. I always search for it by googling <sObjectType> SOAP Salesforce. Always start by looking at the information, and describe the fields to get more (meta)data if you need it.
    – Adrian Larson
    Aug 26 '16 at 0:56
  • Also, just use the sObject.clone method. I should just write up an answer...
    – Adrian Larson
    Aug 26 '16 at 0:58
1

You can clone the OpportunityTeamMember records and just change the OpportunityId if you want to move the team. You may also wish to delete the old records, it is unclear from your post. Something like:

List<OpportunityTeamMembers> newTeam = new List<OpportunityTeamMember>();
for (OpportunityTeamMember member : existingMembers)
{
    OpportunityTeamMember newMember = member.clone();
    newMember.OpportunityId = someOpportunityId;
    newTeam.add(newMember);
}
insert newTeam;

Error handling should be included, but that tends to be context dependent and you haven't shared your context.

3
  • 1
    Also need to add fields required into the query or clone won't pick them up: List<OpportunityTeamMember> existingMembers = [SELECT Id, OpportunityAccessLevel, OpportunityId, TeamMemberRole, UserId FROM OpportunityTeamMember WHERE OpportunityId = '1234567'];
    – vorno
    Aug 26 '16 at 1:04
  • What exactly does clone() duplicate? Aug 26 '16 at 1:11
  • @user The sObject you call it on. In this case, the existing OpportunityTeamMember record (that you query for in your OP).
    – Adrian Larson
    Aug 26 '16 at 1:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.