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 

After Opportunity update need to populate two custom objects

Thanks for any help and feel free to rip my code apart, as I am sure I not done it correctly so far.

When an Opportunity is updated I need to first create a record in License_Overview__c. object This will also be a single record. I am sure I am doing way too much code at this level to get the Opportunity values.

 

After License_Overview__c record is created I need to create record(s) into the License_Detail__c object.  Values are based on the OpportunityLineItem object for the Opportunity.  One or many records can be returned.  I need to insert the LicenceOverview__c ID into this object, since they are Parent-Child relationship (Overview is the parent to Detail).

 

The 2nd part is where I am failing.  I cannot see how to get the inserted ID so I can use it.  Probably really simple and I am just missing it.

 

Trigger OpportunityCloseWon_Products on Opportunity (after insert, after update) {
	List<License_Overview__c> LicenseOverview = new List<License_Overview__c>();
	List<License_Detail__c> LicenseDetail = new List<License_Detail__c>();
   	for ( Opportunity o : trigger.New )
   	{
   		if ( o.isWon ) {
   			License_Overview__c[] lic1 = [Select l.Id  from License_Overview__c l where l.Opportunity__c = :o.Id and IsDeleted != TRUE];
   			if (lic1.size() < 1 ) {
		   		{
					Opportunity[] o3 = [Select o.Licensee__c, o.Id, o.AccountId,
												o.End_Date__c from Opportunity o 
												where Id = :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[0].Licensee__c,
								Opportunity__c = o3[0].Id,
								Status__c = 'Active',
								Account__c = o3[0].AccountId,
								End_Date__c = o3[0].End_Date__c
								);
							LicenseOverview.add(lo);
						}
					insert LicenseOverview;
		   		}
		   		{
		   			OpportunityLineItem[] o4 = [Select o.Description, o.OpportunityId, o.Opportunity.AccountId, 
									   			o.OS_del__c, o.PricebookEntryId, o.PricebookEntry.Name, o.Quantity, o.Version_del__c 
									   			from OpportunityLineItem o where o.OpportunityId = :o.Id and o.IsDeleted != TRUE];
		   				if ( o4.size() > 0 ) {
							for (Integer i = 0; i < o4.size(); i++) {
								License_Detail__c ld = new License_Detail__c (
									Description__c = o4[i].Description,
									License_Overview__c = LicenseOverview.get(0).id,
									Status__c = 'Active',
									Account__c = o4[i].Opportunity.AccountId,
									Product__c = o4[i].PricebookEntry.Name,
									Users__c = o4[i].Quantity,
									System_Code__c = o4[i].OS_del__c,
									Version__c =  o4[i].Version_del__c
									);
								LicenseDetail.add(ld);
		   					}
						}
					insert LicenseDetail;
		   		}
   			}
   		}
   	}
}

 

VPrakashVPrakash

I think You might need to use two triggers - one opportunity insert or update , one on the License_Overview__c insert or update. So that you can get the id of custom object.