+ Start a Discussion
Certified sfdcCertified sfdc 

Ensure that the PricebookEntries are saved on the standard Pricebook.

HI All,

I am trying to complete PD 2 Superbadge (Advanced Apex Specialist), I have tested every product and pricebookentry records, still i am not able to complete challenge.

Update the new product Visualforce page(Update the Product2New Visualforce page and Product2Extension class to meet the business requirements.)

Challenge Not yet complete... here's what's wrong: 
Ensure that the PricebookEntries are saved on the standard Pricebook.

please suggest. 
Thanks
Best Answer chosen by Certified sfdc
Certified sfdcCertified sfdc
Hi Pablo,

p.pricebookentryrecord.Pricebook2Id = constants.STANDARD_PRICEBOOK_ID;
and
ApexPages.addMessage(new ApexPages.Message( ApexPages.Severity.ERROR,constants.ERROR_MESSAGE));

resolved both issues

Thanks

All Answers

Pablo Gonzalez Alvarez 3Pablo Gonzalez Alvarez 3
When the Save button is clicked, you need to 
  1. Validate that all the fields have a value. If all the fields have a value, add the products to a list, and insert the list
for(ProductWrapper pw : productsToInsert){
                if(String.isEmpty(pw.productRecord.Name) || String.isEmpty(pw.productRecord.Family)
                  || pw.pricebookEntryRecord.UnitPrice == null || pw.productRecord.Initial_Inventory__c == null
                  || pw.productRecord.IsActive == false){
                       //do nothing with these ones
                   }else{
                       prodToInsert.add(pw.productRecord);
                   }
2. Then relate the products to a pricebook, by creating pricebook entries
 
pw.pricebookEntryRecord.Pricebook2Id = Constants.STANDARD_PRICEBOOK_ID;
                       pw.pricebookEntryRecord.Product2Id = pw.productRecord.Id;
                       //pw.pricebookEntryRecord.UnitPrice = Decimal.valueOf(pw.UnitPriceValue);
                       pbesToInsert.add(pw.pricebookEntryRecord);
Then insert the list of pricebook entries. 

Please paste your code so that we can suggest how to move forward. 
Pablo Gonzalez Alvarez 3Pablo Gonzalez Alvarez 3
So does that work? Do the same check for null values before populating the pricebook entry details. 
Certified sfdcCertified sfdc
Hi Pablo,

Here is my wrapper
public class productWrapper{
        public product2 productRecord {get;set;}
        public pricebookEntry pricebookEntryRecord{get;set;}
        public productWrapper(){
            productRecord = new product2(Initial_Inventory__c =0);
            pricebookEntryRecord = new pricebookEntry(Unitprice=0.0);
        }
    }
Certified sfdcCertified sfdc
Hi Pablo,

p.pricebookentryrecord.Pricebook2Id = constants.STANDARD_PRICEBOOK_ID;
and
ApexPages.addMessage(new ApexPages.Message( ApexPages.Severity.ERROR,constants.ERROR_MESSAGE));

resolved both issues

Thanks
This was selected as the best answer