You need to sign in to do that
Don't have an account?
Learning Apex (OOP)
Concept of after insert
Hello,
The apex code below is resolving the following requirement (specified as an exercises for practice):
When an opportunity is inserted, automatically add the Oppty's Owner's Manager as a Team Member (of the Opportunity being inserted), with the role "Sales Manager".
The solution provided by the exercise's author is below:
The question is: since the trigger is an "after" trigger, from what I have learnt regarding "after" trigger, you would need to first INSERT the value contained in the variable opp (insert opp;) in order for Salesforce to generate an Id for that record. So, I would have thought that Line 7 would only work after opp has been inserted in the data base.
But, the above code seems to work fine, that is why I am perplexed and would like to understand how can the system know the opp.Id before the opp record has even been inserted by the apex code.
Thank you very much.
The apex code below is resolving the following requirement (specified as an exercises for practice):
When an opportunity is inserted, automatically add the Oppty's Owner's Manager as a Team Member (of the Opportunity being inserted), with the role "Sales Manager".
The solution provided by the exercise's author is below:
trigger OwnerManager on Opportunity (after insert) { for(Opportunity opp : Trigger.new){ //Get opp owner manager info Opportunity oppWithManagerInfo = [SELECT Id, Owner.ManagerId FROM Opportunity WHERE Id = :opp.Id]; //Create Opportunity Team Role if (oppWithManagerInfo.Owner.ManagerId != null) { OpportunityTeamMember otm = new OpportunityTeamMember(); otm.UserId = oppWithManagerInfo.Owner.ManagerId; otm.OpportunityId = opp.Id; otm.TeamMemberRole = 'Sales Manager'; insert otm; } } }My question is regarding Line 7 --> WHERE Id = :opp.Id
The question is: since the trigger is an "after" trigger, from what I have learnt regarding "after" trigger, you would need to first INSERT the value contained in the variable opp (insert opp;) in order for Salesforce to generate an Id for that record. So, I would have thought that Line 7 would only work after opp has been inserted in the data base.
But, the above code seems to work fine, that is why I am perplexed and would like to understand how can the system know the opp.Id before the opp record has even been inserted by the apex code.
Thank you very much.
Either way, the code is going against two of the typical best practices.
Or if you actually want to get rid of the SOQL Query, since is just for a single field, you could create a formula field and use it inside the trigger, so no query needs to be made.
All Answers
Either way, the code is going against two of the typical best practices.
Or if you actually want to get rid of the SOQL Query, since is just for a single field, you could create a formula field and use it inside the trigger, so no query needs to be made.