• Filipe Baradelli
  • NEWBIE
  • 60 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 2
    Likes Received
  • 0
    Likes Given
  • 18
    Questions
  • 41
    Replies
Hi,

I'' m trying to put the value of Quantity from Catalog_Line_Item__c to 'Quantity' from Opportunity.
 
/* 
     *  updateQuantity(String ID) 
     *  Updates the Quantity field of the Catalog Line Item with the given Id 
     */
    public PageReference updateQuantity() {
        if(!Catalog_Line_Item__c.sObjectType.getDescribe().isUpdateable()) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'Insufficient access'));
            return null;
        }
        
        li = [SELECT Quantity__c FROM Catalog_Line_Item__c WHERE Id =: productId LIMIT 1];
        li.Quantity__c = productQuantity;
        update li;
        update myOrder;      
        quantity = li.Quantity__c;
        
        // Stay on current page
        return null;
    }
(variable 'quantity' is double)
 
// Add product and Pricebook to the particular opportunity using OpportunityLineItem 
        OpportunityLineItem oppLineItem = new OpportunityLineItem();
        oppLineItem.OpportunityId = opp.Id;
        oppLineItem.PricebookEntryId = customPriceBookEntry.Id;
        oppLineItem.UnitPrice = customPriceBookEntry.UnitPrice;
        oppLineItem.Quantity = quantity ;
        insert oppLineItem;

This is the error message. that appeats when I Execute the Catalog:
Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING: [Quantity]: [Quantity].

Can someone help me?
Thanks.
Hi.

I'm trying to create a button add a Catalog Price when I'm in the page to add a price to the product that I've just created.
Is this possible?

Thanks.
public pageReference addToCart() {
        if(!Catalog_Order__c.sObjectType.getDescribe().isUpdateable() || !Catalog_Line_Item__c.sObjectType.getDescribe().isCreateable()) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'Insufficient access'));
            return null;
        }
        if( productId == null ) {} else {
        // First make sure that our Order is in the database
        upsert myOrder;
        orderId = myOrder.Id;

        produto = productId;
        // Check if the product is already in the Cart
        if( productSet.contains( productId ) ) { } else {
            // Here is where we add the ID to the set...
            productSet.add( productId );
            // ...and to the Order.
            Catalog_Line_Item__c li =  new Catalog_Line_item__c();
            li.Product__c = productId;
            produto = productId;
            li.Catalog_Order__c = myOrder.Id;
            li.Quantity__c = 1;
            insert li;        
            
            // Added to Cart
            }
        }
        // Clear the parameter and reupsert
        productId = null;
        upsert myOrder;
        return null;
    }
 public PageReference toCart() {

        integer stdPriceBookRecId2; 
        List<Pricebook2> stdPriceBookRecId = new List<Pricebook2>();
        Catalog_Order__c[] orderQuery = [SELECT Items__c,Total_Cost__c,Account__c,Id,(SELECT Id,Product__c,Product_Short_Description__c,Product_Price__c,Product_Name__c,Quantity__c,VF_Image__c FROM Catalog_Line_Items__r) FROM Catalog_Order__c WHERE Status__c = 'Cart' AND CreatedById =:UserInfo.getUserId() ORDER BY LastModifiedDate DESC NULLS FIRST LIMIT 1];

        // Insert Pricebook
        PriceBook2 customPriceBook = new PriceBook2();
        customPriceBook.Name='Custom Pricebook';
        customPriceBook.IsActive=true;
        insert customPriceBook;
        
        // Query Standard and Custom Price Books
        Pricebook2 customPriceBookRec=[select Id from Pricebook2 where id=:customPriceBook.Id];
        stdPriceBookRecId = [SELECT Id, isstandard FROM Pricebook2 WHERE IsStandard=true];
        
        // Create Standard PriceBookEntry
        PriceBookEntry stdPriceBookEntry = new PriceBookEntry();
        stdPriceBookEntry.Product2Id=produto;
        stdPriceBookEntry.Pricebook2Id=stdPriceBookRecId[0].Id;
        stdPriceBookEntry.UnitPrice=2001;
        stdPriceBookEntry.IsActive=true;
        insert stdPriceBookEntry;
        
        // Create Custom PriceBookEntry
        PriceBookEntry customPriceBookEntry = new PriceBookEntry();     
        customPriceBookEntry.Product2Id=produto;
        customPriceBookEntry.Pricebook2Id=customPriceBookRec.Id;
        customPriceBookEntry.UnitPrice=5001;
        customPriceBookEntry.IsActive=true;
        insert customPriceBookEntry;
        
        // Create Opportunity
        Opportunity opp = new Opportunity();
        opp.Name = 'OPORTUNIDADE TESTE';
        opp.CloseDate= System.Today();
        opp.StageName='Prospecting';
        insert opp;
        
        // Add product and Pricebook to the particular opportunity using OpportunityLineItem 
        OpportunityLineItem oppLineItem = new OpportunityLineItem();
        oppLineItem.OpportunityId = opp.Id;
        oppLineItem.PricebookEntryId = customPriceBookEntry.Id;
        oppLineItem.UnitPrice = 7001;
        oppLineItem.Quantity = 4;
        insert oppLineItem;  
        
        
        if(!Catalog_Order__c.sObjectType.getDescribe().isAccessible()) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'Insufficient access'));
            return null;
        }
        try{
            myOrder = [SELECT Items__c,Lines__c,Total_Cost__c,Account__c,Id,(SELECT Id,Product__c,Product_Short_Description__c,Product_Price__c,Product_Name__c,Price__c,Quantity__c,Subtotal__c,VF_Image__c FROM Catalog_Line_Items__r) FROM Catalog_Order__c WHERE Id =: orderId LIMIT 1];
        } catch (exception e) {System.debug(e.getMessage());}

        PageReference next = new PageReference('/apex/catalog_cart');
        next.setRedirect(false);
        // Directing to Cart
        return next;
    }
This is the code.

These are pieces of Catalog_Controller Class that I'm modifying to add the products of the Catalog Order to an Opportunity that's created when the catalog is submited. The error : 
' Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, This price definition already exists in this price book: [] '
appers.

Can someone help me?

Thanks.
Hello.

I'm trying to create a filter to add a Google Doc into the report.

How could I do this?
Hello.

I'm trying to create a filter in reports to show a relactive Google Doc.
How can I do this?
Catalog_Controller : Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, product ID: Type of value of ID incorrect: a0B36000002nFaZEAU: [Product2Id]

I'm trying to generate an Opportunity when a Catalog Order is submited. This Opportunity must have the product that's in the order. I was creating a product each time I create a Opportunity, but I can't create the product again, but I need to get the product data and place it in the Opportunity products.

Here is what I did:
public List<Catalog_Order__c> listaproduto {get;set;} 
// public Product2 produto2 = new Product2(); 
public String nomeproduto; 
public String codproduto; 
public ID idproduto; 
public String descproduto; 
public datetime createddateproduto; 
public String createdbyidproduto; 
public String familyproduto;
I've created variables to get the product data.
listaproduto=[SELECT Items__c,Total_Cost__c,Account__c,Id,(SELECT Id,Product__c,Product_Short_Description__c,Product_Price__c,Product_Name__c,Quantity__c,VF_Image__c FROM Catalog_Line_Items__r) FROM Catalog_Order__c WHERE Status__c = 'Cart' AND CreatedById =:UserInfo.getUserId() ORDER BY LastModifiedDate DESC NULLS FIRST LIMIT 1]; 
    for(Catalog_Order__c ctlgOrdr : listaproduto){ 
        for(Catalog_Line_Item__c ctlgLineItem : ctlgOrdr.Catalog_Line_Items__r){ 
        System.debug(ctlgLineItem.Product__c); 
        ctlgLineItem.Product__c = ctlgLineItem.Id; 
        idproduto = ctlgLineItem.Id; 
        nomeproduto = ctlgLineItem.Product_Name__c; 
        descproduto = ctlgLineItem.Product_Short_Description__c; 
        codproduto = ctlgLineItem.Product__c;
     // produto2.isActive = true; 
        }
    }
Here I tried to pass the product data to the variables.
integer stdPriceBookRecId2; 
        List<Pricebook2> stdPriceBookRecId = new List<Pricebook2>();
        Catalog_Order__c[] orderQuery = [SELECT Items__c,Total_Cost__c,Account__c,Id,(SELECT Id,Product__c,Product_Short_Description__c,Product_Price__c,Product_Name__c,Quantity__c,VF_Image__c FROM Catalog_Line_Items__r) FROM Catalog_Order__c WHERE Status__c = 'Cart' AND CreatedById =:UserInfo.getUserId() ORDER BY LastModifiedDate DESC NULLS FIRST LIMIT 1];

        // Insert Pricebook
        PriceBook2 customPriceBook = new PriceBook2();
        customPriceBook.Name='Custom Pricebook';
        customPriceBook.IsActive=true;
        insert customPriceBook;
        
        // Query Standard and Custom Price Books
        Pricebook2 customPriceBookRec=[select Id from Pricebook2 where id=:customPriceBook.Id];
        stdPriceBookRecId = [SELECT Id, isstandard FROM Pricebook2 WHERE IsStandard=true];
        
        // Create Standard PriceBookEntry
        PriceBookEntry stdPriceBookEntry = new PriceBookEntry();
        stdPriceBookEntry.Product2Id=idproduto;
        stdPriceBookEntry.Pricebook2Id=stdPriceBookRecId[0].Id;
        stdPriceBookEntry.UnitPrice=2000;
        stdPriceBookEntry.IsActive=true;
        insert stdPriceBookEntry;
        
        // Create Custom PriceBookEntry
        PriceBookEntry customPriceBookEntry = new PriceBookEntry();
        customPriceBookEntry.Product2Id=idproduto;
        customPriceBookEntry.Pricebook2Id=customPriceBookRec.Id;
        customPriceBookEntry.UnitPrice=5000;
        customPriceBookEntry.IsActive=true;
        insert customPriceBookEntry;
        
        // Create Opportunity
        Opportunity opp = new Opportunity();
        opp.Name = 'OPORTUNIDADE TESTE';
        opp.CloseDate= System.Today();
        opp.StageName='Prospecting';
        insert opp;
        
        // Add product and Pricebook to the particular opportunity using OpportunityLineItem 
        OpportunityLineItem oppLineItem = new OpportunityLineItem();
        oppLineItem.OpportunityId = opp.Id;
        oppLineItem.PricebookEntryId = customPriceBookEntry.Id;
        oppLineItem.UnitPrice = 7000;
        oppLineItem.Quantity = 5;
        insert oppLineItem;
Here I tried to create the Opportunity and associate it with the product and pricebooks.

The error points to the line :" insert stdPriceBookEntry;"
I've tried to put the ID 'a0B36000002nFaZEAU' instead the variable, but it doesn't work too.

Can someone help me?
Catalog_Controller : Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, product ID: Type of value of ID incorrect: a0B36000002nFaZEAU: [Product2Id]

I'm trying to generate an Opportunity when a Catalog Order is submited. This Opportunity must have the product that's in the order. I was creating a product each time I create a Opportunity, but I can't create the product again, but I need to get the product data and place it in the Opportunity products.

Here is what I did:
public List<Catalog_Order__c> listaproduto {get;set;} 
// public Product2 produto2 = new Product2(); 
public String nomeproduto; 
public String codproduto; 
public ID idproduto; 
public String descproduto; 
public datetime createddateproduto; 
public String createdbyidproduto; 
public String familyproduto;
I've created variables to get the product data.
 
listaproduto=[SELECT Items__c,Total_Cost__c,Account__c,Id,(SELECT Id,Product__c,Product_Short_Description__c,Product_Price__c,Product_Name__c,Quantity__c,VF_Image__c FROM Catalog_Line_Items__r) FROM Catalog_Order__c WHERE Status__c = 'Cart' AND CreatedById =:UserInfo.getUserId() ORDER BY LastModifiedDate DESC NULLS FIRST LIMIT 1]; 
    for(Catalog_Order__c ctlgOrdr : listaproduto){ 
        for(Catalog_Line_Item__c ctlgLineItem : ctlgOrdr.Catalog_Line_Items__r){ 
        System.debug(ctlgLineItem.Product__c); 
        ctlgLineItem.Product__c = ctlgLineItem.Id; 
        idproduto = ctlgLineItem.Id; 
        nomeproduto = ctlgLineItem.Product_Name__c; 
        descproduto = ctlgLineItem.Product_Short_Description__c; 
        codproduto = ctlgLineItem.Product__c;
     // produto2.isActive = true; 
        }
    }
Here I tried to pass the product data to the variables.
 
integer stdPriceBookRecId2; 
        List<Pricebook2> stdPriceBookRecId = new List<Pricebook2>();
        Catalog_Order__c[] orderQuery = [SELECT Items__c,Total_Cost__c,Account__c,Id,(SELECT Id,Product__c,Product_Short_Description__c,Product_Price__c,Product_Name__c,Quantity__c,VF_Image__c FROM Catalog_Line_Items__r) FROM Catalog_Order__c WHERE Status__c = 'Cart' AND CreatedById =:UserInfo.getUserId() ORDER BY LastModifiedDate DESC NULLS FIRST LIMIT 1];

        // Insert Pricebook
        PriceBook2 customPriceBook = new PriceBook2();
        customPriceBook.Name='Custom Pricebook';
        customPriceBook.IsActive=true;
        insert customPriceBook;
        
        // Query Standard and Custom Price Books
        Pricebook2 customPriceBookRec=[select Id from Pricebook2 where id=:customPriceBook.Id];
        stdPriceBookRecId = [SELECT Id, isstandard FROM Pricebook2 WHERE IsStandard=true];
        
        // Create Standard PriceBookEntry
        PriceBookEntry stdPriceBookEntry = new PriceBookEntry();
        stdPriceBookEntry.Product2Id=idproduto;
        stdPriceBookEntry.Pricebook2Id=stdPriceBookRecId[0].Id;
        stdPriceBookEntry.UnitPrice=2000;
        stdPriceBookEntry.IsActive=true;
        insert stdPriceBookEntry;
        
        // Create Custom PriceBookEntry
        PriceBookEntry customPriceBookEntry = new PriceBookEntry();
        customPriceBookEntry.Product2Id=idproduto;
        customPriceBookEntry.Pricebook2Id=customPriceBookRec.Id;
        customPriceBookEntry.UnitPrice=5000;
        customPriceBookEntry.IsActive=true;
        insert customPriceBookEntry;
        
        // Create Opportunity
        Opportunity opp = new Opportunity();
        opp.Name = 'OPORTUNIDADE TESTE';
        opp.CloseDate= System.Today();
        opp.StageName='Prospecting';
        insert opp;
        
        // Add product and Pricebook to the particular opportunity using OpportunityLineItem 
        OpportunityLineItem oppLineItem = new OpportunityLineItem();
        oppLineItem.OpportunityId = opp.Id;
        oppLineItem.PricebookEntryId = customPriceBookEntry.Id;
        oppLineItem.UnitPrice = 7000;
        oppLineItem.Quantity = 5;
        insert oppLineItem;
Here I tried to create the Opportunity and associate it with the product and pricebooks.

The error points to the line :" insert stdPriceBookEntry;"

Can someone help me?

 
The message: "Something has gone wrong. Cannot read property 'ACTION' of null. Please try again." apears when I try to start another discussion here.
What is the problem ?
This error appeared and point to the line 200. I don't know why the id value is not being valid for the object.
 
listaproduto=[SELECT Items__c,Total_Cost__c,Account__c,Id,(SELECT Id,Product__c,Product_Short_Description__c,Product_Price__c,Product_Name__c,Quantity__c,VF_Image__c FROM Catalog_Line_Items__r) FROM Catalog_Order__c WHERE Status__c = 'Cart' AND CreatedById =:UserInfo.getUserId() ORDER BY LastModifiedDate DESC NULLS FIRST LIMIT 1];
        
        for(Catalog_Order__c ctlgOrdr : listaproduto){
            for(Catalog_Line_Item__c ctlgLineItem : ctlgOrdr.Catalog_Line_Items__r){
                System.debug(ctlgLineItem.Product__c);       
                
                produto2.Id = ctlgLineItem.Id;  // LINE 200
                produto2.Name = ctlgLineItem.Product_Name__c;
                produto2.Description = ctlgLineItem.Product_Short_Description__c;
                produto2.isActive = true;
            }
        }

 
I need to get the name, description, quantity .... of this product. But it is inside a subquery.
nomeproduto=[SELECT Items__c,Total_Cost__c,Account__c,Id,(SELECT Id,Product__c,Product_Short_Description__c,Product_Price__c,Product_Name__c,Quantity__c,VF_Image__c FROM Catalog_Line_Items__r) FROM Catalog_Order__c WHERE Status__c = 'Cart' AND CreatedById =:UserInfo.getUserId() ORDER BY LastModifiedDate DESC NULLS FIRST LIMIT 1];
Can someone help me?
 
How could I insert a product in Opportunity by the Apex (the problem is because the sentence is for tests, so how I do this?).
 
public class Teste {

    public void teste1(){
        // Insert Product
        Product2 pr = new Product2();
        pr.Name='Moto - G1';
        pr.isActive=true;
        insert pr;
        
        // Insert Pricebook
        PriceBook2 customPriceBook = new PriceBook2();
        customPriceBook.Name='Custom Pricebook';
        customPriceBook.IsActive=true;
        insert customPriceBook;
        
        // Query Standard and Custom Price Books
        Pricebook2 customPriceBookRec=[select Id from Pricebook2 where id=:customPriceBook.Id];
        Id stdPriceBookRecId = Test.getStandardPricebookId();
        
        // Create Standard PriceBookEntry
        PriceBookEntry stdPriceBookEntry = new PriceBookEntry();
        stdPriceBookEntry.Product2Id=pr.Id;
        stdPriceBookEntry.Pricebook2Id=stdPriceBookRecId;
        stdPriceBookEntry.UnitPrice=2000;
        stdPriceBookEntry.IsActive=true;
        insert stdPriceBookEntry;
        // Create Custom PriceBookEntry
        PriceBookEntry customPriceBookEntry = new PriceBookEntry();
        customPriceBookEntry.Product2Id=pr.Id;
        customPriceBookEntry.Pricebook2Id=customPriceBookRec.Id;
        customPriceBookEntry.UnitPrice=5000;
        customPriceBookEntry.IsActive=true;
        insert customPriceBookEntry;
        
        // Create Opportunity
        Opportunity opp = new Opportunity();
        opp.Name = 'Test';
        opp.CloseDate= System.Today();
        opp.StageName='Prospecting';
        insert opp;
        
        // Add product and Pricebook to the particular opportunity using OpportunityLineItem 
        OpportunityLineItem oppLineItem = new OpportunityLineItem();
        oppLineItem.OpportunityId = opp.Id;
        oppLineItem.PricebookEntryId = customPriceBookEntry.Id;
        oppLineItem.UnitPrice = 7000;
        oppLineItem.Quantity = 5;
        insert oppLineItem;
    }
}

Please, help me.
I've created an Apex class to do a test and I need to execute it. I would like to know how to execute it with a button for example (I've created a button that is related with a Visualforce page).
Here is the code that I want to test: 
public class Teste {

    public void teste1(){
        // Insert Product
        Product2 pr = new Product2();
        pr.Name='Moto - G1';
        pr.isActive=true;
        insert pr;
        
        // Insert Pricebook
        PriceBook2 customPriceBook = new PriceBook2();
        customPriceBook.Name='Custom Pricebook';
        customPriceBook.IsActive=true;
        insert customPriceBook;
        
        // Query Standard and Custom Price Books
        Pricebook2 customPriceBookRec=[select Id from Pricebook2 where id=:customPriceBook.Id];
        Id stdPriceBookRecId = Test.getStandardPricebookId();
        
        // Create Standard PriceBookEntry
        PriceBookEntry stdPriceBookEntry = new PriceBookEntry();
        stdPriceBookEntry.Product2Id=pr.Id;
        stdPriceBookEntry.Pricebook2Id=stdPriceBookRecId;
        stdPriceBookEntry.UnitPrice=2000;
        stdPriceBookEntry.IsActive=true;
        insert stdPriceBookEntry;
        // Create Custom PriceBookEntry
        PriceBookEntry customPriceBookEntry = new PriceBookEntry();
        customPriceBookEntry.Product2Id=pr.Id;
        customPriceBookEntry.Pricebook2Id=customPriceBookRec.Id;
        customPriceBookEntry.UnitPrice=5000;
        customPriceBookEntry.IsActive=true;
        insert customPriceBookEntry;
        
        // Create Opportunity
        Opportunity opp = new Opportunity();
        opp.Name = 'Test';
        opp.CloseDate= System.Today();
        opp.StageName='Prospecting';
        insert opp;
        
        // Add product and Pricebook to the particular opportunity using OpportunityLineItem 
        OpportunityLineItem oppLineItem = new OpportunityLineItem();
        oppLineItem.OpportunityId = opp.Id;
        oppLineItem.PricebookEntryId = customPriceBookEntry.Id;
        oppLineItem.UnitPrice = 7000;
        oppLineItem.Quantity = 5;
        insert oppLineItem;
    }
}
Could you help me please?
I use a code that initially works, but after some tests it stops working. I copy all the code when it works and after that it stops, I paste it, but doesn't works.
This is the part of the code (that belongs to class Catalog Controller from Catalog Order package):
public PageReference toCatalog() {
   
 // begins here    

        Product2 p2 = new Product2();
        p2.Name = 'P2';
        p2.isActive = true;
        insert p2;
        
        Opportunity opp2 = new Opportunity();
        opp2.Name = 'OPP TESTE';
        opp2.CloseDate = System.Today();
        opp2.StageName = 'Prospecting';
        insert opp2;

//ends here

        if(!Product2.sObjectType.getDescribe().isAccessible()) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'Insufficient access'));
            return null;
        }
        PageReference next = new PageReference('/apex/catalog_products');
        next.setRedirect(false);
       
        // Directing to Catalog
        return next;
        }

 
Product2 p2 = new Product2();
        p2.Name = 'P3';
        p2.isActive = true;
        insert p2;
        
        Pricebook2 meupricebook = new Pricebook2();
        meupricebook.Name = 'Meu Pricebook';
        meupricebook.isActive = true;
        insert meupricebook;
        
        PriceBook2 meupricebookrec = [SELECT Id FROM Product2 WHERE id =: meupricebook.Id];
        Id stdpricebookrecid = Test.getStandatPricebookId();
        
        PriceBookEntry sdtpricebookentry = new PriceBookEntry();
        sdtpricebookentry.Product2Id = p2.Id;
        sdtpricebookentry.Pricebook2Id = stdpricebookrecid;
        sdtpricebookentry.UnitPrice = 2000;
        sdtpricebookentry.isActive = true;
        insert sdtpricebookentry;
        
        PriceBookEntry meupricebookentry = new PriceBookEntry();
        meupricebookentry.Product2Id = p2.Id;
        meupricebookentry.Pricebook2Id = meupricebookrec.Id;
        meupricebookentry.UnitPrice = 5000;
        meupricebookentry.isActive = true;
        insert meupricebookentry;
        
        Opportunity opp2 = new Opportunity();
        opp2.Name = 'OPP TESTE 2';
        opp2.CloseDate = System.Today();
        opp2.StageName = 'Prospecting';
        insert opp2;
        
        OpportunityLineItem meuopplineitem = new OpportunityLineItem();
        meuopplineitem.OpportunityId= opp2.Id;
        meuopplineitem.PricebookEntryId = meupricebookentry.Id;
        meuopplineitem.UnityPrice = 7000;
        meuopplineitem.Quantity = 5;
        insert meuopplineitem;

Can someone help me? 
I'm trying to create an Opportunity and insert it a product. It's the message that appears when I put the trigger : expecting right curly bracket, found 'trigger'. Someone can help me?
 
opp = new Opportunity();
        opp.Name = 'Test';
        opp.CloseDate= System.Today();
        opp.StageName='Prospecting';
        insert opp;
        
        oppLineItem = new OpportunityLineItem();
        oppLineItem.OpportunityId = opp.Id;
        oppLineItem.UnitPrice =1000;
        oppLineItem.Quantity =1;
        insert oppLineItem;
        
        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= pr.Id];
            insert oliList;
        }
This code gives me the error : unecpected token '='. It is about the fisrt  '='.
I don't know why this.

Can some one help me?:
 
Product2 pr = new Product2();
    pr.Name = 'PRODUTO TESTE';
    pr.isActive=true;
    insert pr;
    
    PriceBook2 customPriceBook = new PriceBook2();
    customPriceBook.Name='Custom Pricebook';
    customPriceBook.IsActive=true;
    insert customPriceBook;    

    Pricebook2 customPriceBookRec=[select Id from Pricebook2 where id=:customPriceBook.Id];
    Id stdPriceBookRecId = Test.getStandardPricebookId();
    
    PriceBookEntry customPriceBookEntry = new PriceBookEntry();
    customPriceBookEntry.Product2Id=pr.Id;
    customPriceBookEntry.Pricebook2Id=customPriceBookRec.Id;
    customPriceBookEntry.UnitPrice=1000;
    customPriceBookEntry.IsActive=true;
    insert customPriceBookEntry;
    
    Opportunity opp = new Opportunity();
    opp.Name = 'Test';
    opp.CloseDate= System.Today();
    opp.StageName='Prospecting';
    insert opp;
    
    OpportunityLineItem oppLineItem = new OpportunityLineItem();
    oppLineItem.OpportunityId = opp.Id;
    oppLineItem.PricebookEntryId = customPriceBookEntry.Id;
    oppLineItem.UnitPrice =1000;
    oppLineItem.Quantity =1;
    insert oppLineItem;

 
The error "The content of elements must consist of well-formed character data or markup" is pointing to this line of Visualforce Page code: 
<!– <apex:commandButton value="Cancel" action="{!Cancel}"/> –>

Some one can help me?
I'm trying to send the products that are added in the Catalog Order to the list of products in Opportunity. I'm making the same commands of the Catalog with the Opportunity, but the error  appears : "Visualforce Error: Attempt to dereference a null object".
This is the code that calls the error and is from Class Catalog Controller.

    /* 
     *  prepareCart()
     *  Additional constructor helper method for initializing Cart variables. 
     */
    public pageReference prepareCart() {
        if(!Catalog_Order__c.sObjectType.getDescribe().isCreateable()) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'Insufficient access'));
            return null;
        }
        productSet = new Set<String>();
        myOrder = new Catalog_Order__c();
        myOpp = new Opportunity();
        
        myLineItems = new List<Catalog_Line_item__c>();
        myOppLineItems = new List<OpportunityLineItem>();
        
 /*     
        This query tries to eliminate the constant creation of more and more Orders
        It grabs the last catalog modified by the current user with the status of Cart  
*/
         
        Catalog_Order__c[] orderQuery = [SELECT Items__c,Total_Cost__c,Account__c,Id,(SELECT Id,Product__c,Product_Short_Description__c,Product_Price__c,Product_Name__c,Quantity__c,VF_Image__c FROM Catalog_Line_Items__r) FROM Catalog_Order__c WHERE Status__c = 'Cart' AND CreatedById =:UserInfo.getUserId() ORDER BY LastModifiedDate DESC NULLS FIRST LIMIT 1];
        Opportunity[] oppQuery = [SELECT Id,(SELECT Id, Name FROM Produtos__r) FROM Opportunity WHERE CreatedById =:UserInfo.getUserId() ORDER BY LastModifiedDate DESC NULLS FIRST LIMIT 1];
        
        if( orderQuery.size() > 0 ) {
            myOrder = orderQuery[0];
            myOpp = oppQuery[0];
                        
            List<Catalog_Line_Item__c> cliList =  myOrder.getSObjects('Catalog_Line_Items__r'); //Get all Line Items for Order
            List<OpportunityLineItem> opplilist = myOpp.getSObjects('Produtos__r');
       
            if (cliList == null || cliList.size() < 1) {
                // No Line Items in related list so do nothing
            } else {
                // Line Items returned, so add them to product set
               for(Catalog_Line_Item__c cli : cliList) {
                    productSet.add( cli.Product__c );
               }
               
           //    opplilist.add(myOpp.getSObject('Produtos__r'));
               
               for(OpportunityLineItem oppli : opplilist){
                    productSet.add( oppli.ProductCode);
               }
            }
        }        
        return null;
    }

Can some one help me?
How could I insert a product in Opportunity by the Apex (the problem is because the sentence is for tests, so how I do this?).
 
public class Teste {

    public void teste1(){
        // Insert Product
        Product2 pr = new Product2();
        pr.Name='Moto - G1';
        pr.isActive=true;
        insert pr;
        
        // Insert Pricebook
        PriceBook2 customPriceBook = new PriceBook2();
        customPriceBook.Name='Custom Pricebook';
        customPriceBook.IsActive=true;
        insert customPriceBook;
        
        // Query Standard and Custom Price Books
        Pricebook2 customPriceBookRec=[select Id from Pricebook2 where id=:customPriceBook.Id];
        Id stdPriceBookRecId = Test.getStandardPricebookId();
        
        // Create Standard PriceBookEntry
        PriceBookEntry stdPriceBookEntry = new PriceBookEntry();
        stdPriceBookEntry.Product2Id=pr.Id;
        stdPriceBookEntry.Pricebook2Id=stdPriceBookRecId;
        stdPriceBookEntry.UnitPrice=2000;
        stdPriceBookEntry.IsActive=true;
        insert stdPriceBookEntry;
        // Create Custom PriceBookEntry
        PriceBookEntry customPriceBookEntry = new PriceBookEntry();
        customPriceBookEntry.Product2Id=pr.Id;
        customPriceBookEntry.Pricebook2Id=customPriceBookRec.Id;
        customPriceBookEntry.UnitPrice=5000;
        customPriceBookEntry.IsActive=true;
        insert customPriceBookEntry;
        
        // Create Opportunity
        Opportunity opp = new Opportunity();
        opp.Name = 'Test';
        opp.CloseDate= System.Today();
        opp.StageName='Prospecting';
        insert opp;
        
        // Add product and Pricebook to the particular opportunity using OpportunityLineItem 
        OpportunityLineItem oppLineItem = new OpportunityLineItem();
        oppLineItem.OpportunityId = opp.Id;
        oppLineItem.PricebookEntryId = customPriceBookEntry.Id;
        oppLineItem.UnitPrice = 7000;
        oppLineItem.Quantity = 5;
        insert oppLineItem;
    }
}

Please, help me.
I use a code that initially works, but after some tests it stops working. I copy all the code when it works and after that it stops, I paste it, but doesn't works.
This is the part of the code (that belongs to class Catalog Controller from Catalog Order package):
public PageReference toCatalog() {
   
 // begins here    

        Product2 p2 = new Product2();
        p2.Name = 'P2';
        p2.isActive = true;
        insert p2;
        
        Opportunity opp2 = new Opportunity();
        opp2.Name = 'OPP TESTE';
        opp2.CloseDate = System.Today();
        opp2.StageName = 'Prospecting';
        insert opp2;

//ends here

        if(!Product2.sObjectType.getDescribe().isAccessible()) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'Insufficient access'));
            return null;
        }
        PageReference next = new PageReference('/apex/catalog_products');
        next.setRedirect(false);
       
        // Directing to Catalog
        return next;
        }

 
Hi,

I'' m trying to put the value of Quantity from Catalog_Line_Item__c to 'Quantity' from Opportunity.
 
/* 
     *  updateQuantity(String ID) 
     *  Updates the Quantity field of the Catalog Line Item with the given Id 
     */
    public PageReference updateQuantity() {
        if(!Catalog_Line_Item__c.sObjectType.getDescribe().isUpdateable()) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'Insufficient access'));
            return null;
        }
        
        li = [SELECT Quantity__c FROM Catalog_Line_Item__c WHERE Id =: productId LIMIT 1];
        li.Quantity__c = productQuantity;
        update li;
        update myOrder;      
        quantity = li.Quantity__c;
        
        // Stay on current page
        return null;
    }
(variable 'quantity' is double)
 
// Add product and Pricebook to the particular opportunity using OpportunityLineItem 
        OpportunityLineItem oppLineItem = new OpportunityLineItem();
        oppLineItem.OpportunityId = opp.Id;
        oppLineItem.PricebookEntryId = customPriceBookEntry.Id;
        oppLineItem.UnitPrice = customPriceBookEntry.UnitPrice;
        oppLineItem.Quantity = quantity ;
        insert oppLineItem;

This is the error message. that appeats when I Execute the Catalog:
Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING: [Quantity]: [Quantity].

Can someone help me?
Thanks.
public pageReference addToCart() {
        if(!Catalog_Order__c.sObjectType.getDescribe().isUpdateable() || !Catalog_Line_Item__c.sObjectType.getDescribe().isCreateable()) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'Insufficient access'));
            return null;
        }
        if( productId == null ) {} else {
        // First make sure that our Order is in the database
        upsert myOrder;
        orderId = myOrder.Id;

        produto = productId;
        // Check if the product is already in the Cart
        if( productSet.contains( productId ) ) { } else {
            // Here is where we add the ID to the set...
            productSet.add( productId );
            // ...and to the Order.
            Catalog_Line_Item__c li =  new Catalog_Line_item__c();
            li.Product__c = productId;
            produto = productId;
            li.Catalog_Order__c = myOrder.Id;
            li.Quantity__c = 1;
            insert li;        
            
            // Added to Cart
            }
        }
        // Clear the parameter and reupsert
        productId = null;
        upsert myOrder;
        return null;
    }
 public PageReference toCart() {

        integer stdPriceBookRecId2; 
        List<Pricebook2> stdPriceBookRecId = new List<Pricebook2>();
        Catalog_Order__c[] orderQuery = [SELECT Items__c,Total_Cost__c,Account__c,Id,(SELECT Id,Product__c,Product_Short_Description__c,Product_Price__c,Product_Name__c,Quantity__c,VF_Image__c FROM Catalog_Line_Items__r) FROM Catalog_Order__c WHERE Status__c = 'Cart' AND CreatedById =:UserInfo.getUserId() ORDER BY LastModifiedDate DESC NULLS FIRST LIMIT 1];

        // Insert Pricebook
        PriceBook2 customPriceBook = new PriceBook2();
        customPriceBook.Name='Custom Pricebook';
        customPriceBook.IsActive=true;
        insert customPriceBook;
        
        // Query Standard and Custom Price Books
        Pricebook2 customPriceBookRec=[select Id from Pricebook2 where id=:customPriceBook.Id];
        stdPriceBookRecId = [SELECT Id, isstandard FROM Pricebook2 WHERE IsStandard=true];
        
        // Create Standard PriceBookEntry
        PriceBookEntry stdPriceBookEntry = new PriceBookEntry();
        stdPriceBookEntry.Product2Id=produto;
        stdPriceBookEntry.Pricebook2Id=stdPriceBookRecId[0].Id;
        stdPriceBookEntry.UnitPrice=2001;
        stdPriceBookEntry.IsActive=true;
        insert stdPriceBookEntry;
        
        // Create Custom PriceBookEntry
        PriceBookEntry customPriceBookEntry = new PriceBookEntry();     
        customPriceBookEntry.Product2Id=produto;
        customPriceBookEntry.Pricebook2Id=customPriceBookRec.Id;
        customPriceBookEntry.UnitPrice=5001;
        customPriceBookEntry.IsActive=true;
        insert customPriceBookEntry;
        
        // Create Opportunity
        Opportunity opp = new Opportunity();
        opp.Name = 'OPORTUNIDADE TESTE';
        opp.CloseDate= System.Today();
        opp.StageName='Prospecting';
        insert opp;
        
        // Add product and Pricebook to the particular opportunity using OpportunityLineItem 
        OpportunityLineItem oppLineItem = new OpportunityLineItem();
        oppLineItem.OpportunityId = opp.Id;
        oppLineItem.PricebookEntryId = customPriceBookEntry.Id;
        oppLineItem.UnitPrice = 7001;
        oppLineItem.Quantity = 4;
        insert oppLineItem;  
        
        
        if(!Catalog_Order__c.sObjectType.getDescribe().isAccessible()) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'Insufficient access'));
            return null;
        }
        try{
            myOrder = [SELECT Items__c,Lines__c,Total_Cost__c,Account__c,Id,(SELECT Id,Product__c,Product_Short_Description__c,Product_Price__c,Product_Name__c,Price__c,Quantity__c,Subtotal__c,VF_Image__c FROM Catalog_Line_Items__r) FROM Catalog_Order__c WHERE Id =: orderId LIMIT 1];
        } catch (exception e) {System.debug(e.getMessage());}

        PageReference next = new PageReference('/apex/catalog_cart');
        next.setRedirect(false);
        // Directing to Cart
        return next;
    }
This is the code.

These are pieces of Catalog_Controller Class that I'm modifying to add the products of the Catalog Order to an Opportunity that's created when the catalog is submited. The error : 
' Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, This price definition already exists in this price book: [] '
appers.

Can someone help me?

Thanks.
Catalog_Controller : Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, product ID: Type of value of ID incorrect: a0B36000002nFaZEAU: [Product2Id]

I'm trying to generate an Opportunity when a Catalog Order is submited. This Opportunity must have the product that's in the order. I was creating a product each time I create a Opportunity, but I can't create the product again, but I need to get the product data and place it in the Opportunity products.

Here is what I did:
public List<Catalog_Order__c> listaproduto {get;set;} 
// public Product2 produto2 = new Product2(); 
public String nomeproduto; 
public String codproduto; 
public ID idproduto; 
public String descproduto; 
public datetime createddateproduto; 
public String createdbyidproduto; 
public String familyproduto;
I've created variables to get the product data.
listaproduto=[SELECT Items__c,Total_Cost__c,Account__c,Id,(SELECT Id,Product__c,Product_Short_Description__c,Product_Price__c,Product_Name__c,Quantity__c,VF_Image__c FROM Catalog_Line_Items__r) FROM Catalog_Order__c WHERE Status__c = 'Cart' AND CreatedById =:UserInfo.getUserId() ORDER BY LastModifiedDate DESC NULLS FIRST LIMIT 1]; 
    for(Catalog_Order__c ctlgOrdr : listaproduto){ 
        for(Catalog_Line_Item__c ctlgLineItem : ctlgOrdr.Catalog_Line_Items__r){ 
        System.debug(ctlgLineItem.Product__c); 
        ctlgLineItem.Product__c = ctlgLineItem.Id; 
        idproduto = ctlgLineItem.Id; 
        nomeproduto = ctlgLineItem.Product_Name__c; 
        descproduto = ctlgLineItem.Product_Short_Description__c; 
        codproduto = ctlgLineItem.Product__c;
     // produto2.isActive = true; 
        }
    }
Here I tried to pass the product data to the variables.
integer stdPriceBookRecId2; 
        List<Pricebook2> stdPriceBookRecId = new List<Pricebook2>();
        Catalog_Order__c[] orderQuery = [SELECT Items__c,Total_Cost__c,Account__c,Id,(SELECT Id,Product__c,Product_Short_Description__c,Product_Price__c,Product_Name__c,Quantity__c,VF_Image__c FROM Catalog_Line_Items__r) FROM Catalog_Order__c WHERE Status__c = 'Cart' AND CreatedById =:UserInfo.getUserId() ORDER BY LastModifiedDate DESC NULLS FIRST LIMIT 1];

        // Insert Pricebook
        PriceBook2 customPriceBook = new PriceBook2();
        customPriceBook.Name='Custom Pricebook';
        customPriceBook.IsActive=true;
        insert customPriceBook;
        
        // Query Standard and Custom Price Books
        Pricebook2 customPriceBookRec=[select Id from Pricebook2 where id=:customPriceBook.Id];
        stdPriceBookRecId = [SELECT Id, isstandard FROM Pricebook2 WHERE IsStandard=true];
        
        // Create Standard PriceBookEntry
        PriceBookEntry stdPriceBookEntry = new PriceBookEntry();
        stdPriceBookEntry.Product2Id=idproduto;
        stdPriceBookEntry.Pricebook2Id=stdPriceBookRecId[0].Id;
        stdPriceBookEntry.UnitPrice=2000;
        stdPriceBookEntry.IsActive=true;
        insert stdPriceBookEntry;
        
        // Create Custom PriceBookEntry
        PriceBookEntry customPriceBookEntry = new PriceBookEntry();
        customPriceBookEntry.Product2Id=idproduto;
        customPriceBookEntry.Pricebook2Id=customPriceBookRec.Id;
        customPriceBookEntry.UnitPrice=5000;
        customPriceBookEntry.IsActive=true;
        insert customPriceBookEntry;
        
        // Create Opportunity
        Opportunity opp = new Opportunity();
        opp.Name = 'OPORTUNIDADE TESTE';
        opp.CloseDate= System.Today();
        opp.StageName='Prospecting';
        insert opp;
        
        // Add product and Pricebook to the particular opportunity using OpportunityLineItem 
        OpportunityLineItem oppLineItem = new OpportunityLineItem();
        oppLineItem.OpportunityId = opp.Id;
        oppLineItem.PricebookEntryId = customPriceBookEntry.Id;
        oppLineItem.UnitPrice = 7000;
        oppLineItem.Quantity = 5;
        insert oppLineItem;
Here I tried to create the Opportunity and associate it with the product and pricebooks.

The error points to the line :" insert stdPriceBookEntry;"
I've tried to put the ID 'a0B36000002nFaZEAU' instead the variable, but it doesn't work too.

Can someone help me?
Catalog_Controller : Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, product ID: Type of value of ID incorrect: a0B36000002nFaZEAU: [Product2Id]

I'm trying to generate an Opportunity when a Catalog Order is submited. This Opportunity must have the product that's in the order. I was creating a product each time I create a Opportunity, but I can't create the product again, but I need to get the product data and place it in the Opportunity products.

Here is what I did:
public List<Catalog_Order__c> listaproduto {get;set;} 
// public Product2 produto2 = new Product2(); 
public String nomeproduto; 
public String codproduto; 
public ID idproduto; 
public String descproduto; 
public datetime createddateproduto; 
public String createdbyidproduto; 
public String familyproduto;
I've created variables to get the product data.
 
listaproduto=[SELECT Items__c,Total_Cost__c,Account__c,Id,(SELECT Id,Product__c,Product_Short_Description__c,Product_Price__c,Product_Name__c,Quantity__c,VF_Image__c FROM Catalog_Line_Items__r) FROM Catalog_Order__c WHERE Status__c = 'Cart' AND CreatedById =:UserInfo.getUserId() ORDER BY LastModifiedDate DESC NULLS FIRST LIMIT 1]; 
    for(Catalog_Order__c ctlgOrdr : listaproduto){ 
        for(Catalog_Line_Item__c ctlgLineItem : ctlgOrdr.Catalog_Line_Items__r){ 
        System.debug(ctlgLineItem.Product__c); 
        ctlgLineItem.Product__c = ctlgLineItem.Id; 
        idproduto = ctlgLineItem.Id; 
        nomeproduto = ctlgLineItem.Product_Name__c; 
        descproduto = ctlgLineItem.Product_Short_Description__c; 
        codproduto = ctlgLineItem.Product__c;
     // produto2.isActive = true; 
        }
    }
Here I tried to pass the product data to the variables.
 
integer stdPriceBookRecId2; 
        List<Pricebook2> stdPriceBookRecId = new List<Pricebook2>();
        Catalog_Order__c[] orderQuery = [SELECT Items__c,Total_Cost__c,Account__c,Id,(SELECT Id,Product__c,Product_Short_Description__c,Product_Price__c,Product_Name__c,Quantity__c,VF_Image__c FROM Catalog_Line_Items__r) FROM Catalog_Order__c WHERE Status__c = 'Cart' AND CreatedById =:UserInfo.getUserId() ORDER BY LastModifiedDate DESC NULLS FIRST LIMIT 1];

        // Insert Pricebook
        PriceBook2 customPriceBook = new PriceBook2();
        customPriceBook.Name='Custom Pricebook';
        customPriceBook.IsActive=true;
        insert customPriceBook;
        
        // Query Standard and Custom Price Books
        Pricebook2 customPriceBookRec=[select Id from Pricebook2 where id=:customPriceBook.Id];
        stdPriceBookRecId = [SELECT Id, isstandard FROM Pricebook2 WHERE IsStandard=true];
        
        // Create Standard PriceBookEntry
        PriceBookEntry stdPriceBookEntry = new PriceBookEntry();
        stdPriceBookEntry.Product2Id=idproduto;
        stdPriceBookEntry.Pricebook2Id=stdPriceBookRecId[0].Id;
        stdPriceBookEntry.UnitPrice=2000;
        stdPriceBookEntry.IsActive=true;
        insert stdPriceBookEntry;
        
        // Create Custom PriceBookEntry
        PriceBookEntry customPriceBookEntry = new PriceBookEntry();
        customPriceBookEntry.Product2Id=idproduto;
        customPriceBookEntry.Pricebook2Id=customPriceBookRec.Id;
        customPriceBookEntry.UnitPrice=5000;
        customPriceBookEntry.IsActive=true;
        insert customPriceBookEntry;
        
        // Create Opportunity
        Opportunity opp = new Opportunity();
        opp.Name = 'OPORTUNIDADE TESTE';
        opp.CloseDate= System.Today();
        opp.StageName='Prospecting';
        insert opp;
        
        // Add product and Pricebook to the particular opportunity using OpportunityLineItem 
        OpportunityLineItem oppLineItem = new OpportunityLineItem();
        oppLineItem.OpportunityId = opp.Id;
        oppLineItem.PricebookEntryId = customPriceBookEntry.Id;
        oppLineItem.UnitPrice = 7000;
        oppLineItem.Quantity = 5;
        insert oppLineItem;
Here I tried to create the Opportunity and associate it with the product and pricebooks.

The error points to the line :" insert stdPriceBookEntry;"

Can someone help me?

 
This error appeared and point to the line 200. I don't know why the id value is not being valid for the object.
 
listaproduto=[SELECT Items__c,Total_Cost__c,Account__c,Id,(SELECT Id,Product__c,Product_Short_Description__c,Product_Price__c,Product_Name__c,Quantity__c,VF_Image__c FROM Catalog_Line_Items__r) FROM Catalog_Order__c WHERE Status__c = 'Cart' AND CreatedById =:UserInfo.getUserId() ORDER BY LastModifiedDate DESC NULLS FIRST LIMIT 1];
        
        for(Catalog_Order__c ctlgOrdr : listaproduto){
            for(Catalog_Line_Item__c ctlgLineItem : ctlgOrdr.Catalog_Line_Items__r){
                System.debug(ctlgLineItem.Product__c);       
                
                produto2.Id = ctlgLineItem.Id;  // LINE 200
                produto2.Name = ctlgLineItem.Product_Name__c;
                produto2.Description = ctlgLineItem.Product_Short_Description__c;
                produto2.isActive = true;
            }
        }

 
I need to get the name, description, quantity .... of this product. But it is inside a subquery.
nomeproduto=[SELECT Items__c,Total_Cost__c,Account__c,Id,(SELECT Id,Product__c,Product_Short_Description__c,Product_Price__c,Product_Name__c,Quantity__c,VF_Image__c FROM Catalog_Line_Items__r) FROM Catalog_Order__c WHERE Status__c = 'Cart' AND CreatedById =:UserInfo.getUserId() ORDER BY LastModifiedDate DESC NULLS FIRST LIMIT 1];
Can someone help me?
 
How could I insert a product in Opportunity by the Apex (the problem is because the sentence is for tests, so how I do this?).
 
public class Teste {

    public void teste1(){
        // Insert Product
        Product2 pr = new Product2();
        pr.Name='Moto - G1';
        pr.isActive=true;
        insert pr;
        
        // Insert Pricebook
        PriceBook2 customPriceBook = new PriceBook2();
        customPriceBook.Name='Custom Pricebook';
        customPriceBook.IsActive=true;
        insert customPriceBook;
        
        // Query Standard and Custom Price Books
        Pricebook2 customPriceBookRec=[select Id from Pricebook2 where id=:customPriceBook.Id];
        Id stdPriceBookRecId = Test.getStandardPricebookId();
        
        // Create Standard PriceBookEntry
        PriceBookEntry stdPriceBookEntry = new PriceBookEntry();
        stdPriceBookEntry.Product2Id=pr.Id;
        stdPriceBookEntry.Pricebook2Id=stdPriceBookRecId;
        stdPriceBookEntry.UnitPrice=2000;
        stdPriceBookEntry.IsActive=true;
        insert stdPriceBookEntry;
        // Create Custom PriceBookEntry
        PriceBookEntry customPriceBookEntry = new PriceBookEntry();
        customPriceBookEntry.Product2Id=pr.Id;
        customPriceBookEntry.Pricebook2Id=customPriceBookRec.Id;
        customPriceBookEntry.UnitPrice=5000;
        customPriceBookEntry.IsActive=true;
        insert customPriceBookEntry;
        
        // Create Opportunity
        Opportunity opp = new Opportunity();
        opp.Name = 'Test';
        opp.CloseDate= System.Today();
        opp.StageName='Prospecting';
        insert opp;
        
        // Add product and Pricebook to the particular opportunity using OpportunityLineItem 
        OpportunityLineItem oppLineItem = new OpportunityLineItem();
        oppLineItem.OpportunityId = opp.Id;
        oppLineItem.PricebookEntryId = customPriceBookEntry.Id;
        oppLineItem.UnitPrice = 7000;
        oppLineItem.Quantity = 5;
        insert oppLineItem;
    }
}

Please, help me.