+ Start a Discussion
Shubham NandwanaShubham Nandwana 

Superbadge Advanced Apex Specialist : Error

Hello,
I am facing a problem with challenge 5 of the Superbadge. Error is:

Challenge Not yet complete... here's what's wrong: 
Ensure that you perform a query to verify the Save button works.

I have saved the 5 records by populating them and trying to work on the save button but unable to catch the error.
Below is my code of Product2Tests...Any help is appreciated.

@isTest (seeAllData=false)
private class Product2Tests {

    /**
     * @name product2Extension_UnitTest
     * @description UnitTest for product2Extension
    **/
    static TestMethod void Product2Extension_UnitTest(){
        Test.startTest();
           PageReference pageRef = Page.Product2New;
           Test.setCurrentPage(pageRef);
          
        
        ApexPages.StandardController controller =new Apexpages.StandardController(new Product2());
            Product2Extension ext = new Product2Extension(controller);
            ext.addRows();
            ext.save();
            System.assertEquals(Product2Extension.productsToInsert.size(), Constants.DEFAULT_ROWS);
        
         
         //if(addValue.equals('Add'))
                  //System.assertEquals(Product2Extension.productsToInsert.size(), 2*Constants.DEFAULT_ROWS);
        
        Integer i=0;
        for(Product2Extension.ProductWrapper pr: Product2Extension.productsToInsert){
            if (String.isBlank(pr.productRecord.Name) || pr.productRecord.Name==null){
                pr.productRecord.Name='Product'+i;
            }
               
            if(String.isBlank(pr.productRecord.Family) ||  pr.productRecord.Family==null ){
                pr.productRecord.Family='Side';
            }
                  
            if(pr.productRecord.IsActive==false){
                pr.productRecord.IsActive=true;
            }
            if(pr.productRecord.Initial_Inventory__c==null){
                pr.productRecord.Initial_Inventory__c=10;
            }
            i++;
                   
        }
        PageReference p=ext.save();
        
        System.assertEquals(Product2Extension.productsToInsert.size(), 5);
        System.debug(Product2Extension.productsToInsert.size());
        
        Test.stopTest();
    }

}
 
Best Answer chosen by Shubham Nandwana
NagendraNagendra (Salesforce Developers) 
Hi Shubham,

Please check with below code which might help you with the above issue.
@isTest 
    static void Product2Extension_UnitTest(){
        PageReference pageRef = Page.Product2New;
        Test.setCurrentPage(pageRef);
        Product2 prod = new Product2(name='Test',isActive=true);        
        ApexPages.StandardController stdcontroller = new ApexPages.StandardController(prod);        
        Product2Extension ext = new Product2Extension(stdcontroller);        
       	System.assertEquals(Constants.DEFAULT_ROWS, ext.productsToInsert.size());
        
        ext.addRows();
        System.assertEquals(Constants.DEFAULT_ROWS * 2, ext.productsToInsert.size());
        
        for (Integer i = 0; i < 5; i++) {
            Product2Extension.ProductWrapper wrapper = ext.productsToInsert[i];
            
            Product2 testProduct = new Product2();
            testProduct.Name = 'Test Product ' + i;
            testProduct.IsActive = true;
            testProduct.Initial_Inventory__c = 20;
            testProduct.Family = Constants.PRODUCT_FAMILY[0].getValue();
            wrapper.productRecord = testProduct;
            
            PricebookEntry testEntry = new PricebookEntry();
            testEntry.IsActive = true;
            testEntry.UnitPrice = 10;
            wrapper.pricebookEntryRecord = testEntry;
        }
        
        Test.startTest();
        ext.save();
        Test.stopTest();
        
        ext.GetFamilyOptions();
        ext.GetInventory();
        List<Product2> createdProducts = [
            SELECT
            	Id
            FROM
           		Product2
        ];
        System.assertEquals(5, createdProducts.size());
    }
Hope this helps.

Kindly mark this as solved if the information was helpful.

Thanks,
Nagendra
 

All Answers

NagendraNagendra (Salesforce Developers) 
Hi Shubham,

Please check with below code which might help you with the above issue.
@isTest 
    static void Product2Extension_UnitTest(){
        PageReference pageRef = Page.Product2New;
        Test.setCurrentPage(pageRef);
        Product2 prod = new Product2(name='Test',isActive=true);        
        ApexPages.StandardController stdcontroller = new ApexPages.StandardController(prod);        
        Product2Extension ext = new Product2Extension(stdcontroller);        
       	System.assertEquals(Constants.DEFAULT_ROWS, ext.productsToInsert.size());
        
        ext.addRows();
        System.assertEquals(Constants.DEFAULT_ROWS * 2, ext.productsToInsert.size());
        
        for (Integer i = 0; i < 5; i++) {
            Product2Extension.ProductWrapper wrapper = ext.productsToInsert[i];
            
            Product2 testProduct = new Product2();
            testProduct.Name = 'Test Product ' + i;
            testProduct.IsActive = true;
            testProduct.Initial_Inventory__c = 20;
            testProduct.Family = Constants.PRODUCT_FAMILY[0].getValue();
            wrapper.productRecord = testProduct;
            
            PricebookEntry testEntry = new PricebookEntry();
            testEntry.IsActive = true;
            testEntry.UnitPrice = 10;
            wrapper.pricebookEntryRecord = testEntry;
        }
        
        Test.startTest();
        ext.save();
        Test.stopTest();
        
        ext.GetFamilyOptions();
        ext.GetInventory();
        List<Product2> createdProducts = [
            SELECT
            	Id
            FROM
           		Product2
        ];
        System.assertEquals(5, createdProducts.size());
    }
Hope this helps.

Kindly mark this as solved if the information was helpful.

Thanks,
Nagendra
 
This was selected as the best answer
Shubham NandwanaShubham Nandwana

Hi Nagendra,

Thank you so much for your answer.

I have completed the challenge. Just added the below query in my code and it worked well

 List<Product2> queryResults=[Select Name,IsActive,Initial_Inventory__c from Product2 where Initial_Inventory__c=15 and IsActive= true ];

I cannot verify your answer as the challenge is completed but I think it will work fine.

Thank you once again.