• Jyoti Bhosale
  • NEWBIE
  • 0 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 2
    Replies
public class ProductHandllerOnOpportunity {
    public static void CheckproductandPricebook(List<Opportunity> opportunities){
        for(Opportunity opp : opportunities){
            if(opp.Add_Products__c==null || opp.Pricebook__c==null){
                opp.addError('Please fill The Product and Pricebook field');
            }
            
        }
    }
    
    public static void CreateProductMethod(List<Opportunity> opportunities){

        List<Pricebook2> priceBook = new List<Pricebook2>();
           List<OpportunityLineItem> productList = new List<OpportunityLineItem>();
        for (Opportunity opp : opportunities) {
        OpportunityLineItem product = new OpportunityLineItem(
        OpportunityId= opp.Id,
        Quantity=6,
        TotalPrice=6000,
        PricebookEntryId=opp.Pricebook2Id);
            
        productList.add(product);
            
        Pricebook2 pb = new Pricebook2(Id=opp.Pricebook2Id);
        priceBook.add(pb);
            }
    insert productList;
}
      
 }
why i am gtting this error
error=redirect_uri_mismatch&error_description=redirect_uri%20must%20match%20configuration
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
SFDCIronMan