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 

Message: FIELD_INTEGRITY_EXCEPTION

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?

 
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Dear Filepe,

It seemd 'idproduto ' varable does not hold a valid product id , Try using debug to chek which record id it holds.

Regards
Ashish
Filipe BaradelliFilipe Baradelli
I debug and the message by the developer console is the same. I tried to change the type of 'idproduto' from ID to String, but the error was the same again.