• Batmansfdc
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 4
    Replies
public without sharing class CreateContactController {

    @AuraEnabled
    public static Id createRecord (Contact contact){
        System.debug('Inserting contact with:' + contact.FirstName + contact.LastName + contact.Email);
        Id returnID = null;        
        if(contact != null){
      User u = [SELECT Id, Contact.AccountId FROM User WHERE Id = :UserInfo.getUserId()][0];
            contact.AccountId = u.Contact.AccountId;
            insert contact;
            returnId = contact.Id;
        }
        else {
            System.debug('Could not insert.');
        }
        return returnId;
    }

}
User u = [SELECT Id, Contact.AccountId FROM User WHERE Id = :UserInfo.getUserId()][0];
            contact.AccountId = u.Contact.AccountId;
            insert contact;
Been working on this Superbadge for over a week now and I'm currently stuck on Challenge 7 with getting "Ensure that values are preserved during pagination." after impimenting OrderExtension.

When looking at the logs, what's happening is the Quantity and UnitPrice values for the test data are being set to 2 when they should be 1 after calling GetPrevious().

Has this happened to anyone else who has overcome the issue and how can I correct this? Thanks.

OrderExtension Code:
/**
 * @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);
        }
        
        RefreshStandardSetController();
        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.Quantity * oi.UnitPrice;
            }
        }
        RefreshData();
    }

    //  Implement your own method(s) to populate orderItemList
    //  that you will call after pagination and/or family selection
    void RefreshStandardSetController() {
        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 LIMIT 14';
        
        standardSetController = new ApexPages.StandardSetController(Database.getQueryLocator(query));
        standardSetController.setPageSize(Constants.DEFAULT_ROWS);
    }
    
    void RefreshData() {
        orderItemList = new List<OrderItem>();
        
        for(SObject o : standardSetController.getRecords()) {
            PricebookEntry p = (PricebookEntry)o;
            
            if(orderItemMap.containsKey(p.Product2Id)) {
                orderItemList.add(orderItemMap.get(p.Product2Id));
            } else {
                orderItemList.add(new OrderItem(
	                    PricebookEntryId=p.Id,
	                    Product2Id=p.Product2Id,
	                    UnitPrice=p.UnitPrice,
	                    Quantity=0,
	                    Product2=p.Product2
	                ));
            }
        }
    }


    /**
     * @name OnFieldChange
     * @description
    **/
    public void OnFieldChange(){
        // Implement logic to store the values changed on the page
        //      and populate pieData
        //      and populate total
        for(OrderItem oi : orderItemList) {
            orderItemMap.put(oi.Product2Id, oi);
        }
        
        pieData = null;
        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.Quantity * oi.UnitPrice;
            }
        }
    }

    /**
     * @name SelectFamily
     * @description
    **/
    public void SelectFamily(){
        //Implement logic to filter based on the selected product family
        RefreshStandardSetController();
        RefreshData();
    }

    /**
     * @name Save
     * @description
    **/
    public void Save(){
        //Implement logic to save the Order and populated OrderItems
        System.Savepoint save = Database.setSavepoint();
        
        try {
            if(orderRecord.Pricebook2Id == null) {
                orderRecord.Pricebook2Id = Constants.STANDARD_PRICEBOOK_ID;
            }
            
            upsert orderRecord;
            
            List<OrderItem> toUpsert = new List<OrderItem>();
            List<orderItem> toDelete = new List<OrderItem>();
            
            for(OrderItem oi : orderItemMap.values()) {
                if(oi.Quantity > 0) {
                    if(oi.OrderId == null) {
                        oi.OrderId = orderRecord.Id;
                    }
                    toUpsert.add(oi);
                } else if (oi.Id != null) {
                    toDelete.add(new OrderItem(Id = oi.Id));
                    oi.Id = null;
                }
            }
            
            upsert toUpsert;
            delete toDelete;
        }
        catch (Exception e) {
            Database.rollback(save);
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,Constants.ERROR_MESSAGE));
        }

    }


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


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


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

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

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

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

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

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

    /**
     * @name GetFamilyOptions
     * @description
    **/
    public List<SelectOption> GetFamilyOptions(){
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption(Constants.SELECT_ONE, Constants.SELECT_ONE));
        for(Schema.PicklistEntry p : Constants.PRODUCT_FAMILY) {
            options.add(new SelectOption(p.getValue(), p.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
        ];
    }

    
}

And the little Anonymous Execution Code I run to validate that same assertion by the checker:
orderExtension ext = new orderExtension(new apexPages.standardController(new Order()));
ext.orderItemList[0].Quantity = 1;
ext.orderItemList[0].UnitPrice = 1;
ext.orderItemList[1].Quantity = 2;
ext.orderItemList[1].UnitPrice = 2;
system.debug('Quantity on Creation: ' + ext.orderItemList[0].Quantity);
system.debug('UnitPrice on Creation: ' + ext.orderItemList[0].UnitPrice);
system.debug('Quantity on Creation: ' + ext.orderItemList[1].Quantity);
system.debug('UnitPrice on Creation: ' + ext.orderItemList[1].UnitPrice);
ext.OnFieldChange();
system.debug('Quantity after Field Change: ' + ext.orderItemList[0].Quantity);
system.debug('UnitPrice after Field Change: ' + ext.orderItemList[0].UnitPrice);
system.debug('Quantity after Field Change: ' + ext.orderItemList[1].Quantity);
system.debug('UnitPrice after Field Change: ' + ext.orderItemList[1].UnitPrice);
ext.next();
system.debug('Quantity after Next: ' + ext.orderItemList[0].Quantity);
system.debug('UnitPrice after Next: ' + ext.orderItemList[0].UnitPrice);
system.debug('Quantity after Next: ' + ext.orderItemList[1].Quantity);
system.debug('UnitPrice after Next: ' + ext.orderItemList[1].UnitPrice);
system.assert(ext.orderItemList[0].Quantity != 1);
system.assert(ext.orderItemList[0].UnitPrice != 1);
system.assert(ext.orderItemList[1].Quantity != 2);
system.assert(ext.orderItemList[1].UnitPrice != 2);
ext.previous();
system.debug('Quantity after Previous: ' + ext.orderItemList[0].Quantity);
system.debug('UnitPrice after Previous: ' + ext.orderItemList[0].UnitPrice);
system.debug('Quantity after Previous: ' + ext.orderItemList[1].Quantity);
system.debug('UnitPrice after Previous: ' + ext.orderItemList[1].UnitPrice);
system.assert(ext.orderItemList[0].Quantity == 1);
system.assert(ext.orderItemList[0].UnitPrice == 1);
system.assert(ext.orderItemList[1].Quantity == 2);
system.assert(ext.orderItemList[1].UnitPrice == 2);

 
public without sharing class CreateContactController {

    @AuraEnabled
    public static Id createRecord (Contact contact){
        System.debug('Inserting contact with:' + contact.FirstName + contact.LastName + contact.Email);
        Id returnID = null;        
        if(contact != null){
      User u = [SELECT Id, Contact.AccountId FROM User WHERE Id = :UserInfo.getUserId()][0];
            contact.AccountId = u.Contact.AccountId;
            insert contact;
            returnId = contact.Id;
        }
        else {
            System.debug('Could not insert.');
        }
        return returnId;
    }

}
User u = [SELECT Id, Contact.AccountId FROM User WHERE Id = :UserInfo.getUserId()][0];
            contact.AccountId = u.Contact.AccountId;
            insert contact;