- santhosh konathala 26
- NEWBIE
- 15 Points
- Member since 2023
-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
3Questions
-
6Replies
Any one can help
Geeting an error like "system.asset failed"
Below is my class:
/**
* @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);
}
loadInitialOrderItemData();
loadPriceBookentry();
}
public void loadPriceBookentry() {
String query = 'SELECT Name, Id, Pricebook2Id, Product2Id, UnitPrice, Product2.Name,Product2.Quantity_Remaining__c, Product2.Family FROM PricebookEntry WHERE isActive= true';
if (String.isNotBlank(selectedFamily) && selectedFamily != Constants.SELECT_ONE ) {
query += ' AND Product2.Family = \''+ selectedFamily + '\'';
}
/*if (orderRecord.Id != null) {
query += ' AND Product2Id in (select Product2Id from OrderItem where orderId= \''+ orderRecord.Id + '\')';
}*/
standardSetController = new ApexPages.StandardSetController(Database.getQueryLocator(query));
standardSetController.setPageSize(Constants.DEFAULT_ROWS);
loadOrderItem();
}
public void loadOrderItem() {
orderItemList = new List<OrderItem>();
if(standardSetController.getRecords()!=null && standardSetController.getRecords().size() > 0) {
for (SObject obj : standardSetController.getRecords()) {
PricebookEntry entry = (PricebookEntry)obj;
if(orderItemMap.containsKey(entry.product2Id)) {
orderItemList.add(orderItemMap.get(entry.product2Id));
} else{
OrderItem item = new OrderItem(
Quantity = 0,
PricebookEntryId = entry.Id,
Product2Id = entry.Product2Id,
UnitPrice = entry.UnitPrice,
Product2 = entry.Product2
);
orderItemList.add(item);
orderItemMap.put(entry.Product2Id, item);
}
}
}
}
public void loadInitialOrderItemData() {
for(OrderItem item : orderRecord.OrderItems) {
orderItemMap.put(item.Product2Id, item);
}
loadChartData();
}
public void loadChartData() {
this.total = 0;
pieData = new List<ChartHelper.ChartData>();
for(OrderItem item : orderItemMap.values()) {
pieData.add(new ChartHelper.ChartData(item.Product2.Name, (item.UnitPrice * item.Quantity)));
total = total + (item.UnitPrice*item.Quantity);
}
}
//ToDo: Implement your own method to populate orderItemList
// that you will call after pagination and/or family selection
/**
* @name OnFieldChange
* @description
**/
public void OnFieldChange(){
//ToDo: Implement logic to store the values changed on the page
// and populate pieData
// and populate total
for(OrderItem item : orderItemList){
orderItemMap.put(item.Product2Id, item);
}
loadChartData();
}
/**
* @name SelectFamily
* @description
**/
public void SelectFamily(){
//ToDo: Implement logic to filter based on the selected product family
loadPriceBookentry();
}
/**
* @name Save
* @description
**/
public void Save(){
//ToDo: Implement logic to save the Order and populated OrderItems
/*System.Savepoint sp = Database.setSavepoint();
try {
if(orderRecord.Pricebook2Id == null) {
orderRecord.Pricebook2Id = Constants.STANDARD_PRICEBOOK_ID;
}
upsert orderRecord;
List<OrderItem> insertItems = new List<OrderItem>();
List<OrderItem> deleteItems = new List<OrderItem>();
for (OrderItem item : orderItemMap.values()) {
if(item.Quantity > 0) {
if(item.OrderId == null) {
item.OrderId = orderRecord.Id;
}
insertItems.add(item);
} else if(item.Id != null) {
OrderItem newItem = new OrderItem();
newItem.id=item.Id;
deleteItems.add(newItem);
item.Id = null;
}
}
upsert insertItems;
delete deleteItems;
} catch (Exception e){
Database.rollback(sp);
apexPages.addMessage(new ApexPages.message(ApexPages.Severity.INFO,Constants.ERROR_MESSAGE));
}*/
System.Savepoint sp = Database.setSavepoint();
try {
if(orderRecord.Pricebook2Id == null) {
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(oi.OrderId == null) {
oi.OrderId = orderRecord.Id;
}
orderItemsToUpsert.add(oi);
} else if(oi.Id != null) {
orderItemsToDelete.add(new OrderItem(id=oi.Id));
oi.Id = null;
}
}
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();
loadOrderItem();
}
/**
* @name Next
* @description
**/
public void Next(){
standardSetController.next();
loadOrderItem();
}
/**
* @name Previous
* @description
**/
public void Previous(){
standardSetController.previous();
loadOrderItem();
}
/**
* @name Last
* @description
**/
public void Last(){
standardSetController.last();
loadOrderItem();
}
/**
* @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
];
}
}
Below is my Test class:
@isTest (seeAllData=false)
private class OrderTests {
@testSetup
static void SetupTestData() {
TestDataFactory.InsertTestData(20);
}
static testmethod void OrderUpdate_UnitTest() {
Order selectedOrder = [Select name,Status, Id from Order limit 1];
Product2 oldProd = [Select Quantity_Ordered__c, Name, Id from Product2 limit 1];
selectedOrder.Status = Constants.ACTIVATED_ORDER_STATUS;
update selectedOrder;
Product2 updatedProd = [Select Quantity_Ordered__c, Name, Id from Product2 limit 1];
TestDataFactory.VerifyQuantityOrdered(oldProd,updatedProd,Constants.DEFAULT_ROWS);
}
static testmethod void OrderExtension_UnitTest() {
PageReference reference = Page.OrderEdit;
Test.setCurrentPage(reference);
Order CurOrder = [Select Id,Status from Order limit 1];
ApexPages.StandardController controller = new Apexpages.StandardController(CurOrder);
OrderExtension extension = new OrderExtension(controller);
//System.assertEquals(5, extension.orderItemList.size());
extension.selectedFamily = 'Dessert';
extension.SelectFamily();
extension.OnFieldChange();
extension.First();
extension.Next();
extension.Previous();
extension.Last();
extension.GetHasNext();
extension.GetPageNumber();
extension.GetHasPrevious();
extension.GetTotalPages();
extension.GetFamilyOptions();
extension.Save();
ChartHelper.GetInventory();
}
}
Below is my class:
/**
* @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);
}
loadInitialOrderItemData();
loadPriceBookentry();
}
public void loadPriceBookentry() {
String query = 'SELECT Name, Id, Pricebook2Id, Product2Id, UnitPrice, Product2.Name,Product2.Quantity_Remaining__c, Product2.Family FROM PricebookEntry WHERE isActive= true';
if (String.isNotBlank(selectedFamily) && selectedFamily != Constants.SELECT_ONE ) {
query += ' AND Product2.Family = \''+ selectedFamily + '\'';
}
/*if (orderRecord.Id != null) {
query += ' AND Product2Id in (select Product2Id from OrderItem where orderId= \''+ orderRecord.Id + '\')';
}*/
standardSetController = new ApexPages.StandardSetController(Database.getQueryLocator(query));
standardSetController.setPageSize(Constants.DEFAULT_ROWS);
loadOrderItem();
}
public void loadOrderItem() {
orderItemList = new List<OrderItem>();
if(standardSetController.getRecords()!=null && standardSetController.getRecords().size() > 0) {
for (SObject obj : standardSetController.getRecords()) {
PricebookEntry entry = (PricebookEntry)obj;
if(orderItemMap.containsKey(entry.product2Id)) {
orderItemList.add(orderItemMap.get(entry.product2Id));
} else{
OrderItem item = new OrderItem(
Quantity = 0,
PricebookEntryId = entry.Id,
Product2Id = entry.Product2Id,
UnitPrice = entry.UnitPrice,
Product2 = entry.Product2
);
orderItemList.add(item);
orderItemMap.put(entry.Product2Id, item);
}
}
}
}
public void loadInitialOrderItemData() {
for(OrderItem item : orderRecord.OrderItems) {
orderItemMap.put(item.Product2Id, item);
}
loadChartData();
}
public void loadChartData() {
this.total = 0;
pieData = new List<ChartHelper.ChartData>();
for(OrderItem item : orderItemMap.values()) {
pieData.add(new ChartHelper.ChartData(item.Product2.Name, (item.UnitPrice * item.Quantity)));
total = total + (item.UnitPrice*item.Quantity);
}
}
//ToDo: Implement your own method to populate orderItemList
// that you will call after pagination and/or family selection
/**
* @name OnFieldChange
* @description
**/
public void OnFieldChange(){
//ToDo: Implement logic to store the values changed on the page
// and populate pieData
// and populate total
for(OrderItem item : orderItemList){
orderItemMap.put(item.Product2Id, item);
}
loadChartData();
}
/**
* @name SelectFamily
* @description
**/
public void SelectFamily(){
//ToDo: Implement logic to filter based on the selected product family
loadPriceBookentry();
}
/**
* @name Save
* @description
**/
public void Save(){
//ToDo: Implement logic to save the Order and populated OrderItems
/*System.Savepoint sp = Database.setSavepoint();
try {
if(orderRecord.Pricebook2Id == null) {
orderRecord.Pricebook2Id = Constants.STANDARD_PRICEBOOK_ID;
}
upsert orderRecord;
List<OrderItem> insertItems = new List<OrderItem>();
List<OrderItem> deleteItems = new List<OrderItem>();
for (OrderItem item : orderItemMap.values()) {
if(item.Quantity > 0) {
if(item.OrderId == null) {
item.OrderId = orderRecord.Id;
}
insertItems.add(item);
} else if(item.Id != null) {
OrderItem newItem = new OrderItem();
newItem.id=item.Id;
deleteItems.add(newItem);
item.Id = null;
}
}
upsert insertItems;
delete deleteItems;
} catch (Exception e){
Database.rollback(sp);
apexPages.addMessage(new ApexPages.message(ApexPages.Severity.INFO,Constants.ERROR_MESSAGE));
}*/
System.Savepoint sp = Database.setSavepoint();
try {
if(orderRecord.Pricebook2Id == null) {
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(oi.OrderId == null) {
oi.OrderId = orderRecord.Id;
}
orderItemsToUpsert.add(oi);
} else if(oi.Id != null) {
orderItemsToDelete.add(new OrderItem(id=oi.Id));
oi.Id = null;
}
}
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();
loadOrderItem();
}
/**
* @name Next
* @description
**/
public void Next(){
standardSetController.next();
loadOrderItem();
}
/**
* @name Previous
* @description
**/
public void Previous(){
standardSetController.previous();
loadOrderItem();
}
/**
* @name Last
* @description
**/
public void Last(){
standardSetController.last();
loadOrderItem();
}
/**
* @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
];
}
}
Below is my Test class:
@isTest (seeAllData=false)
private class OrderTests {
@testSetup
static void SetupTestData() {
TestDataFactory.InsertTestData(20);
}
static testmethod void OrderUpdate_UnitTest() {
Order selectedOrder = [Select name,Status, Id from Order limit 1];
Product2 oldProd = [Select Quantity_Ordered__c, Name, Id from Product2 limit 1];
selectedOrder.Status = Constants.ACTIVATED_ORDER_STATUS;
update selectedOrder;
Product2 updatedProd = [Select Quantity_Ordered__c, Name, Id from Product2 limit 1];
TestDataFactory.VerifyQuantityOrdered(oldProd,updatedProd,Constants.DEFAULT_ROWS);
}
static testmethod void OrderExtension_UnitTest() {
PageReference reference = Page.OrderEdit;
Test.setCurrentPage(reference);
Order CurOrder = [Select Id,Status from Order limit 1];
ApexPages.StandardController controller = new Apexpages.StandardController(CurOrder);
OrderExtension extension = new OrderExtension(controller);
//System.assertEquals(5, extension.orderItemList.size());
extension.selectedFamily = 'Dessert';
extension.SelectFamily();
extension.OnFieldChange();
extension.First();
extension.Next();
extension.Previous();
extension.Last();
extension.GetHasNext();
extension.GetPageNumber();
extension.GetHasPrevious();
extension.GetTotalPages();
extension.GetFamilyOptions();
extension.Save();
ChartHelper.GetInventory();
}
}
- santhosh konathala 26
- August 23, 2023
- Like
- 0
Can any one help me below code getting an error like "Ensure that values are preserved during pagination."
Below is my 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);
}
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';
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();
standardSetController = null;
}
/**
* @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 : orderItemList) {
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
];
}
}
/**
* @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';
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();
standardSetController = null;
}
/**
* @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 : orderItemList) {
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
];
}
}
- santhosh konathala 26
- August 21, 2023
- Like
- 0
Hi Team plz help me getting an error like list index out of bounds 1
For the below code while executing
public static list<Product2> ConstructProducts(Integer cnt){
//ToDo: Ensure this method returns a list, of size cnt, of uniquely named Product2 records
// with all the required fields populated
// and IsActive = true
// an Initial Inventory set to 10
// and iterating through the product family picklist values throughout the list.
list<Product2> products=new list<Product2>();
list<Schema.PicklistEntry> pEntries = Constants.PRODUCT_FAMILY;
Integer pVal = 0;
for(Integer i=0;i<cnt;i++){
Product2 pd2=new Product2();
pd2.Name='Product-'+i;
pd2.IsActive = true;
pd2.Initial_Inventory__c = 10;
if(pVal == 4){
pVal = 0;
}
pd2.Family = pEntries.get(pVal).getValue();
pVal++;
products.add(pd2);
}
return products;
}
public static list<Product2> ConstructProducts(Integer cnt){
//ToDo: Ensure this method returns a list, of size cnt, of uniquely named Product2 records
// with all the required fields populated
// and IsActive = true
// an Initial Inventory set to 10
// and iterating through the product family picklist values throughout the list.
list<Product2> products=new list<Product2>();
list<Schema.PicklistEntry> pEntries = Constants.PRODUCT_FAMILY;
Integer pVal = 0;
for(Integer i=0;i<cnt;i++){
Product2 pd2=new Product2();
pd2.Name='Product-'+i;
pd2.IsActive = true;
pd2.Initial_Inventory__c = 10;
if(pVal == 4){
pVal = 0;
}
pd2.Family = pEntries.get(pVal).getValue();
pVal++;
products.add(pd2);
}
return products;
}
- santhosh konathala 26
- August 21, 2023
- Like
- 0
Can any one help me below code getting an error like "Ensure that values are preserved during pagination."
Below is my 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);
}
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';
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();
standardSetController = null;
}
/**
* @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 : orderItemList) {
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
];
}
}
/**
* @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';
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();
standardSetController = null;
}
/**
* @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 : orderItemList) {
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
];
}
}
- santhosh konathala 26
- August 21, 2023
- Like
- 0
Hi Team plz help me getting an error like list index out of bounds 1
For the below code while executing
public static list<Product2> ConstructProducts(Integer cnt){
//ToDo: Ensure this method returns a list, of size cnt, of uniquely named Product2 records
// with all the required fields populated
// and IsActive = true
// an Initial Inventory set to 10
// and iterating through the product family picklist values throughout the list.
list<Product2> products=new list<Product2>();
list<Schema.PicklistEntry> pEntries = Constants.PRODUCT_FAMILY;
Integer pVal = 0;
for(Integer i=0;i<cnt;i++){
Product2 pd2=new Product2();
pd2.Name='Product-'+i;
pd2.IsActive = true;
pd2.Initial_Inventory__c = 10;
if(pVal == 4){
pVal = 0;
}
pd2.Family = pEntries.get(pVal).getValue();
pVal++;
products.add(pd2);
}
return products;
}
public static list<Product2> ConstructProducts(Integer cnt){
//ToDo: Ensure this method returns a list, of size cnt, of uniquely named Product2 records
// with all the required fields populated
// and IsActive = true
// an Initial Inventory set to 10
// and iterating through the product family picklist values throughout the list.
list<Product2> products=new list<Product2>();
list<Schema.PicklistEntry> pEntries = Constants.PRODUCT_FAMILY;
Integer pVal = 0;
for(Integer i=0;i<cnt;i++){
Product2 pd2=new Product2();
pd2.Name='Product-'+i;
pd2.IsActive = true;
pd2.Initial_Inventory__c = 10;
if(pVal == 4){
pVal = 0;
}
pd2.Family = pEntries.get(pVal).getValue();
pVal++;
products.add(pd2);
}
return products;
}
- santhosh konathala 26
- August 21, 2023
- Like
- 0
Challenge Not yet complete... here's what's wrong: Ensure that you implement all the pagination methods using the corresponding StandardSetController methods.
7.Challenge Not yet complete... here's what's wrong:
Ensure that you implement all the pagination methods using the corresponding StandardSetController methods.
/**
* @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);
}
orderItemList = new List<OrderItem>();
OrderItemList.addAll(orderRecord.OrderItems);
set<Id> p2ids = new set<Id>();
for(OrderItem oitem : OrderItemList){
p2ids.add(oitem.PriceBookEntryId);
}
List<PriceBookEntry> p2List = new List<PriceBookEntry>();
if(p2Ids.size()>0){
p2List = [select id from PriceBookEntry where id IN: p2Ids];
}
standardSetController= new ApexPages.StandardSetController(p2List);
standardSetController.setPageSize(Constants.DEFAULT_ROWS);
}
public void resetstandardSetController() {
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';
standardSetController= new ApexPages.StandardSetController(Database.getQueryLocator(query));
if(StandardSetController== null){
StandardSetController= new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Product2Id, Product2.Family, Product2.Name,
Product2.Quantity_Remaining__c, UnitPrice FROM PricebookEntry ]));
StandardSetController.setPageSize(Constants.DEFAULT_ROWS);
}
}
//ToDo: Implement your own method to populate orderItemList
// that you will call after pagination and/or family selection
public void GetOrderItems() {
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.PricebookEntryId, 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
resetstandardSetController();
GetOrderItems();
}
/**
* @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>();
system.debug('@@@@@@@@@@@@@@@#######'+orderItemMap.values());
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);
}
}
if(orderItemsToUpsert.size()>0)
upsert orderItemsToUpsert;
if(orderItemsToDelete.size()>0)
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();
GetOrderItems();
}
/**
* @name Next
* @description
**/
public void Next(){
standardSetController.next();
GetOrderItems();
}
/**
* @name Previous
* @description
**/
public void Previous(){
standardSetController.previous();
GetOrderItems();
}
/**
* @name Last
* @description
**/
public void Last(){
standardSetController.last();
GetOrderItems();
}
/**
* @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 totalPages = (Integer)Math.ceil(standardSetController.getResultSize() / (Decimal)Constants.DEFAULT_ROWS);
System.debug('############ totalPages: ' + totalPages);
return totalPages;
}
/**
* @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 where PricebookEntry.isActive = true
)
FROM Order WHERE Id = :orderId
];
}
}
Ensure that you implement all the pagination methods using the corresponding StandardSetController methods.
/**
* @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);
}
orderItemList = new List<OrderItem>();
OrderItemList.addAll(orderRecord.OrderItems);
set<Id> p2ids = new set<Id>();
for(OrderItem oitem : OrderItemList){
p2ids.add(oitem.PriceBookEntryId);
}
List<PriceBookEntry> p2List = new List<PriceBookEntry>();
if(p2Ids.size()>0){
p2List = [select id from PriceBookEntry where id IN: p2Ids];
}
standardSetController= new ApexPages.StandardSetController(p2List);
standardSetController.setPageSize(Constants.DEFAULT_ROWS);
}
public void resetstandardSetController() {
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';
standardSetController= new ApexPages.StandardSetController(Database.getQueryLocator(query));
if(StandardSetController== null){
StandardSetController= new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Product2Id, Product2.Family, Product2.Name,
Product2.Quantity_Remaining__c, UnitPrice FROM PricebookEntry ]));
StandardSetController.setPageSize(Constants.DEFAULT_ROWS);
}
}
//ToDo: Implement your own method to populate orderItemList
// that you will call after pagination and/or family selection
public void GetOrderItems() {
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.PricebookEntryId, 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
resetstandardSetController();
GetOrderItems();
}
/**
* @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>();
system.debug('@@@@@@@@@@@@@@@#######'+orderItemMap.values());
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);
}
}
if(orderItemsToUpsert.size()>0)
upsert orderItemsToUpsert;
if(orderItemsToDelete.size()>0)
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();
GetOrderItems();
}
/**
* @name Next
* @description
**/
public void Next(){
standardSetController.next();
GetOrderItems();
}
/**
* @name Previous
* @description
**/
public void Previous(){
standardSetController.previous();
GetOrderItems();
}
/**
* @name Last
* @description
**/
public void Last(){
standardSetController.last();
GetOrderItems();
}
/**
* @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 totalPages = (Integer)Math.ceil(standardSetController.getResultSize() / (Decimal)Constants.DEFAULT_ROWS);
System.debug('############ totalPages: ' + totalPages);
return totalPages;
}
/**
* @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 where PricebookEntry.isActive = true
)
FROM Order WHERE Id = :orderId
];
}
}
- pavithra v 5
- August 20, 2018
- Like
- 0
Advanced Apex Superbadge (PDII) Step 7
Hi all,
I'm getting an error while verifying step 7:
Any advice or suggestion?
Thank you.
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.
Any advice or suggestion?
Thank you.
- Rocco Sorrentino
- December 19, 2017
- Like
- 0