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
Ragnar Lothbrok 7Ragnar Lothbrok 7 

You can't select products until you've chosen a price book for this opportunity on the products related list.: []

Hi,
I am trying to insert products from lwc component to opportunity object from community. I have a component where there is an option for searching the product based on input and then there is "SAVE" button onclick() it saves the selected product to opportunity. So while searching the product the code is like : 
Select Id,Name,ProductID__c,OldMaterialNumber__c From Product2 where Name like : searchTxt With SECURITY_ENFORCED LIMIT 1000

and for saving the product code is like:
public static List<OpportunityLineItem> opportunityProductCreation(Id oppId, List<OpportunityLineItemWrapper> oliData){
        List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
        id oppRecordId = oppId;
        try {
            if(oliData.size()>0){  
                system.debug('Whole wrapper data print'+oliData);
                system.debug('incoming Opp ID'+oppId);
                for(OpportunityLineItemWrapper oli: oliData){
                    system.debug('Single data at once'+oli);
                    OpportunityLineItem oppLineItem = new OpportunityLineItem();
                    oppLineItem.product2Id          =   oli.selectedProductId;
                    oppLineItem.Opportunityid       =   oppRecordId;
                    oppLineItem.Quantity            =   decimal.valueOf(oli.orderQuantity);
                    oppLineItem.TargetPrice1__c =   decimal.valueOf(oli.targetCost);
                    oppLineItem.ResalePrice__c      =   decimal.valueOf(oli.resalePrice);
                    oppLineItem.CompetitorName__c = oli.competitor;
                    oppLineItem.PriceUnit__c    =   decimal.valueOf(oli.perUnit);
                    oppLineItem.Price1__c     =   decimal.valueOf(oli.Price);
                    oliList.add(oppLineItem);
                }
                system.debug('check all OPPLINEITEM data before insertion');
                insert oliList;
            }
            return oliList;
        } catch (Exception e) {
            system.debug('Error Msg-- ' + e.getMessage() + 'Error Line No-- ' + e.getLineNumber());
            SC_ExceptionLogHandler.createExceptionLog(e,'pdrPartsComponentCls');
            throw new AuraHandledException(e.getMessage());
        }

    }

So here while inserting Data i am recieving this error: 
Insert failed. 
    First exception on row 0; first error: REQUIRED_FIELD_MISSING, 
    Error: You can't select products until you've chosen a price book for this opportunity on the products related list.: []

So can anyone help me what i have to do inorder to save the opportunity from community side.
Thanks.
sooraj kesavadassooraj kesavadas
You need to populate the Pricebook on the Opportunity before you can add an Opportunity Line Item.

Pricebook2Id = [select Id from Pricebook2 where name = ''].id
and the populate that on the Opportunity.Pricebook2id field,s ave the opportunity before trying to insert OpportunityLineItem