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 

Invalid id value for this SObject type: a0B36000002Hn66EAC

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

 
Abhishek BansalAbhishek Bansal

Hi Filipe,

In Salesforce the "Id" field is a system generated field and we are not allowed to assign any value for this field. The id is automatically generated when the record is inserted in the system and after that we cannot change this.

If you have any lookup field of "Catalog_Line_Item__c" object in your produto2 object then you can simply use the below statement :
produto2.Catalog_Line_Item__c = ctlgLineItem.Id;(Catalog_Line_Item__c is the API name of lookup field)

Please let me know if you need more information or help on this.

Thanks,
Abhishek Bansal

Filipe BaradelliFilipe Baradelli
Hi Abhishek,

The produto2 is a Product2 and doesn't have the Catalog_Line_Item__c field. This part of the code is to get the product's data and pass to produto2 (public), which will allow me take this product and add it to a newly created Opportunity.

Here is my full code if you want to see : 
 
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 Product2 produto2 = new Product2();
    // 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 );
               }
        /*       for(OpportunityLineItem oppli : opplilist) {
                    productSet.add( oppli.Product2Id );
               }*/
            }
        }        
        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];
        
        for(Catalog_Order__c ctlgOrdr : listaproduto){
            for(Catalog_Line_Item__c ctlgLineItem : ctlgOrdr.Catalog_Line_Items__r){
                System.debug(ctlgLineItem.Product__c);       

                produto2.Id = ctlgOrdr.Id;
                produto2.Name = ctlgLineItem.Product_Name__c;
                produto2.Description = ctlgLineItem.Product_Short_Description__c;
                produto2.isActive = true;
            }
        }  

        // 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;
            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;
        }
        Catalog_Line_Item__c li = [SELECT Quantity__c FROM Catalog_Line_Item__c WHERE Id =: productId];
        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 Product
        Product2 pr = new Product2();
        pr.Name='PRODUTO TESTE';
        pr.isActive=true;
        insert pr;
        
       // produto2.isActive = true;
     //   insert produto2;
        
        // 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=produto2.Id;
        stdPriceBookEntry.Pricebook2Id=stdPriceBookRecId[0].Id;
        stdPriceBookEntry.UnitPrice=2000;
        stdPriceBookEntry.IsActive=true;
        insert stdPriceBookEntry;
        
        // Create Custom PriceBookEntry
        PriceBookEntry customPriceBookEntry = new PriceBookEntry();
        customPriceBookEntry.Product2Id=produto2.Id;
        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;  
        
        
        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');
    }
}

Thanks.
Abhishek BansalAbhishek Bansal

Hi Filipe,

As I already told you that we are not allowed to assign any value to Id field in salesforce so you have to comment out the line no. 200.
I am not aware of your current requirement  but you cannot assign any value to Id field so you have to remove this line.

Thanks,
Abhishek Bansal

Filipe BaradelliFilipe Baradelli
Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields missing : [Produto]: [Produto]
This error points to line 393, column 1:  insert stdPriceBookEntry;

The field Product__c is reference type and I didn't find a field from Product2 to accept the Product__c data (if it is the problem).
 
Abhishek BansalAbhishek Bansal
From the name of field it is clear that the Product__c is a custom lookup field in your Product2 object. This field is also made required so you are not allowed to insert Product2 record without a value in this field. If you are having any problem in finding this field on your object then you can share your credentials of the org so that i can have a look and provide you help. You can also contact me on gmail or Skype Gmail : abhibansal2790@gmail.com Skype : abhishek.bansal2790
Filipe BaradelliFilipe Baradelli
Product__c is a field from Catalog_Line_Item__c (this object is from a catalog package). I only found this object with its fields in the developer console (there is a field with the same name, Catalog_Line_Item__c, in Catalog_Order__c object). I tried to assign the field produto2.Name to field ctlgLineItem.Product__c, but didn't work (the error was the same).
What I need to do in this case (I'm sorry if you've told me and I'm asking again)?
Abhishek BansalAbhishek Bansal

Hi Filipe,

I am not completely sure but you can try with the below statement :

produto2.Product__c= ctlgLineItem.Product__c;

Thanks,
Abhishek

Filipe BaradelliFilipe Baradelli
Now the error is this: Error: Erro de compilação: Invalid field Product__c for SObject Product2 at line of the code produto2.Product__c= ctlgLineItem.Product__c;

Product__c is a field from Catalog_Line_Item__c so I can't assign it to produto2 (Product2).
 
Abhishek BansalAbhishek Bansal

Hi Filipe,

You can remove this line and as i already told you that i am not aware of your data structure so i can't help you much here.
If you want then you can contact me on Skype or Gmail.
Gmail : abhibansal2790@gmail.com
Skype : abhishek.bansal2790

Thanks,
Abhishek Bansal