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
SFDCDoctorStrangeSFDCDoctorStrange 

ABC Firm needs to auto-create Opportunity Products in certain use cases.

  • Create a custom checkbox field on Opportunity and name it ‘Add Products?’
  • Create a multi-select picklist field on Opportunity that shows list of products one can auto-select.
  • Create a picklist field on Opportunity as Pricebook.
  • Each time an opportunity record is saved with Ádd Products’ selected as true following should happen
    • Throw validation error if pricebook and products are not selected.
    • Once opportunity is saved auto insert opportunity products on basis of selection made on pricebook and products.
  • Code should be able to handle bulk data; meaning multiple opportunity records can be inserted together using Import Wizard or Data loader
  • Code should adhere to programming best practices like
  • Standardized Naming conventions
  • Apt utilization of collection elements
  • Follow of Trigger pattern
  • Follow Trigger deactivation mechanism
  • Avoiding Trigger recurrence on same object
  • Proper Code comments
  • Keeping Salesforce Governor limits into consideration
  • Create test class to ensure developed code can be deployed; adhere to best practices of testing framework
//this is the code Ive made
public class OpportunityProductAndPriceBook_Handler 
{
    public static void AfterInsertOpportunity(List<Opportunity> records)
    {
        List<OpportunityLineItem> lineItem = new List<OpportunityLineItem>();
        
        List<Pricebook2> priceBook = new List<Pricebook2>();
        
        if(RecursiveTriggerHandler.isFirstTime)
        {
            RecursiveTriggerHandler.isFirstTime = false;
            for(Opportunity opp : records)
            {
                if(opp.Add_Products__c)
                {
                    OpportunityLineItem oli = new OpportunityLineItem();
                    oli.OpportunityId = opp.id;
                    oli.Product2Id = opp.Select_Product__c;
                    oli.Quantity = opp.Product_Quantity__c;
                    lineItem.add(oli);
                    
                    Pricebook2 pb = new Pricebook2();
                    pb.Id = opp.Pricebook2Id;
                    pb.Name = opp.Price_Book__c;
                    priceBook.add(pb);
                }
            }
            if(!lineItem.IsEmpty() && !priceBook.IsEmpty())
            {
                insert lineItem;
                insert priceBook;
            }
        }
    }
}


and this the error im getting from the code please help me 

OpportunityProductAndPriceBook_Trigger: execution of AfterInsert caused by: System.StringException: Invalid id: GenWatt Diesel 1000kW Class.OpportunityProductAndPriceBook_Handler.AfterInsertOpportunity: line 18, column 1 Trigger.OpportunityProductAndPriceBook_Trigger: line 5, column 1


Regards
SFDCDoctorStrange