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
NBP1411NBP1411 

Apex Code Question

Hi, I am writing an Apex Trigger for product X to be added to all Opportuntities containing products Y and Z. My current trigger coding is is not getting the result I was hoping for. Can anyone help me out here on what I need to do to have it look for products Y and Z in my opportunities? Thank you for your help!

trigger CreateOLI on Opportunity (after insert) {
    List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();

    List<PriceBookEntry> priceBookList = [SELECT Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry WHERE Product2Id='01t1a000000Cwc3' AND PriceBook2.isStandard=true LIMIT 1];
 
    
    for (Opportunity oppty: Trigger.new) {
        if (OLI.Name=Z) (OLI.Name=Y)
            OLI.Name = (Product2.Name)
            //create new Oli
            OpportunityLineItem oli = new OpportunityLineItem(OpportunityId=oppty.Id, PricebookEntryId=priceBookList[0].Id /*rest of required fields*/);
            oliList.add(oli);
        }
    }

    insert oliList;
}
Best Answer chosen by NBP1411
Neetu_BansalNeetu_Bansal
Hi,

Try using this trigger:
trigger OpportunityTrg on Opportunity( after insert )
{
	List<Opportunity> opps = [ Select Id, Name, ( Select Id, Product2.Name from OpportunityLineItems where Product2.Name = 'Y' OR Product2.Name = 'Z') from Opportunity where Id IN: trigger.new ];
	
	List<PriceBookEntry> priceBookList = [SELECT Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry WHERE Product2Id='01t1a000000Cwc3' AND PriceBook2.isStandard=true LIMIT 1];
	
	List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
	for( Opportunity opp : opps )
	{
		Boolean flagY = false;
		Boolean flagZ = false;
		for( OpportunityLineItem line : opp.OpportunityLineItems )
		{
			if( flagY && flagZ )
				break;
			
			if( line.Product2.Name == 'Y' )
				flagY = true;
			
			if( line.Product2.Name == 'Z' )
				flagZ = true;
		}
		
		if( flagY && flagZ )
		{
			//create new Oli
            OpportunityLineItem oli = new OpportunityLineItem( OpportunityId=opp.Id, PricebookEntryId = priceBookList[0].Id /*rest of required fields*/);
            oliList.add( oli );
		}
	}
	
	if( oliList.size() > 0 )
		insert oliList;
}
Let me know, if you need any other help.

Thanks,
Neetu

All Answers

Neetu_BansalNeetu_Bansal
Hi,

Try using this trigger:
trigger OpportunityTrg on Opportunity( after insert )
{
	List<Opportunity> opps = [ Select Id, Name, ( Select Id, Product2.Name from OpportunityLineItems where Product2.Name = 'Y' OR Product2.Name = 'Z') from Opportunity where Id IN: trigger.new ];
	
	List<PriceBookEntry> priceBookList = [SELECT Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry WHERE Product2Id='01t1a000000Cwc3' AND PriceBook2.isStandard=true LIMIT 1];
	
	List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
	for( Opportunity opp : opps )
	{
		Boolean flagY = false;
		Boolean flagZ = false;
		for( OpportunityLineItem line : opp.OpportunityLineItems )
		{
			if( flagY && flagZ )
				break;
			
			if( line.Product2.Name == 'Y' )
				flagY = true;
			
			if( line.Product2.Name == 'Z' )
				flagZ = true;
		}
		
		if( flagY && flagZ )
		{
			//create new Oli
            OpportunityLineItem oli = new OpportunityLineItem( OpportunityId=opp.Id, PricebookEntryId = priceBookList[0].Id /*rest of required fields*/);
            oliList.add( oli );
		}
	}
	
	if( oliList.size() > 0 )
		insert oliList;
}
Let me know, if you need any other help.

Thanks,
Neetu
This was selected as the best answer
NBP1411NBP1411
Thank you so much, Neetu. I tried running it after the checkbox for Active was clicked. Unfortunately Product X did not auto-populate when I added products Y and Z to a new opportunity. Am I missing anything here?
Neetu_BansalNeetu_Bansal
Hi Tom,

This seems that you need to write trigger on Opportunity Line Items. I can help you on that too.
Can you please contact me either on my gmail id: neetu.bansal.5@gmail.com or Skype id: neetu.bansal.5

Thanks,
Neetu