function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BigSmokeBigSmoke 

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!!

Best Answer chosen by Admin (Salesforce Developers) 
Sonali BhardwajSonali Bhardwaj

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

Sonali BhardwajSonali Bhardwaj

Do you have anything on Opportuntiy_Allocattion__c? Like any trigger/workflow rule, which is updating its Sales_Team_Member__c?

BigSmokeBigSmoke

No, I've got nothing like that.

Sonali BhardwajSonali Bhardwaj

Check your debug logs, where you are printing OA. does that contain that field value? If possible share your debug logs.

BigSmokeBigSmoke

Yea, the debug is what got me to post this.

 

Here's the line from the debug log:

 

 

11:53:43.174 (174332000)|USER_DEBUG|[17]|DEBUG|this is oa:Opportunity_Allocation__c:{Percentage_Of_Revenue__c=100, IsOwner__c=true, Region__c=XXX, Opportunity__c=006L0000001rXEmIAM, STM_txt__c=005E0000000E2UbIAK, Sales_Team_Member__c=005E0000000E2UbIAK}

 

I see it there, but then after the record is inserted, that one value is missing.

 

Sonali BhardwajSonali Bhardwaj

Can you share your complete debug log not just this statment?

BigSmokeBigSmoke

 

21.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,FINEST;VALIDATION,INFO;VISUALFORCE,INFO
11:53:43.066 (66780000)|EXECUTION_STARTED
11:53:43.066 (66875000)|CODE_UNIT_STARTED|[EXTERNAL]|Validation:Opportunity:new
11:53:43.066 (66903000)|VALIDATION_RULE|03dL0000000CbBO|Multiyear_Scheduling_Percentage_is_100
11:53:43.067 (67644000)|VALIDATION_FORMULA|BLANKVALUE(Schedule_Percentage_Year_1__c,0) +
 BLANKVALUE(Schedule_Percentage_Year_2__c,0) +
 BLANKVALUE(Schedule_Percentage_Year_3__c,0) +
 BLANKVALUE(Schedule_Percentage_Year_4__c,0) +
 BLANKVALUE(Schedule_Percentage_Year_5__c,0) != 1|Schedule_Percentage_Year_2__c=null , Schedule_Percentage_Year_1__c=100.0000 , Schedule_Percentage_Year_5__c=null , Schedule_Percentage_Year_4__c=null , Schedule_Percentage_Year_3__c=null
11:53:43.067 (67687000)|VALIDATION_PASS
11:53:43.067 (67706000)|CODE_UNIT_FINISHED|Validation:Opportunity:new
11:53:43.067 (67729000)|EXECUTION_FINISHED
11:53:43.172 (172083000)|EXECUTION_STARTED
11:53:43.172 (172121000)|CODE_UNIT_STARTED|[EXTERNAL]|01qL00000008Onm|insUpdOpportunity on Opportunity trigger event AfterInsert for [006L0000001rXEm]
11:53:43.172 (172185000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
11:53:43.172 (172701000)|STATEMENT_EXECUTE|[1]|TriggerBody: insUpdOpportunity
11:53:43.172 (172765000)|STATEMENT_EXECUTE|[3]|DeclareVar: LIST<Opportunity_Allocation__c> oaList
11:53:43.172 (172793000)|VARIABLE_SCOPE_BEGIN|[3]|oaList|LIST|true
11:53:43.172 (172825000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
11:53:43.172 (172900000)|VARIABLE_ASSIGNMENT|[3]|oaList|[]
11:53:43.172 (172936000)|STATEMENT_EXECUTE|[5]|Condition
11:53:43.172 (172973000)|STATEMENT_EXECUTE|[5]|Block with 2 statements
11:53:43.173 (173013000)|STATEMENT_EXECUTE|[6]|SelectLoop:LIST<Opportunity>
11:53:43.173 (173331000)|VARIABLE_ASSIGNMENT|[6]|o|{"IsWon":false,"Probability":10,"IsSplit":false,"Type":"New Business","OwnerId":"005E0000000E2UbIAK","LastModifiedDate":1307012023000,"LeadSource":"Web Trial","Pricebook2Id":"01sL00000000A3bIAE","IsPrivate":false,"IsClosed":false,"Schedule_Percentage_ (9 more) ...":100.0,"SystemModstamp":1307012023000,"SOW_Estimate_Provide (4 more) ...":false,"ForecastCategoryName":"Pipeline","ForecastCategory":"Pipeline","StageName":"First Appointment","Amount":10000.0,"IsLocked":false,"CurrencyIsoCode":"USD","LastModifiedById":"005E0000000E2UbIAK","GSA_Sale__c":false,"StageSortOrder":1,"ExpectedRevenue":1000.0,"TotalOpportunityQuan (4 more) ...":10.0,"Name":"Test 2 - Policy Mana (3 more) ...","HasOpportunityLineIt (2 more) ...":false,"AccountId":"001L0000001oA4YIAU","Division":"1","CreatedById":"005E0000000E2UbIAK","MayEdit":false,"CreatedDate":1307012023000,"IsDeleted":false,"Id":"006L0000001rXEmIAM","CloseDate":1306713600000,"Billing_Frequency__c":"One Time","Opportunity_Transact (13 more) ...":"OTN000000013"}|0x1d0ee5c
11:53:43.173 (173384000)|STATEMENT_EXECUTE|[6]|Block with 3 statements
11:53:43.173 (173427000)|STATEMENT_EXECUTE|[8]|DeclareVar: SOBJECT:Opportunity_Allocation__c oa
11:53:43.173 (173452000)|VARIABLE_SCOPE_BEGIN|[8]|oa|Opportunity_Allocation__c|true
11:53:43.173 (173599000)|VARIABLE_ASSIGNMENT|[9]|this.Opportunity__c|"006L0000001rXEmIAM"|0x14d59d7
11:53:43.173 (173653000)|VARIABLE_ASSIGNMENT|[10]|this.IsOwner__c|true|0x14d59d7
11:53:43.173 (173701000)|VARIABLE_ASSIGNMENT|[11]|this.Percentage_Of_Revenue__c|100|0x14d59d7
11:53:43.173 (173748000)|VARIABLE_ASSIGNMENT|[12]|this.Region__c|"XXX"|0x14d59d7
11:53:43.173 (173833000)|VARIABLE_ASSIGNMENT|[13]|this.Sales_Team_Member__c|"005E0000000E2UbIAK"|0x14d59d7
11:53:43.173 (173896000)|VARIABLE_ASSIGNMENT|[14]|this.STM_txt__c|"005E0000000E2UbIAK"|0x14d59d7
11:53:43.173 (173930000)|HEAP_ALLOCATE|[8]|Bytes:190
11:53:43.174 (174022000)|VARIABLE_ASSIGNMENT|[8]|oa|{"Percentage_Of_Revenu (4 more) ...":100,"IsOwner__c":true,"Region__c":"XXX","Opportunity__c":"006L0000001rXEmIAM","STM_txt__c":"005E0000000E2UbIAK","Sales_Team_Member__c":"005E0000000E2UbIAK"}|0x14d59d7
11:53:43.174 (174087000)|STATEMENT_EXECUTE|[17]|system.debug(String)
11:53:43.174 (174121000)|METHOD_ENTRY|[17]|System.debug(ANY)
11:53:43.174 (174301000)|HEAP_ALLOCATE|[17]|Bytes:206
11:53:43.174 (174332000)|USER_DEBUG|[17]|DEBUG|this is oa:Opportunity_Allocation__c:{Percentage_Of_Revenue__c=100, IsOwner__c=true, Region__c=XXX, Opportunity__c=006L0000001rXEmIAM, STM_txt__c=005E0000000E2UbIAK, Sales_Team_Member__c=005E0000000E2UbIAK}
11:53:43.174 (174361000)|METHOD_EXIT|[17]|System.debug(ANY)
11:53:43.174 (174406000)|STATEMENT_EXECUTE|[18]|LIST<Opportunity_Allocation__c>.add(SOBJECT:Opportunity_Allocation__c)
11:53:43.174 (174436000)|METHOD_ENTRY|[18]|LIST.add(ANY)
11:53:43.174 (174483000)|HEAP_ALLOCATE|[18]|Bytes:4
11:53:43.174 (174509000)|METHOD_EXIT|[18]|LIST.add(ANY)
11:53:43.174 (174828000)|STATEMENT_EXECUTE|[21]|Insert: LIST<Opportunity_Allocation__c>
11:53:43.174 (174889000)|DML_BEGIN|[21]|Op:Insert|Type:Opportunity_Allocation__c|Rows:1
11:53:43.175 (175008000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
11:53:43.215 (215735000)|DML_END|[21]
11:53:43.215 (215803000)|STATEMENT_EXECUTE|[27]|Condition
11:53:43.215 (215839000)|STATEMENT_EXECUTE|[27]|Condition
11:53:43.215 (215937000)|VARIABLE_SCOPE_END|[EXTERNAL]|o
11:53:43.684|CUMULATIVE_LIMIT_USAGE
11:53:43.684|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 1 out of 10000
  Number of script statements: 6 out of 200000
  Maximum heap size: 0 out of 3000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

11:53:43.684|CUMULATIVE_LIMIT_USAGE_END

11:53:43.215 (215997000)|CODE_UNIT_FINISHED|insUpdOpportunity on Opportunity trigger event AfterInsert for [006L0000001rXEm]
11:53:43.216 (216024000)|EXECUTION_FINISHED

 

 

Sonali BhardwajSonali Bhardwaj

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.

This was selected as the best answer
BigSmokeBigSmoke

The mistake was in the query:

 

problem version:  

 

oaList = [select Id,Opportunity__c, Sales_Team_Member__c 
	from Opportunity_Allocation__c
	where Opportunity__c in :Trigger.newMap.keyset() 
	and IsOwner__c = true]

 

corrected version:

 

 

 

oaList = [select Id,Opportunity__c, Sales_Team_Member__c 
	from Opportunity_Allocation__c
	where Opportunity__c in :oppMap.keyset() 
	and IsOwner__c = true

 

Thanks, Sonali.  I'd have not figured that out forever!!!