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
Filipe BaradelliFilipe Baradelli 

REQUIRED_FIELD_MISSING: [Quantity]: [Quantity]

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.
Ishwar ShindeIshwar Shinde
Hi Filipe,

Could you please check the quantity varibale value before assigning to oppLineItem.Quantity. Seems like a Null value is getting assiged to Opporunity line item and it is throwing an error. 

Thanks,
Ishwar

Please mark this as the best answer if this helps!!
Filipe BaradelliFilipe Baradelli
I checked the 'quantity', but the problem was the same.
Akash_CRMAkash_CRM
Hi Fillipe,

Can you debug and check for the value of productQuantity and also I would recommend you to include the update in a try catch block so that the runtime exceptions could be handled.

Regards,
Akash.
Kevin CrossKevin Cross
What is the actual debugged value of quantity at the time of assignment?  The issue may be that it is not null but still doesn't parse to double.  Therefore, the assignment is failing.
Filipe BaradelliFilipe Baradelli
How could I parse this value to double? I've tried with the conversion of other type of variable, but didn't work.
Kevin CrossKevin Cross
What is the current value and type?
It should work as Double.valueOf(your_field_name) or Double.valueOf(string_value).
Filipe BaradelliFilipe Baradelli
Value: quantity = li.Quantity__c
quantity is double
Kevin CrossKevin Cross
Yes, sorry.  I meant the current value shown when you did System.debug(quantity) per the other suggestion above.  I figure you either wrapped this with an IF statement or debugged the value to "check" it before the assignment.  Therefore, I was wondering what the current value was to make sure it wasn't one of those cases where it is a double value stored as a string that has a character that gives the conversion a problem.  Anyway, if it is a string, you should be able to use Double.valueOf( ) to convert.
Filipe BaradelliFilipe Baradelli
I think that the 'quantity' is not null but is not double too.
I put the following code:
try {
        if (quantity == li.Quantity__c){
            Double.ValueOf(quantity);
            oppLineItem.Quantity = quantity;
            } else {
            oppLineItem.Quantity = 2;
            }
        } catch (exception e) {
        
            oppLineItem.Quantity = 1;
        }

The error continues. Appears that it is not null and the command Double.ValueOf(quantity) is not working.
Filipe BaradelliFilipe Baradelli
I've put the wrong code. This one show that the 'quantity' is null:
try {
        if (quantity == li.Quantity__c && quantity != null){
            Double.ValueOf(quantity);
            oppLineItem.Quantity = quantity;
            } else {
            oppLineItem.Quantity = 2;
            }
        } catch (exception e) {
        
            oppLineItem.Quantity = 1;
        }

I don't know why it is null, the variable 'li' (the query) is public and is working for the other parts.
 
Ishwar ShindeIshwar Shinde
Hi Filipe, Your variable is not maintaining state, may be something wrong with declaration. You can query the Catalog_Item object before assigning quantity to opportunity line item in same method. Hope it will help.
Kevin CrossKevin Cross
Agreed. If you cannot figure it out, please post the entire code instead of in chunks as the issue likely is the scope of the variables.
Filipe BaradelliFilipe Baradelli
Here is the code. It's note working too:
/* 
     *  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];
        quantity = [SELECT Quantity__c FROM Catalog_Line_Item__c WHERE Id =: productId LIMIT 1];
        li.Quantity__c = productQuantity;
        quantity.Quantity__c = productQuantity;
//        quantity = li.Quantity__c;
        update quantity;
        update li;
        update myOrder;      
            
        // Stay on current page
        return null;

Variable 'quantity' is declared:     public Catalog_Line_Item__c quantity = new Catalog_Line_Item__c();
( like 'li')
 
// 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;
        try {
        if (quantity.Quantity__c != null){
            oppLineItem.Quantity = Double.ValueOf(quantity.Quantity__c);
            } else {
            oppLineItem.Quantity = 2;
            }
        } catch (exception e) {
            oppLineItem.Quantity = 1;
        }
        insert oppLineItem;

The oppLineItem.Quantity is resulting 2.
Kevin CrossKevin Cross
Why do you keep splitting the code up into different blocks.  Are they in different files?  I guess I was hoping to see the entire code file at once because it appears the error is scoping as pointed out before.  However, I am perplexed by what your intent is with:

 public Catalog_Line_Item__c quantity = new Catalog_Line_Item__c();

This is storing a Catalog_Line_Item__c value and not just a double quantity.  This does not seem to make sense for my previous understanding of what you were doing in the code.  Further, in this new code you pull the same record into li and quantity, so update li and update quantity are repititious.  What is the point?

Anyway, to understand why quantity is failing the IF condition in the second code block, I would need to see the full code for both files if this is not in one file.  As seeing this in this fashion is hindering our ability to help you here.
Kevin CrossKevin Cross
P.S. I am on a project now, so if you cannot reply with full code before I start traveling you may not hear back from me for a while.  Either way, good luck with finding the problem.  Think you need to step back and document in pseudo code what you want to do.  From there it may help you debug this problem. 
Ishwar ShindeIshwar Shinde
I beileve your page is gettting refreshed, which reset the catalog item value which you have populated earlier in updateQuantity() method.

You can try below code - 
Catalog_Line_Item__c catalogItem = [SELECT Quantity__c FROM Catalog_Line_Item__c WHERE Id =: productId LIMIT 1];
	
	// 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 = Double.ValueOf(catalogItem.Quantity__c);
            
        
        insert oppLineItem;

 
Filipe BaradelliFilipe Baradelli
I know that what I was doing did not make sense, I'm confusing many things. Howerver, I tried the code that you gave me Shinde Ishwar, but now the error is: 'List has no rows for assignment to SObject'.
Here is the full code:
public with sharing class Catalog_Controller {

    // Primarily Catalog variables
    public Map<String, String> familyMap {get;set;}
    public Map<String, List<Product2>> productMap {get;set;}
    public Integer numberOfFamilies {get;set;}
    public List<Product2> productsInFamily {get;set;}
    public String hostURL {get;set;}
    public Catalog_Template__c myCatalogTemplate {get;set;}
    public String backgroundURL {get;set;}
    
    // Primarily Cart variables
    public Set<String> productSet {get;set;}
    public String productId {get;set;}
    public String accountId {get;set;}
    public String orderId {get;set;}
    public Integer productQuantity {get;set;}
    public Catalog_Order__c myOrder {get;set;}
    public Opportunity myOpp {get;set;}
    public List<Catalog_Line_item__c> myLineItems {get;set;}
    public List<OpportunityLineItem> myOppLineItems {get;set;}

    // Lookup Component variables
    public string debugString {get;set;}
    public String lookupValue {get;set;}
    public sObject myObject {get;set;}
    public String objectName {get;set;}
    public String objectField {get;set;}
    public String filter {get;set;}
    public List<Item> myValues {get;set;}

    public List<Catalog_Order__c> listaproduto {get;set;}
    public String nomeproduto {get;set;}
    public String codproduto {get;set;}
    public ID idproduto {get;set;}
    public String descproduto {get;set;}
    public datetime createddateproduto {get;set;}
    public String createdbyidproduto {get;set;}
    public String familyproduto {get;set;}
    public ID produto {get;set;}
    Public Catalog_Line_Item__c li = new Catalog_Line_Item__c();
    
    // Custom Item class for use with Lookup Component 
    // contains query results (Name and ID)
    public class Item{
        public String value {get;set;}
        public String label {get;set;}

        public Item(String v, String l){
          value = v;
          label = l;
        }
    }

    public Catalog_Controller() {
        prepareBackground();
        prepareCatalog();
        prepareCart();
        prepareCatalogTemplate();
        debugString = 'My debug';
    }

    /* 
     *  prepareBackground()
     *  Additional constructor helper method grabbing the appropriate background image. 
     */
    public void prepareBackground() {
        String servlet = '/servlet/servlet.FileDownload?file=';
        String backgroundName = 'Catalog_BG';
        Document backgroundDoc = [SELECT Id FROM Document WHERE Name = :backgroundName];
        backgroundURL = servlet + backgroundDoc.Id;
    }

    /*
     *  prepareCatalog()
     *  This method contains all initialization for the catalog, and notably aids in 
     *  navigation with the FamilyMap (used in the category nav on the first page). 
     */
    public pageReference prepareCatalog() {
        if(!Product2.sObjectType.getDescribe().isAccessible()) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'Insufficient access'));
            return null;
        }
        // Initialize maps, run queries
        productMap = new Map<String, List<Product2>>();
        familyMap = new Map<String, String>();
        numberOfFamilies = 0;

        // Prepare the picklist values from Family field on Product2
        Schema.DescribeFieldResult familyResult = Product2.Family.getDescribe();
        List<Schema.PicklistEntry> familyPLE = familyResult.getPicklistValues();

        // For each Family picklist value, clean the value and continue
        for( Schema.PicklistEntry f : familyPLE ) {
            String familyLabel = f.getLabel().trim();
            String familyValue = f.getValue().trim();
            String familyNoWhtSpc = familyValue.replaceAll('\\s+','');
            // Now that we have a clean Family, get all Products with that Family
            // Then add it to the map for Family -> ProductList
            Integer countProductsInFamily = 0;
            try {
                countProductsInFamily = 0;
                countProductsInFamily = [SELECT count() FROM Product2 WHERE Family = :familyValue AND Mobile_Ready__c = true];
                if(countProductsInFamily > 0) {
                    productsInFamily = [SELECT Id,Name,Description,Family,ProductCode,Blurb__c,Default_Price__c,VF_Image__c,Inventory__c,Mobile_Ready__c FROM Product2 WHERE Mobile_Ready__c = true AND Family = :familyValue ORDER BY Name DESC NULLS FIRST];
                    FamilyMap.put(familyNoWhtSpc, familyLabel);
                    ProductMap.put(familyNoWhtSpc, productsInFamily);
                    numberOfFamilies += 1;
                }
            } catch (exception e) {System.debug(e.getMessage());}
        }

        //Prep the host URL for use in Catalog images
        hostURL  = 'https://' + ApexPages.currentPage().getHeaders().get('Host');
        return null;
    }

    /* 
     *  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 Description,Amount,Catalog_Order__c,Id,(SELECT Id,Name,Inventory__c,Description FROM Produtos__r) FROM Opportunity WHERE CreatedById =:UserInfo.getUserId() ORDER BY LastModifiedDate DESC NULLS FIRST LIMIT 1];
        
        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];

        
        if( orderQuery.size() > 0 ) {
            myOrder = orderQuery[0];
            myOpp = oppQuery[0];
            
            
        // Add product and Pricebook to the particular opportunity using OpportunityLineItem 
            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 );
               }
            }
        }        
        return null;
    }

    /* 
     *  prepareCatalogTemplate()
     *  Additional constructor helper method for initializing Catalog Template variables. 
     */
    public pageReference prepareCatalogTemplate() {
        if(!Catalog_Template__c.sObjectType.getDescribe().isCreateable()) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'Insufficient access'));
            return null;
        }
        myCatalogTemplate = new Catalog_Template__c();

        // This query tries to eliminate the constant creation of more and more Templates
        // It grabs the Template with the name we set in the Brander Controller
        Catalog_Template__c[] templateQuery = [SELECT Category_Text_Color__c,Header_Left_Text_Color__c,Header_Right_Text_Color__c,Id,Name,Subtitle_Text_Color__c,Subtitle_Text_Size__c,Subtitle_Text__c,Title_Text_Color__c,Title_Text_Size__c,Title_Text__c FROM Catalog_Template__c WHERE Name = 'Catalog Template 1'];
        if( templateQuery.size() > 0 ) {
            myCatalogTemplate = templateQuery[0];
        }
        return null;
    }

    /* 
     *  addToCart()
     *  Handles the complexity of adding a product as a line item to the Order from Actionfunction
     */
    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;
      
  
        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];
        
        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;
            
//---------CÓDIGO----------------//            
            
            // Added to Cart
            }
        }
        // Clear the parameter and reupsert
        productId = null;
        upsert myOrder;
        return null;
    }

    /* 
     *  deleteFromCart()
     *  Handles the complexity of deleting a product as a line item from the Order
     */
    public PageReference deleteFromCart() {
        if(!Catalog_Order__c.sObjectType.getDescribe().isUpdateable() || !Catalog_Line_Item__c.sObjectType.getDescribe().isDeletable()) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'Insufficient access'));
            return null;
        }
        if( productId == null ) {system.debug('Empty delete.'); return null;} 
        else {
            Catalog_Line_Item__c li = [SELECT Id FROM Catalog_Line_Item__c WHERE Id =: productId];
            try {
                // About to delete Line Item
                if(!Catalog_Order__c.sObjectType.getDescribe().isDeletable()){
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'Insufficient access'));
                    return null;
                }
                delete li;
            } catch (DmlException e) {System.debug(e.getMessage());}
    
            // Check if the product is already in the Cart
            if( productSet.contains( productId ) ) { 
                // Here is where we remove the ID from the set...
                productSet.remove( productId );
            }
        }
        // Clear the parameter and reupsert
        productId = null;
        upsert myOrder;

        return null;
    }
    
    /* 
     *  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;      
            
        // Stay on current page
        return null;
    }
    
    /* 
     *  updateAccount(String ID) 
     *  Updates the Account field of the Order with the given Id 
     */
    public PageReference updateAccount() {
    
        if(!Catalog_Order__c.sObjectType.getDescribe().isUpdateable()) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'Insufficient access'));
            return null;
        }
        myOrder.Account__c = accountId;
        system.debug('====myorder======='+myOrder);
        update myOrder;
        // Stay on current page
        return null;
        
    }
    
    /* 
     *  getProduct(String ID) 
     *  queries and returns relevant product info from a string ID 
     */
    public Product2 getProduct(String stringID) {
        if(!Product2.sObjectType.getDescribe().isAccessible()) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'Insufficient access'));
            return null;
        }
              
        Product2 product = [SELECT Id,Name,Description,Family,ProductCode,Blurb__c,Default_Price__c,VF_Image__c,Inventory__c,Mobile_Ready__c FROM Product2 WHERE Id =: stringID LIMIT 1];
        return product;
    }
    
    /* 
     *  clearOrder() 
     *  clears all line items on the order and navigates to the catalog page 
     */
    public PageReference clearOrder() {
        if(!Catalog_Line_Item__c.sObjectType.getDescribe().isDeletable()) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'Insufficient access'));
            return null;
        }
        // Remove all line items from the order
        List<Catalog_Line_Item__c> toDelete = [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_Item__c WHERE Catalog_Order__c =: myOrder.Id];
        delete toDelete;

        // Stay on current page
        return null;
    }
    
    /* 
     *  completeOrder() 
     *  Completes the current order and prepares a new one for the user
     */
    public PageReference completeOrder() {
        if(!Catalog_Order__c.sObjectType.getDescribe().isCreateable() || !Catalog_Order__c.sObjectType.getDescribe().isDeletable()) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'Insufficient access'));
            return null;
        }
        //Preparing to complete myOrder
        // Set status of current order
        myOrder.Status__c = 'Submitted';
        update myOrder;
        // myOrder was submitted
        
        // create new order and set it to myOrder
        // This allows us to continue operations when the user returns to the Catalog
        productSet = new Set<String>();
        myOrder = new Catalog_Order__c();
        myOrder.Status__c = 'Cart';
        // myOrder reinitiated and set to Cart
        myLineItems = new List<Catalog_Line_item__c>();

        // Stay on current page
        return null;
    }

    /* 
     *  toCart() 
     *  navigates to the cart page 
     */
    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];
         
        // Create Custom PriceBookEntry
        
        PriceBookEntry customPriceBookEntry = new PriceBookEntry();      
        customPriceBookEntry.Product2Id=produto;
        customPriceBookEntry.Pricebook2Id=customPriceBookRec.Id;
        customPriceBookEntry.UnitPrice=[SELECT Default_Price__c FROM Product2 WHERE Id =:produto LIMIT 1].Default_Price__c;
        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 = customPriceBookEntry.UnitPrice;
        
        Catalog_Line_Item__c catalogItem = [SELECT Quantity__c FROM Catalog_Line_Item__c WHERE Id =: productId LIMIT 1];
        oppLineItem.Quantity = Double.ValueOf(catalogItem.Quantity__c);
        
        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;
    }
    
    /*
     *  toCatalog() 
     *  navigates to the catalog page 
     */    
   
    public PageReference toCatalog() {

        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;
        }

    /***************************
     *  Lookup Component Methods
     ***************************/

    /*
     *  PopulateValues() 
     *  Creates the values for the lookup
     */
    public PageReference PopulateValues() {
        if(!Account.sObjectType.getDescribe().isAccessible()) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'Insufficient access'));
            return null;
        }
        Schema.DescribeSObjectResult objDescribe;
        
        myValues = new List<Item>();
        if (filter != null) filter = escapeReservedCharacters(filter);
        String protectFilter = '%' + filter + '%';
        String myQuery = 'SELECT Id, Name FROM Account WHERE Name LIKE ' + protectFilter;
        List<SObject> recList = [SELECT Id, Name FROM Account WHERE Name LIKE :protectFilter];
        for (SObject rec : recList) {
            myValues.add(new Item((String)rec.get('Id'), (String)rec.get('Name')));
        }
        return null;
    }

    /*
     *  escapeReservedCharacters(String) 
     *  Cleans up the dishonorable...
     */
    private String escapeReservedCharacters(String s) {
        String reservedCharacters = '—&|!{}[]()^~:\\\"\'+-';
        for (Integer i = 0; i < reservedCharacters.length(); i++)
        s = s.replace(reservedCharacters.substring(i,i+1), '\\' + reservedCharacters.substring(i,i+1));
        return s;
    }

    public void TestFunction(){
        System.assert(1 == 1,'WTF');
    }
}

 
Ishwar ShindeIshwar Shinde
Hi Filipe,

You are receiving that error because of parameter used in query - productId. Your page is not maintaining state, which causes to resetting of variable value. Because of this, query is not retruning any value to you. 

Please share the page code and the sequence which you follow to perform this activiity? It will be good if you share the controller method which is getting called on each activity.