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
travis.truetttravis.truett 

Pull Data from Opp Product in Opportunity Trigger

I'm working with a trigger I wrote a few months back, but we want to change the way a particular line works. Currently, the trigger duplicates opportunities when they close and creates a followup opportunity for the following year. We want to change our naming convention for the new opportunities. Currently, this is how they're named:

oppNew.Name = oppNew.Name.substringBefore('-')  + ' - Annual ' + o.CloseDate.year();

Instead of this, we want to pull some data from the opportunity product attached to the opportunity. Specifically, the number of users, the duration of the product, and the price per user for the product. So here's what the change would look like:

"CompanyName - Annual 2015" ---> "CompanyName 15.3.30"
Where 15 is the number of users, 3 is the duration in months, and 30 is the price per seat.
All of that information is available in the opportunity product object, I just forgot how to query for it because I haven't worked with apex in half a year.

Thanks
James LoghryJames Loghry
Hi Travis,

The relationship you're looking for is OpportunityLineItems.

Salesforce uses Opportunity Products and Opportunity Line Items interchangeably 

In other words, you would have some SOQL query similar to below, in a before or after update trigger:
 
List<Opportunity> ops = [Select Id,(Select Id From OpportunityLineItems) From Opportunity];