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
Rocco SorrentinoRocco Sorrentino 

Advanced Apex Superbadge (PDII) Step 7

Hi all,

I'm getting an error while verifying step 7:
  • Challenge Not yet complete... here's what's wrong:  Ensure that you implement all the pagination methods using the corresponding StandardSetController methods.
I implemented the class using the StandardSetController methods but it doesn't work.

Any advice or suggestion?
Thank you.
Best Answer chosen by Rocco Sorrentino
Zachery EngmanZachery Engman

I would guess perhaps you have a small typo or wrong method in one of them.  Here is a sample of my methods that got passed that error:

public void First(){
        standardSetController.first();
        GetOrderItems();
    }

public void Next(){
        standardSetController.next();
        GetOrderItems();
    }

....

It sounds like you are doing the exact same as it sounds from your post, maybe just double check for a typo/wrong method.

All Answers

Zachery EngmanZachery Engman

I would guess perhaps you have a small typo or wrong method in one of them.  Here is a sample of my methods that got passed that error:

public void First(){
        standardSetController.first();
        GetOrderItems();
    }

public void Next(){
        standardSetController.next();
        GetOrderItems();
    }

....

It sounds like you are doing the exact same as it sounds from your post, maybe just double check for a typo/wrong method.
This was selected as the best answer
Rocco SorrentinoRocco Sorrentino
Hi Zachery,

yes it works.
I changed the variable from stanCon to standardSetController.

Thank you.
Abhishek-tandonAbhishek-tandon
Hi Zachery/Rocco,

Could you share your order extension class, my pagination is not working fine, also in case of edit, do we need show existing product only?
AMIT SINGH 2375AMIT SINGH 2375
Hi Zachry,

I am getting below Error 
Challenge Not yet complete... here's what's wrong: 
Ensure that you implement all the pagination methods using the corresponding StandardSetController methods.

Below is the class that I am using
/**
 * @name OrderExtension
 * @description This class is provided for you to facilitate the Super Badge
**/
public class OrderExtension {

    public Order orderRecord {get;set;}
    public List<OrderItem> orderItemList {get;set;}
    public String selectedFamily {get;set;}
    public List<chartHelper.chartData> pieData {get;set;}
    public Decimal total {get;set;}

    public Map<Id,OrderItem> orderItemMap;
    ApexPages.StandardSetController standardSetController;

    public OrderExtension(ApexPages.StandardController standardController){
        orderRecord = (Order)standardController.getRecord();
        orderItemMap = new Map<id,OrderItem>();
        if ( orderRecord.Id != null ){
            orderRecord = queryOrderRecord(orderRecord.Id);
        }

        resetSsc();
        total = 0;

        for (OrderItem oi : orderRecord.OrderItems) {
            orderItemMap.put(oi.Product2Id, oi);
            if (oi.Quantity > 0) {
                if (null == pieData) {
                    pieData = new List<ChartHelper.ChartData>();
                }
                pieData.add(new chartHelper.ChartData(oi.Product2.Name, oi.Quantity * oi.UnitPrice));
                total += oi.UnitPrice * oi.Quantity;
            }
        }
        PopulateOrderItems();
    }

    void resetSsc() {
        String query = 'SELECT Name, Product2.Family, Product2.Name, Product2Id, UnitPrice, Product2.Quantity_Remaining__c'
                     + '  FROM PricebookEntry WHERE IsActive = TRUE';

        if (selectedFamily != null && selectedFamily != Constants.SELECT_ONE) {
            query += ' AND Product2.Family = \'' + selectedFamily + '\'';
        }
        query += ' ORDER BY Name';
        IF(standardSetController == null ){
            standardSetController = new ApexPages.StandardSetController(Database.getQueryLocator(query));
            standardSetController.setPageSize(Constants.DEFAULT_ROWS);
        }
        
    }

    //ToDo: Implement your own method to populate orderItemList
    //  that you will call after pagination and/or family selection
    void PopulateOrderItems() {
        orderItemList = new List<OrderItem>();
        for (SObject obj : standardSetController.getRecords()) {
            PricebookEntry pbe = (PricebookEntry)obj;

            if (orderItemMap.containsKey(pbe.Product2Id)) {
                orderItemList.add(orderItemMap.get(pbe.Product2Id));
            } else {
                orderItemList.add(new OrderItem(
                    PricebookEntryId=pbe.Id,
                    Product2Id=pbe.Product2Id,
                    UnitPrice=pbe.UnitPrice,
                    Quantity=0,
                    Product2=pbe.Product2
                ));
            }
        }
    }


    /**
     * @name OnFieldChange
     * @description
    **/
    public void OnFieldChange(){
        //ToDo: Implement logic to store the values changed on the page
        for (OrderItem oi : orderItemList) {
            orderItemMap.put(oi.Product2Id, oi);
        }

        //      and populate pieData
        pieData = null;
        total = 0;
        for (OrderItem oi : orderItemMap.values()) {
            if (oi.Quantity > 0) {
                if (null == pieData) {
                    pieData = new List<chartHelper.ChartData>();
                }
                pieData.add(new chartHelper.ChartData(oi.Product2.Name, oi.Quantity * oi.UnitPrice));
                //      and populate total
                total += oi.UnitPrice * oi.Quantity;
            }

        }

    }

    /**
     * @name SelectFamily
     * @description
    **/
    public void SelectFamily(){
        //ToDo: Implement logic to filter based on the selected product family
        resetSsc();
        PopulateOrderItems();
    }

    /**
     * @name Save
     * @description
    **/
    public void Save(){
        //ToDo: Implement logic to save the Order and populated OrderItems
        System.Savepoint sp = Database.setSavepoint();
        try {
            if (null == orderRecord.Pricebook2Id) {
                orderRecord.Pricebook2Id = Constants.STANDARD_PRICEBOOK_ID;
            }
            upsert orderRecord;

            List<OrderItem> orderItemsToUpsert = new List<OrderItem>();
            List<OrderItem> orderItemsToDelete = new List<OrderItem>();
            for (OrderItem oi : orderItemMap.values()) {
                if (oi.Quantity > 0) {
                    if (null == oi.OrderId) {
                        oi.OrderId = orderRecord.Id;
                    }
                    orderItemsToUpsert.add(oi);
                } else if (oi.Id != null) {
                    orderItemsToDelete.add(oi);
                }
            }

            upsert orderItemsToUpsert;
            delete orderItemsToDelete;
        } catch (Exception e) {
            Database.rollback(sp);
            apexPages.addMessage(new ApexPages.message(ApexPages.Severity.INFO,Constants.ERROR_MESSAGE));
        }
    }


    /**
     * @name First
     * @description
    **/
    public void First(){
        standardSetController.first();
        PopulateOrderItems();
    }


    /**
     * @name Next
     * @description
    **/
    public void Next(){
        standardSetController.next();
        PopulateOrderItems();
    }


    /**
     * @name Previous
     * @description
    **/
    public void Previous(){
        standardSetController.previous();
        PopulateOrderItems();
    }

    /**
     * @name Last
     * @description
    **/
    public void Last(){
        standardSetController.last();
        PopulateOrderItems();
    }

    /**
     * @name GetHasPrevious
     * @description
    **/
    public Boolean GetHasPrevious(){
        return standardSetController.getHasPrevious();
    }

    /**
     * @name GetHasNext
     * @description
    **/
    public Boolean GetHasNext(){
        return standardSetController.getHasNext();
    }

    /**
     * @name GetTotalPages
     * @description
    **/
    public Integer GetTotalPages(){
        return (Integer)Math.ceil(standardSetController.getResultSize() / (Decimal)Constants.DEFAULT_ROWS);
    }

    /**
     * @name GetPageNumber
     * @description
    **/
    public Integer GetPageNumber(){
        return standardSetController.getPageNumber();
    }

    /**
     * @name GetFamilyOptions
     * @description
    **/
    public List<SelectOption> GetFamilyOptions() {
        List<SelectOption> options = new List<SelectOption>{
            new SelectOption(Constants.SELECT_ONE, Constants.SELECT_ONE)
        };

        for (Schema.PicklistEntry ple : Constants.PRODUCT_FAMILY) {
            options.add(new SelectOption(ple.getValue(), ple.getLabel()));
        }
        return options;
    }

    /**
     * @name QueryOrderRecord
     * @description
    **/
    public static Order QueryOrderRecord(Id orderId){
        return [
            SELECT Id, AccountId, EffectiveDate, Name, Status, Pricebook2Id,
                (
                    SELECT Id, OrderId, Quantity, UnitPrice, PricebookEntryId, Product2Id,
                         Product2.Name, Product2.Family, Product2.Quantity_Remaining__c
                    FROM OrderItems
                )
            FROM Order
            WHERE Id = :orderId
        ];
    }

}

 
Jaap ScheperJaap Scheper
Hi Amit, did you find out what went wrong? I am stuck at the same problem.
Amit Singh 1Amit Singh 1
Hi Jaap,

Yes, Please make sure you do not have more than 15 Products in the Org where you are testing the same
Jaap ScheperJaap Scheper

Thanks Amit.

I solved it by adding LIMIT 14 in the query that I pass in to 

standardSetController = new ApexPages.StandardSetController(Database.getQueryLocator(query));

Now there are only three pages, to :|
AYANAYAN
Thanks Amit & Jaap
Rajat Jain 1Rajat Jain 1
@Amit - put the below line in SelectFamily() method to reset the standardSetCotroller() every time we change the product family dropdown value.

standardSetController = null;
Parjanya RoyParjanya Roy
Okay, so I was finally able to get past challenge 7 . Here are a few pointers :

1.  I was able to complete the challenge with 8 active price book entries. [ I had initially not set price-book record isActive status to active/inactive while writing the save method for Product2Extension.apxc. I changed that to new PricebookEntry(UnitPrice=ppr.getPriceBookRecord().UnitPrice,PriceBook2Id=Constants.STANDARD_PRICEBOOK_ID,isActive=ppr.getProductRecord().IsActive)
2. Once you get the above working chances are that the pagination error goes away. (If it doesn't, add plenty of debug in the GetTotalPages method to understand what is happening.

public Integer GetTotalPages(){
           System.debug('standardSetController.getResultSize()'+standardSetController.getResultSize());
           System.debug('(Decimal)Constants.DEFAULT_ROWS'+(Decimal)Constants.DEFAULT_ROWS);
        Integer totalPages = (Integer)Math.ceil(standardSetController.getResultSize() / (Decimal)Constants.DEFAULT_ROWS);
        System.debug('############ totalPages: ' + totalPages);
        return totalPages;
    }

3. You might be facing some "Assertion Failed" errors after this. It has to do with your org data (I have set quantity and unit price as '1' for the second page which was precisely what I shouldn't have set to pass the test :P ). I suggest opening the raw log, copy-pasting the apex in an anonymous window and executing it. It would help you locate the exact assert where its failing. 

I hope the above helps!
 
Anuja Patil 10Anuja Patil 10
Hi Parjanya,can you share the orderextension controller?
SForceBeWithYouSForceBeWithYou
It expects you to have between 11 and 15 Pricebook Entries, because the getTotalPages assertion is always checking for 3. ¯\_(ツ)_/¯

 
orderExtension ext = new orderExtension(new apexPages.standardController(new Order())); system.assert( ext.gethasNext() == true , 'ext.getHasNext() did not return the expected result.' ); system.assert( ext.gethasPrevious() == false, 'ext.gethasPrevious() did not return the expected result.' ); system.assert( ext.getTotalPages() == 3, 'ext.getTotalPages() did not return the expected result.' ); system.assert( ext.getPageNumber() == 1, 'ext.getPageNumber() did not return the expected result.' ); ext.next(); system.assert( ext.gethasNext() == true, 'ext.getHasNext() did not return the expected result.' ); system.assert( ext.gethasPrevious() == true, 'ext.gethasPrevious() did not return the expected result.' ); system.assert( ext.gettotalPages() == 3, 'ext.getTotalPages() did not return the expected result.' ); system.assert( ext.getPageNumber() == 2, 'ext.getPageNumber() did not return the expected result.' ); ext.last(); system.assert( ext.gethasNext() == false, 'ext.getHasNext() did not return the expected result.' ); system.assert( ext.gethasPrevious() == true, 'ext.gethasPrevious() did not return the expected result.' ); system.assert( ext.gettotalPages() == 3, 'ext.getTotalPages() did not return the expected result.' ); system.assert( ext.getPageNumber() == 3, 'ext.getPageNumber() did not return the expected result.' ); ext.previous(); system.assert( ext.gethasNext() == true, 'ext.getHasNext() did not return the expected result.' ); system.assert( ext.gethasPrevious() == true, 'ext.gethasPrevious() did not return the expected result.' ); system.assert( ext.gettotalPages() == 3, 'ext.getTotalPages() did not return the expected result.' ); system.assert( ext.getPageNumber() == 2, 'ext.getPageNumber() did not return the expected result.' ); ext.first(); system.assert( ext.gethasNext() == true, 'ext.getHasNext() did not return the expected result.' ); system.assert( ext.gethasPrevious() == false, 'ext.gethasPrevious() did not return the expected result.' ); system.assert( ext.gettotalPages() == 3, 'ext.getTotalPages() did not return the expected result.' ); system.assert( ext.getPageNumber() == 1, 'ext.getPageNumber() did not return the expected result.' );

 
Bhavesh Chaudhari 6Bhavesh Chaudhari 6
Getting this error, someone please help me to solve Chellenge 8..


Getting this error
 
Aishwarya Shivhare 6Aishwarya Shivhare 6
Hi All, I deleted a few PricebookEntry Records and I worked. Thanks!
Aishwarya Shivhare 6Aishwarya Shivhare 6
Hi All, I deleted a few PricebookEntry Records and it worked. Thanks!
Nihar Ali 6Nihar Ali 6
Challenge Not yet complete... here's what's wrong:
Ensure that values are preserved during pagination
.User-added image

someone please help me regar this thanks in advance.
@Bhavesh Chaudhari 6
Ramesh Babu 134Ramesh Babu 134
Please Add all the products manually in classic. 
santhosh konathala 26santhosh konathala 26
Hi Ramesh,
Can u suggest how can we avoid this error "Ensure that values are preserved during pagination."?