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
billgreenhawbillgreenhaw 

Need help to get started - Insert record into custom object based on Opportunity Won

Hi,

 

First time really diving into APEX from scratch. Before I just took other code and did made changes.

 

I am trying to insert a record using a trigger when Opportunity is Won.  Getting an error on linke 13. Just some advice to get me in the right direction would be greatly appreciated.

 

Trigger OpportunityCloseWon_Products on Opportunity (after update) {
	//
	List<License_Overview__c> LicenseOverview = new List<License_Overview__c>();
   	for ( Opportunity o : trigger.New )
   	{
   		if ( o.isWon )
   		{
			OpportunityLineItem[] o3 = [Select o.Licensee__c, o.OpportunityId, o.Opportunity.AccountId from OpportunityLineItem o where OpportunityId = :o.Id and IsDeleted != TRUE limit 1];
				if (o3.size() > 0) {
					//if ( Trigger.isUpdate )
					//Remember to set Status = Active
					License_Overview__c lo = new License_Overview__c (
						Licensee__c = o3.Licensee__c,
						Opportunity__c = o3.OpportunityId,
						Status__c = 'Active',
						Account__c = o3.AccountId
						);
					LicenseOverview.add(lo);
				}
			insert LicenseOverview;
   		}
   	}
}

 

Best Answer chosen by Admin (Salesforce Developers) 
billgreenhawbillgreenhaw

Figured it out myself.

 

I had two issues.

 

1.  Needed to have [0] so I only got the first record ... o3[0].OpportunityId

 

2.  I didn't have the field names correct.  o3.AccountId needed to be o3[0].Opportunity.AccountId, so I get the child object reference.