You need to sign in to do that
Don't have an account?

Apex Trigger Code for Oppty Owner to be automatically added to Oppty Sales Team
Could someone provide the Apex code for a trigger that automatically pulls the opportunity creator/owner to the sales team on the oppty? Here is an example: User: John Smith Profile: CDS Whenever this guy creates (owns) an oppty, i would like him to immediately be added to the Sales Team with the team role "Sales - CDS" * all of the team roles say "sales - " and then the department which always matches the profile name. Can anyone help? :-) |
You need to do 2 things in order to create an Opportunity Team Member automatically using a trigger.
First Create a share for the team member. In the example below the Access Level is set to read but this will not matter as your user owns the opportunity and will have inherited read/write privileges
Next create the team member itself.
Enjoy
i am adding directly from the leads
Here is the code
trigger salesContacts on Opportunity (after insert) {
Set<Id> oppId = new Set<Id>();
Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>();
for(Opportunity newOpp :trigger.new){
oppId.add(newOpp.Id);
oppMap.put(newOpp.Id,newOpp);
}
List<OpportunityTeamMember> tList = new List<OpportunityTeamMember>();
List<OpportunityShare> shList = new List<OpportunityShare>();
// OpportunityTeamMember newT = new OpportunityTeamMember();
for(Opportunity newOpp : trigger.new){
OpportunityShare processorSharingRule = new OpportunityShare();
processorSharingRule.OpportunityId = newOpp.Id;
processorSharingRule.OpportunityAccessLevel = 'Edit';
processorSharingRule.UserOrGroupId = newOpp.OwnerID;
system.debug(' processorSharingRule.UserOrGroupId:'+ processorSharingRule.UserOrGroupId);
shList.add(processorSharingRule);
OpportunityTeamMember newT = new OpportunityTeamMember();
if(newOpp.Partner_User__c !=null){
newT.TeamMemberRole = 'Reseller';
newT.UserId = newOpp.Partner_User__c;
newT.OpportunityId = newOpp.Id;
tList.add(newT);
}
}
if(shList.size()>0)
insert shList;
if(tList.Size() > 0)
insert tList;
}
Error: salesContacts: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: [] Trigger.salesContacts: line 35, column 9
when iam trying to convert to the opportunity
the error is on
converted status field
iam trying to get this done,Please help me out of this
I looked over your code and it looks like you are attempting to create the share with a different user than the team memebr.
Share Owner:
processorSharingRule.UserOrGroupId = newOpp.OwnerID;
Team Member:
newT.UserId = newOpp.Partner_User__c;
These two would need to be the same in order for you to create a sharing rule using the trigger.
Also, you have an IF statement that qualifies that the Partner_User__c field is not null. If that field is null then your code will not attempt to create a new team memeber.
Lastly, I am assuming that the Partner_User__c field is a lookup to a valid, active User Object.
Based on this I worked up an alternate trigger that should meet your needs.
my previous code & this is also working but the only thing is that iam not gettting control on the OpportunityShare as it is everytime creating with 'read Only' i want to get edit when it was created
Try adding an OpportunityAccessLevel line when inserting the Team Member.
OpportunityTeamMember ot = new OpportunityTeamMember();
ot.OpportunityId = trigger.new[0].Id;
ot.OpportunityAccessLevel = 'Edit';
ot.UserId = trigger.new[0].Partner_User__c;
ot.TeamMemberRole = 'Reseller';
oppteam.add(ot);
field is not writable & i had seen no field OpportunityShare on the OpportunityTeamMember ,Iam getting error field is not writable
while adding this ot.OpportunityAccessLevel = 'Edit';
Thanks for the help!
So i was just trying to get this to work as well, and it turns out that you need to first create the opportunity team members, then create their opportunity shares. All the sample code above creates the shares first, then the team members... this will only create read-only access! If you create the team members first, you can then create a share to set them to 'read/write' by setting the share 'opportunityAccessLevel' to 'Edit'
I have just started coding some complex triggers, however, I am not completely aware of things yet!!
Can someone please explain me why are we creating "OpportunityShare"? And also why was trigger.new[0] used with an index[0]?
Would be a great help!!