Salesforce Products without PriceBookEntry

Salesforce Products without PriceBookEntry

SOQL to find Products without Price Book Entries:

SELECT Id, ProductCode 
FROM Product2 
WHERE Id NOT IN (
SELECT Product2Id 
    FROM PricebookEntry 
    WHERE Pricebook2Id = '01sSB000006IC45YAG'
) 

Sample Apex Code to create PriceBookEntry for Products:

List < PriceBookEntry > listPBEs = new List < PriceBookEntry >();

for( Product2 objProduct : [
    SELECT Id, ProductCode 
    FROM Product2 
    WHERE Id NOT IN (
        SELECT Product2Id 
        FROM PriceBookEntry 
        WHERE PriceBook2Id = '01sSB000006IC45YAG'
    )
    LIMIT 500
] ) {
    
    PriceBookEntry objPBE = new PriceBookEntry();
    objPBE.IsActive = true;
    objPBE.UnitPrice = 123;
    objPBE.Product2Id = objProduct.Id;
    objPBE.PriceBook2Id = '01sSB000006IC45YAG';
    listPBEs.add( objPBE );
    
}

if ( listPBEs.size() > 0 ) {
    
    insert listPBEs;
    
}

Leave a Reply