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
rumdumdumrumdumdum 

Get record ID when trigger fires!

Hey Guys,

 

Trying  to get my head around apex triggers, would appreciate some help:

 

To create a specific product after opportunity is created, but I can't get the id.

 

My Code:

 

 

trigger AddProduct on Opportunity (after insert) { OpportunityLineItem bLineItem = new OpportunityLineItem(); bLineItem.quantity = 1; bLineItem.UnitPrice = 100.00; bLineItem.Opportunityid = ???????????; bLineItem.pricebookentryid = '01uT0000000vtvnIAA'; Insert bLineItem;}

 

I want to insert the ID from a newly created opportunity.

 

Thanks 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Always ThinkinAlways Thinkin

Hi Rumdumdum,

I think this code may help you get what you're after. It's bulkified to handle batch inserts of Opportunities too. Based on your code, I'm assuming that you want to add a specific product to each Opp after the Opp is created.

 

 

trigger Opportunity on Opportunity (after insert) {
//Create a List to hold Opp Line Items to be inserted
List<OpportunityLineItem> blineItems = new List<OpportunityLineItem>();
//use for loop to iterate through all opportunities in trigger
for(Opportunity o : trigger.new){
//add an Opp Line Item for each Opportunity, using o.Id to relate the OLI to the Opp.
blineItems.add(new OpportunityLineItem(quantity = 1,
UnitPrice = 100.00,
OpportunityId = o.Id,
//not good to use a static ID...but that's a topic for another post
pricebookentryid = '01uT0000000vtvnIAA'));
}
insert blineItems;
}

 

Message Edited by Always Thinkin on 01-26-2010 03:39 PM

All Answers

bob_buzzardbob_buzzard
The newly created opportunity will be in trigger.new (list).  You should be aware that your trigger may get called for >1 record, so you need to ensure you can handle bulk changes if you plan to use it in this way.
Always ThinkinAlways Thinkin

Hi Rumdumdum,

I think this code may help you get what you're after. It's bulkified to handle batch inserts of Opportunities too. Based on your code, I'm assuming that you want to add a specific product to each Opp after the Opp is created.

 

 

trigger Opportunity on Opportunity (after insert) {
//Create a List to hold Opp Line Items to be inserted
List<OpportunityLineItem> blineItems = new List<OpportunityLineItem>();
//use for loop to iterate through all opportunities in trigger
for(Opportunity o : trigger.new){
//add an Opp Line Item for each Opportunity, using o.Id to relate the OLI to the Opp.
blineItems.add(new OpportunityLineItem(quantity = 1,
UnitPrice = 100.00,
OpportunityId = o.Id,
//not good to use a static ID...but that's a topic for another post
pricebookentryid = '01uT0000000vtvnIAA'));
}
insert blineItems;
}

 

Message Edited by Always Thinkin on 01-26-2010 03:39 PM
This was selected as the best answer
rumdumdumrumdumdum

Thank you Guys, it's nice to see that there's help for newcomers here! :)

 

And Luke, much appreciated for your input!

 

Thanks