You need to sign in to do that
Don't have an account?
Trigger (after insert): having trouble assigning OwnerId value
Hi,
I've got a really simple trigger here that's giving me a little grief. I've got child records off Opportunities called "Opportunity Allocations". When an opportunity is created, we assume the allocation to the owner is at 100%. Later, we might allocate other percentages to the owner, plus members of the sales team.
After insert on Opportunity, I create a row in Opportunity_Allocation__c, supplying OpportunityId and Opportunity.OwnerId. For some reason I can't figure out, the Opportunity.OwnerId field won't populate on the child record.
The child record field is called "Sales_Team_Member__c and I've set it up as a lookup to User. So I should be able to simply populate OwnerId into it or any other primary key for any other record in User object. But it comes up null when the trigger fires.
So I decided to add a text field in called "STM" and I also put Opportunity.OwnerId in that field. That works.
Here's the code. The only bit that doesn't work properly is the beginning, inside the first if. The rest of the trigger works as it should. The rest of the trigger reassigns the owner's share of allocation every time the owner of the opportunity works.
I expected to have more trouble with the latter half of the code but that works. The trouble's in the first 18 or so lines.
trigger insUpdOpportunity on Opportunity (after insert, after update) { List<Opportunity_Allocation__c> oaList = new List<Opportunity_Allocation__c>(); if(Trigger.isInsert) { for (Opportunity o : trigger.new) { Opportunity_Allocation__c oa = new Opportunity_Allocation__c ( Opportunity__c = o.Id, IsOwner__c = true, Percentage_Of_Revenue__c = 100, Region__c = 'XXX', Sales_Team_Member__c = o.OwnerId, STM_txt__c = o.OwnerId ); system.debug('this is oa:' + oa); oaList.add(oa); } insert oaList; } // end if if(Trigger.isUpdate) { Opportunity[] oldOpp = trigger.old; Opportunity[] newOpp = trigger.new; // build the map of opp to new opp owner map<Id, Id> oppMap = new map<Id, Id>(); for (Integer i = 0; i < newOpp.size(); i++ ) { if (oldOpp[i].OwnerId != newOpp[i].OwnerId) { oppMap.put(newOpp[i].Id, newOpp[i].OwnerId) ; } } // build the list of opportunity allocations to modify oaList = [select Id,Opportunity__c, Sales_Team_Member__c from Opportunity_Allocation__c where Opportunity__c in :Trigger.newMap.keyset() and IsOwner__c = true]; // loop over the list and change the Sales_Team_Member for (Opportunity_Allocation__c oa : oaList) { oa.Sales_Team_Member__c = oppMap.get(oa.Opportunity__c); } //upsert the changed values (if any) if (oaList.size() > 0) upsert oaList; } // end if }
What am I doing wrong?
Thanks!!
What I can interpret is after you insert an opportunity an after insert trigger gets fire, but I think you have some Roll Up summary or something which again fires after update for the same record and assign oa.Sales_Team_Member__c to null at line
oa.Sales_Team_Member__c = oppMap.get(oa.Opportunity__c);
this statement actually returns null due to your given conditions. Try commenting this line and see your thing will work.
All Answers
Do you have anything on Opportuntiy_Allocattion__c? Like any trigger/workflow rule, which is updating its Sales_Team_Member__c?
No, I've got nothing like that.
Check your debug logs, where you are printing OA. does that contain that field value? If possible share your debug logs.
Yea, the debug is what got me to post this.
Here's the line from the debug log:
I see it there, but then after the record is inserted, that one value is missing.
Can you share your complete debug log not just this statment?
What I can interpret is after you insert an opportunity an after insert trigger gets fire, but I think you have some Roll Up summary or something which again fires after update for the same record and assign oa.Sales_Team_Member__c to null at line
oa.Sales_Team_Member__c = oppMap.get(oa.Opportunity__c);
this statement actually returns null due to your given conditions. Try commenting this line and see your thing will work.
The mistake was in the query:
problem version:
corrected version:
Thanks, Sonali. I'd have not figured that out forever!!!