You need to sign in to do that
Don't have an account?
Account team member update from Opportunity
HI there,
my problem is to update or insert an account team member record on the account related to the opportunity where this trigger is. So, basically, after updating an opporunity, the Owner of the opportunity (if a certain type criteria is matched ) needs to be part of the account team, if not already, of the accont related to the opportunity.
If the opportunity owner is already an account team member of the account, update the TEammemberrole to "Account manager".
If the opportunity owner is NOT an account team member of the account related, insert a new account team member record with the teammemberrole 'Account manager'.
I'm getting an error when updating the opportunity: first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]: Trigger.AMforopptTEAMmember: line 30, column 1
Here is the trigger:
trigger AMforopptTEAMmember on Opportunity (after update) { Set<Id> recordIds = new Set<Id>(); Set<Id> acctIds = new Set<Id>(); Set<Id> userIds = new Set<Id>(); Map<Id,Id> ownerIds = new Map<Id,Id>(); for(opportunity S:Trigger.new) if(S.type == 'Renewal' && S.stagename<>'Closed Won' && S.stagename<>'Closed Lost' ){ recordIds.add(S.id); acctIds.add(S.accountid); userIds.add(S.ownerid); ownerIds.put(s.accountid,s.ownerid); } List < opportunity> usersOK = new List < opportunity >{[select id, ownerid, accountid from opportunity where id in :recordIds]}; List < Accountteammember> membersToUpdate = new List < Accountteammember >{[select id, userid, TeamMemberRole from accountteammember where accountid in :acctIds and Userid in :userIds]}; { IF(memberstoupdate.size() > 0) memberstoupdate[0].TeamMemberRole = 'Account Manager'; } update memberstoupdate; List < Accountteammember> membersToinsert = new List < Accountteammember >{[select id, userid, TeamMemberRole from accountteammember where accountid in :acctIds and Userid in :userIds]}; IF(membersToinsert.size()== 0) Accountteammember newmember= new Accountteammember( accountid=usersOK[0].accountid, teammemberrole = 'Account Manager', Userid=usersOK[0].ownerid); insert memberstoinsert; }
Thanks!
B
Line 30 is inserting memberstoinsert. But memberstoinsert is not a list of new records (which have no id, and which you are permitted to insert). Instead, memberstoinsert is a list of existing records that you queried from the database on line 26. You cannot insert them because they are already in the database.
I suggest that you rewrite everything from line 26 down. Your logic is just off; the query on line 26 is the same as the one on line 17.
Thanks bo! I see what you're saying and re-wrote the lines to this:
The issue now is List has no rows for assignment to SObject: Trigger.AMforopptTEAMmember: line 15, column 1
and it comes after updating an opportunity. how come no rows if I'm querying the current opportunity for its id, ownerid and account id ??
Thanks for the help!
B
ok... for the lists on lines 15 and 17, if it can't return a row, it is givin me an error. the thing is taht I have an IF statement that (the Else If) that if it returns 0 rows ( size == 0) then, insert a record. Shouldn't it take care of a query with 0 results ?
Thx!
All done.. The issue is that the "List" on lines 15 and 17 should not be = NEW LIST but just the SOQL statement.
I.e.
List < Accountteammember> membersToUpdate = [select id, userid,....
B
That's right, Baguiar2.
This construct:
uses the constructor for List. It expects a list of comma separated Opportunity sobjects between the curly brackets.
This construct:
does not explicitly invoke the List constructor. It takes advantage of the fact that a naked soql statement returns a list of sobject.