You need to sign in to do that
Don't have an account?
Shruti Suman Mishra
Advanced Apex Specialist- Step 8 Error Please ensure that product2Extension and its methods are still working as specified in the earlier challenge.
Hi,
I am facing below error while trying to verify Step 8-
Error: Please ensure that product2Extension and its methods are still working as specified in the earlier challenge.
Code below:
public class Product2Extension {
public List<ProductWrapper> productsToInsert {get;set;}
public Product2Extension(ApexPages.StandardController controller){
productsToInsert = new List<ProductWrapper>();
addRows();
}
public List<SelectOption> GetFamilyOptions() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption(Constants.SELECT_ONE, Constants.SELECT_ONE));
for(PickListEntry eachPicklistValue : Constants.PRODUCT_FAMILY) {
options.add(new SelectOption(eachPicklistValue.getValue(), eachPicklistValue.getLabel()));
}
return options;
}
public void AddRows(){
for (Integer i=0; i<Constants.DEFAULT_ROWS; i++ ){
productsToInsert.add(new ProductWrapper());
}
}
public List<ChartHelper.ChartData> GetInventory(){
return ChartHelper.GetInventory();
}
public PageReference Save(){
SavePoint sp = Database.setSavepoint();
Integer insertedCount = 0;
try {
List<Product2> newProducts = new List<Product2>();
List<PriceBookEntry> pbeList = new List<PriceBookEntry>();
List<ProductWrapper> filteredProductWrappers = new List<ProductWrapper>();
for(ProductWrapper eachPW : productsToInsert) {
if(!String.isBlank(eachPW.productRecord.Name) && !String.isBlank(eachPW.productRecord.Family) &&
eachPW.productRecord.Family!=Constants.SELECT_ONE && eachPW.productRecord.isActive &&
eachPW.pricebookEntryRecord.UnitPrice!=null && eachPW.productRecord.Initial_Inventory__c!=null &&
eachPW.productRecord.Initial_Inventory__c!=0 && eachPW.pricebookEntryRecord.UnitPrice!=0) {
filteredProductWrappers.add(eachPW);
}
}
for(ProductWrapper eachPW : filteredProductWrappers) {
newProducts.add(eachPW.productRecord);
}
Database.SaveResult[] productSaveResults = Database.insert(newProducts, false);
for(Integer i=0; i<productSaveResults.size(); i++) {
if(productSaveResults[i].isSuccess()) {
PriceBookEntry pbe = filteredProductWrappers[i].pricebookEntryRecord;
pbe.Product2Id = productSaveResults[i].getId();
pbe.IsActive = true;
pbe.Pricebook2Id = Constants.STANDARD_PRICEBOOK_ID;
pbeList.add(pbe);
insertedCount++;
}
}
Database.SaveResult[] pbeSaveResults = Database.insert(pbeList, false);
//If successful clear the list and display an informational message
apexPages.addMessage(new ApexPages.message(ApexPages.Severity.INFO,insertedCount + ' Inserted'));
productsToInsert.clear(); //Do not remove
addRows(); //Do not remove
}
catch (Exception e){
System.debug('Exception occured:'+e.getMessage());
Database.rollback(sp);
apexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR, Constants.ERROR_MESSAGE));
}
return null;
}
public class ProductWrapper {
public Product2 productRecord {get;set;}
public PriceBookEntry pricebookEntryRecord {get;set;}
public ProductWrapper() {
productRecord = new Product2();
pricebookEntryRecord = new PricebookEntry();
}
}
}
I am facing below error while trying to verify Step 8-
Error: Please ensure that product2Extension and its methods are still working as specified in the earlier challenge.
Code below:
public class Product2Extension {
public List<ProductWrapper> productsToInsert {get;set;}
public Product2Extension(ApexPages.StandardController controller){
productsToInsert = new List<ProductWrapper>();
addRows();
}
public List<SelectOption> GetFamilyOptions() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption(Constants.SELECT_ONE, Constants.SELECT_ONE));
for(PickListEntry eachPicklistValue : Constants.PRODUCT_FAMILY) {
options.add(new SelectOption(eachPicklistValue.getValue(), eachPicklistValue.getLabel()));
}
return options;
}
public void AddRows(){
for (Integer i=0; i<Constants.DEFAULT_ROWS; i++ ){
productsToInsert.add(new ProductWrapper());
}
}
public List<ChartHelper.ChartData> GetInventory(){
return ChartHelper.GetInventory();
}
public PageReference Save(){
SavePoint sp = Database.setSavepoint();
Integer insertedCount = 0;
try {
List<Product2> newProducts = new List<Product2>();
List<PriceBookEntry> pbeList = new List<PriceBookEntry>();
List<ProductWrapper> filteredProductWrappers = new List<ProductWrapper>();
for(ProductWrapper eachPW : productsToInsert) {
if(!String.isBlank(eachPW.productRecord.Name) && !String.isBlank(eachPW.productRecord.Family) &&
eachPW.productRecord.Family!=Constants.SELECT_ONE && eachPW.productRecord.isActive &&
eachPW.pricebookEntryRecord.UnitPrice!=null && eachPW.productRecord.Initial_Inventory__c!=null &&
eachPW.productRecord.Initial_Inventory__c!=0 && eachPW.pricebookEntryRecord.UnitPrice!=0) {
filteredProductWrappers.add(eachPW);
}
}
for(ProductWrapper eachPW : filteredProductWrappers) {
newProducts.add(eachPW.productRecord);
}
Database.SaveResult[] productSaveResults = Database.insert(newProducts, false);
for(Integer i=0; i<productSaveResults.size(); i++) {
if(productSaveResults[i].isSuccess()) {
PriceBookEntry pbe = filteredProductWrappers[i].pricebookEntryRecord;
pbe.Product2Id = productSaveResults[i].getId();
pbe.IsActive = true;
pbe.Pricebook2Id = Constants.STANDARD_PRICEBOOK_ID;
pbeList.add(pbe);
insertedCount++;
}
}
Database.SaveResult[] pbeSaveResults = Database.insert(pbeList, false);
//If successful clear the list and display an informational message
apexPages.addMessage(new ApexPages.message(ApexPages.Severity.INFO,insertedCount + ' Inserted'));
productsToInsert.clear(); //Do not remove
addRows(); //Do not remove
}
catch (Exception e){
System.debug('Exception occured:'+e.getMessage());
Database.rollback(sp);
apexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR, Constants.ERROR_MESSAGE));
}
return null;
}
public class ProductWrapper {
public Product2 productRecord {get;set;}
public PriceBookEntry pricebookEntryRecord {get;set;}
public ProductWrapper() {
productRecord = new Product2();
pricebookEntryRecord = new PricebookEntry();
}
}
}
@Amer Admin ,
Could you please clarify me that which filter logic I need to remove
if(!String.isBlank(eachPW.productRecord.Name) && !String.isBlank(eachPW.productRecord.Family) &&
eachPW.productRecord.Family!=Constants.SELECT_ONE && eachPW.productRecord.isActive &&
eachPW.pricebookEntryRecord.UnitPrice!=null && eachPW.productRecord.Initial_Inventory__c!=null &&
eachPW.productRecord.Initial_Inventory__c!=0 && eachPW.pricebookEntryRecord.UnitPrice!=0) {
filteredProductWrappers.add(eachPW);
}
to just this
filteredProductWrappers.add(eachPW);
public ProductWrapper() {
productRecord = new Product2(Initial_Inventory__c =0, Name='Test Product', isActive=true); pricebookEntryRecord = new PricebookEntry(Unitprice=0.0);
}
i made change in my code, and it got run
please copy and paste my code into Product2Extension class.
I hope it will work..
public class Product2Extension {
public List<ProductWrapper> productsToInsert {get;set;}
public Product2Extension(ApexPages.StandardController controller){
productsToInsert = new List<ProductWrapper>();
AddRows();
}
public void AddRows(){
for ( Integer i=0; i<Constants.DEFAULT_ROWS; i++ ){
ProductWrapper prodWrapper = new ProductWrapper();
productsToInsert.add( prodWrapper );
}
}
public List<ChartHelper.ChartData> GetInventory(){
return ChartHelper.GetInventory();
}
public PageReference Save(){
SavePoint sp1 = Database.setSavepoint();
try {
Map<Integer, Product2> products = new Map<Integer, Product2>();
Map<Integer, PriceBookEntry> priceBookEntries = new Map<Integer, PriceBookEntry>();
Integer index = 0;
for(ProductWrapper prdWrapper : productsToInsert) {
if(String.isNotBlank(prdWrapper.productRecord.Name) && prdWrapper.pricebookEntryRecord.UnitPrice!=null &&
prdWrapper.productRecord.Initial_Inventory__c!=null && prdWrapper.productRecord.isActive &&
prdWrapper.productRecord.Initial_Inventory__c != 0 && prdWrapper.pricebookEntryRecord.UnitPrice!=0){
products.put(index,prdWrapper.productRecord);
priceBookEntries.put(index,prdWrapper.pricebookEntryRecord);
index ++;
}
}
insert products.values();
List<PriceBookEntry> pbList = new List<PriceBookEntry>();
for(Integer mapIndex : products.keySet()) {
PriceBookEntry currentPBEntry = priceBookEntries.get(mapIndex);
if(products.get(mapIndex).Id!=null) {
currentPBEntry.Pricebook2Id = Constants.STANDARD_PRICEBOOK_ID;
System.debug('' + products.get(mapIndex).Id);
currentPBEntry.Product2Id = products.get(mapIndex).Id;
currentPBEntry.IsActive = true;
pbList.add(currentPBEntry);
}
}
insert pbList;
//If successful clear the list and display an informational message
apexPages.addMessage(new ApexPages.message(ApexPages.Severity.INFO,pbList.size()+' Inserted'));
productsToInsert.clear(); //Do not remove
addRows(); //Do not remove
} catch (Exception e){
apexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR,Constants.ERROR_MESSAGE));
Database.rollback(sp1);
}
return null;
}
public List<SelectOption> GetFamilyOptions(){
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption(Constants.SELECT_ONE,Constants.SELECT_ONE));
for(Schema.PicklistEntry entry : Constants.PRODUCT_FAMILY){
options.add(new SelectOption(entry.getLabel(),entry.getValue()));
}
return options;
}
public class ProductWrapper{
public Product2 productRecord {get;set;}
public PriceBookEntry pricebookEntryRecord {get;set;}
public ProductWrapper() {
productRecord = new Product2(Initial_Inventory__c =0,Name='Test Product', isActive=true);
pricebookEntryRecord = new PricebookEntry(Unitprice=0.0);
}
}
}
If you are facing this type of error in Advanced Apex Specialist:
Challenge Not yet complete... here's what's wrong: Please ensure that product2Extension and its methods are still working as specified in the earlier challenge.
Please copy the below code:
public class Product2Extension {
public List<ProductWrapper> productsToInsert {get;set;}
public Product2Extension(ApexPages.StandardController controller){
productsToInsert = new List<ProductWrapper>();
AddRows();
}
public void AddRows(){
for ( Integer i=0; i<Constants.DEFAULT_ROWS; i++ ){
ProductWrapper prodWrapper = new ProductWrapper();
productsToInsert.add( prodWrapper );
}
}
public List<ChartHelper.ChartData> GetInventory(){
return ChartHelper.GetInventory();
}
public PageReference Save(){
SavePoint sp1 = Database.setSavepoint();
try {
Map<Integer, Product2> products = new Map<Integer, Product2>();
Map<Integer, PriceBookEntry> priceBookEntries = new Map<Integer, PriceBookEntry>();
Integer index = 0;
for(ProductWrapper prdWrapper : productsToInsert) {
if(String.isNotBlank(prdWrapper.productRecord.Name) && prdWrapper.pricebookEntryRecord.UnitPrice!=null &&
prdWrapper.productRecord.Initial_Inventory__c!=null && prdWrapper.productRecord.isActive &&
prdWrapper.productRecord.Initial_Inventory__c != 0 && prdWrapper.pricebookEntryRecord.UnitPrice!=0){
products.put(index,prdWrapper.productRecord);
priceBookEntries.put(index,prdWrapper.pricebookEntryRecord);
index ++;
}
}
insert products.values();
List<PriceBookEntry> pbList = new List<PriceBookEntry>();
for(Integer mapIndex : products.keySet()) {
PriceBookEntry currentPBEntry = priceBookEntries.get(mapIndex);
if(products.get(mapIndex).Id!=null) {
currentPBEntry.Pricebook2Id = Constants.STANDARD_PRICEBOOK_ID;
System.debug('' + products.get(mapIndex).Id);
currentPBEntry.Product2Id = products.get(mapIndex).Id;
currentPBEntry.IsActive = true;
pbList.add(currentPBEntry);
}
}
insert pbList;
//If successful clear the list and display an informational message
apexPages.addMessage(new ApexPages.message(ApexPages.Severity.INFO,pbList.size()+' Inserted'));
productsToInsert.clear(); //Do not remove
addRows(); //Do not remove
} catch (Exception e){
apexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR,Constants.ERROR_MESSAGE));
Database.rollback(sp1);
}
return null;
}
public List<SelectOption> GetFamilyOptions(){
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption(Constants.SELECT_ONE,Constants.SELECT_ONE));
for(Schema.PicklistEntry entry : Constants.PRODUCT_FAMILY){
options.add(new SelectOption(entry.getLabel(),entry.getValue()));
}
return options;
}
public class ProductWrapper{
public Product2 productRecord {get;set;}
public PriceBookEntry pricebookEntryRecord {get;set;}
public ProductWrapper() {
productRecord = new Product2(Initial_Inventory__c =0, Name='Test Product', isActive=true);
pricebookEntryRecord = new PricebookEntry(Unitprice=0.0);
}
}
}
I am getting the same error as above.I tried all the above workaround still the error persists:
Please ensure that product2Extension and its methods are still working as specified in the earlier challenge.
Below is my class:
public class Product2Extension {
public List<ProductWrapper> productsToInsert {get;set;}
public Product2Extension(ApexPages.StandardController controller){
productsToInsert = new List<ProductWrapper>();
addRows();
}
public List<SelectOption> GetFamilyOptions() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption(Constants.SELECT_ONE, Constants.SELECT_ONE));
for(PickListEntry eachPicklistValue : Constants.PRODUCT_FAMILY) {
options.add(new SelectOption(eachPicklistValue.getValue(), eachPicklistValue.getLabel()));
}
return options;
}
public void AddRows(){
for (Integer i=0; i<Constants.DEFAULT_ROWS; i++ ){
productsToInsert.add(new ProductWrapper());
}
}
public List<ChartHelper.ChartData> GetInventory(){
return ChartHelper.GetInventory();
}
public PageReference Save(){
SavePoint sp = Database.setSavepoint();
Integer insertedCount = 0;
try {
List<Product2> newProducts = new List<Product2>();
List<PriceBookEntry> pbeList = new List<PriceBookEntry>();
List<ProductWrapper> filteredProductWrappers = new List<ProductWrapper>();
for(ProductWrapper eachPW : productsToInsert) {
{
filteredProductWrappers.add(eachPW);
}
}
for(ProductWrapper eachPW : filteredProductWrappers) {
newProducts.add(eachPW.productRecord);
}
Database.SaveResult[] productSaveResults = Database.insert(newProducts, false);
for(Integer i=0; i<productSaveResults.size(); i++) {
if(productSaveResults[i].isSuccess()) {
PriceBookEntry pbe = filteredProductWrappers[i].pricebookEntryRecord;
pbe.Product2Id = productSaveResults[i].getId();
pbe.IsActive = true;
pbe.Pricebook2Id = Constants.STANDARD_PRICEBOOK_ID;
pbeList.add(pbe);
insertedCount++;
}
}
Database.SaveResult[] pbeSaveResults = Database.insert(pbeList, false);
//If successful clear the list and display an informational message
apexPages.addMessage(new ApexPages.message(ApexPages.Severity.INFO,insertedCount + ' Inserted'));
productsToInsert.clear(); //Do not remove
addRows(); //Do not remove
}
catch (Exception e){
System.debug('Exception occured:'+e.getMessage());
Database.rollback(sp);
apexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR, Constants.ERROR_MESSAGE));
}
return null;
}
public class ProductWrapper {
public Product2 productRecord {get;set;}
public PriceBookEntry pricebookEntryRecord {get;set;}
public ProductWrapper() {
productRecord = new Product2(Initial_Inventory__c =0, Name='Test Product', isActive=true);
pricebookEntryRecord = new PricebookEntry(Unitprice=0.0);
}
}
}