You need to sign in to do that
Don't have an account?
Anonymous Developer
Challenge Not yet complete... here's what's wrong: Ensure that the Visualforce page is displayed when a user clicks the New button on the Product object.
Need Help on Advance Apex Specialist:
Product2New.vfp:
<apex:page standardcontroller="Product2" extensions="Product2Extension">
<apex:sectionHeader title="New Product" subtitle="Add Inventory"/>
<apex:pageMessages id="pageMessages"/>
<apex:form id="form">
<apex:actionRegion>
<apex:pageBlock title="Existing Inventory" id="existingInv">
<apex:chart data="{!Inventory}" width="600" height="400">
<apex:axis type="Category" fields="name" position="left" title="Product Family"/>
<apex:axis type="Numeric" fields="val" position="bottom" title="Quantity Remaining"/>
<apex:barSeries axis="bottom" orientation="horizontal" xField="val" yField="name"/>
</apex:chart>
</apex:pageBlock>
<apex:pageBlock title="New Products">
<apex:pageBlockButtons location="top">
<apex:commandButton action="{!save}" value="Save" reRender="existingInv, orderItemTable, pageMessages"/>
</apex:pageBlockButtons>
<apex:pageBlockButtons location="bottom">
<apex:commandButton action="{!addRows}" value="Add" reRender="orderItemTable, pageMessages"/>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!productsToInsert}" var="p" id="orderItemTable">
<apex:column headerValue="{!$ObjectType.Product2.Fields.Name.Label}">
<apex:inputText value="{!p.productRecord.Name}"/>
</apex:column>
<apex:column headerValue="{!$ObjectType.Product2.Fields.Family.Label}">
<apex:selectList value="{!p.productRecord.Family}" size="1" multiselect="false">
<apex:selectOptions value="{!FamilyOptions}"></apex:selectOptions>
</apex:selectList>
</apex:column>
<apex:column headerValue="{!$ObjectType.Product2.Fields.IsActive.Label}">
<apex:inputField value="{!p.productRecord.isActive}"/>
</apex:column>
<apex:column headerValue="{!$ObjectType.PricebookEntry.Fields.UnitPrice.Label}">
<apex:inputText value="{!p.pricebookEntryRecord.UnitPrice}"/>
</apex:column>
<apex:column headerValue="{!$ObjectType.Product2.Fields.Initial_Inventory__c.Label}">
<apex:inputField value="{!p.productRecord.Initial_Inventory__c}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:actionRegion>
</apex:form>
</apex:page>
Product2Extensions.apxc:
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++ ) {
productsToInsert.add( new ProductWrapper() );
}
}
public List<ChartHelper.ChartData> GetInventory(){
return ChartHelper.GetInventory();
}
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 PageReference Save(){
Savepoint sp = Database.setSavepoint();
try {
List<Product2> products = new List<Product2>();
List<PricebookEntry> pbes = new List<PricebookEntry>();
for (ProductWrapper prodwrapper : productsToInsert) {
if(prodwrapper.productRecord != null && prodwrapper.pricebookEntryRecord != null) {
if(prodwrapper.productRecord.Name != null && prodwrapper.productRecord.Family != null && constants.SELECT_ONE != prodwrapper.productRecord.Family && prodwrapper.productRecord.Initial_Inventory__c != null && prodwrapper.pricebookEntryRecord.UnitPrice != null) {
products.add(prodwrapper.productRecord);
PricebookEntry pbe = prodwrapper.pricebookEntryRecord;
pbe.IsActive = true;
pbe.Pricebook2Id = Constants.STANDARD_PRICEBOOK_ID;
pbes.add(pbe);
}
}
}
insert products;
for (integer i = 0; i < pbes.size(); i++) {
pbes[i].Product2Id = products[i].Id;
}
insert pbes;
//If successful clear the list and display an informational message
apexPages.addMessage(new ApexPages.message(ApexPages.Severity.INFO,productsToInsert.size()+' Inserted'));
productsToInsert.clear(); //Do not remove
AddRows(); //Do not remove
} catch (Exception e){
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);
pricebookEntryRecord = new pricebookEntry(Unitprice=0.0);
}
}
}
Product2New.vfp:
<apex:page standardcontroller="Product2" extensions="Product2Extension">
<apex:sectionHeader title="New Product" subtitle="Add Inventory"/>
<apex:pageMessages id="pageMessages"/>
<apex:form id="form">
<apex:actionRegion>
<apex:pageBlock title="Existing Inventory" id="existingInv">
<apex:chart data="{!Inventory}" width="600" height="400">
<apex:axis type="Category" fields="name" position="left" title="Product Family"/>
<apex:axis type="Numeric" fields="val" position="bottom" title="Quantity Remaining"/>
<apex:barSeries axis="bottom" orientation="horizontal" xField="val" yField="name"/>
</apex:chart>
</apex:pageBlock>
<apex:pageBlock title="New Products">
<apex:pageBlockButtons location="top">
<apex:commandButton action="{!save}" value="Save" reRender="existingInv, orderItemTable, pageMessages"/>
</apex:pageBlockButtons>
<apex:pageBlockButtons location="bottom">
<apex:commandButton action="{!addRows}" value="Add" reRender="orderItemTable, pageMessages"/>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!productsToInsert}" var="p" id="orderItemTable">
<apex:column headerValue="{!$ObjectType.Product2.Fields.Name.Label}">
<apex:inputText value="{!p.productRecord.Name}"/>
</apex:column>
<apex:column headerValue="{!$ObjectType.Product2.Fields.Family.Label}">
<apex:selectList value="{!p.productRecord.Family}" size="1" multiselect="false">
<apex:selectOptions value="{!FamilyOptions}"></apex:selectOptions>
</apex:selectList>
</apex:column>
<apex:column headerValue="{!$ObjectType.Product2.Fields.IsActive.Label}">
<apex:inputField value="{!p.productRecord.isActive}"/>
</apex:column>
<apex:column headerValue="{!$ObjectType.PricebookEntry.Fields.UnitPrice.Label}">
<apex:inputText value="{!p.pricebookEntryRecord.UnitPrice}"/>
</apex:column>
<apex:column headerValue="{!$ObjectType.Product2.Fields.Initial_Inventory__c.Label}">
<apex:inputField value="{!p.productRecord.Initial_Inventory__c}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:actionRegion>
</apex:form>
</apex:page>
Product2Extensions.apxc:
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++ ) {
productsToInsert.add( new ProductWrapper() );
}
}
public List<ChartHelper.ChartData> GetInventory(){
return ChartHelper.GetInventory();
}
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 PageReference Save(){
Savepoint sp = Database.setSavepoint();
try {
List<Product2> products = new List<Product2>();
List<PricebookEntry> pbes = new List<PricebookEntry>();
for (ProductWrapper prodwrapper : productsToInsert) {
if(prodwrapper.productRecord != null && prodwrapper.pricebookEntryRecord != null) {
if(prodwrapper.productRecord.Name != null && prodwrapper.productRecord.Family != null && constants.SELECT_ONE != prodwrapper.productRecord.Family && prodwrapper.productRecord.Initial_Inventory__c != null && prodwrapper.pricebookEntryRecord.UnitPrice != null) {
products.add(prodwrapper.productRecord);
PricebookEntry pbe = prodwrapper.pricebookEntryRecord;
pbe.IsActive = true;
pbe.Pricebook2Id = Constants.STANDARD_PRICEBOOK_ID;
pbes.add(pbe);
}
}
}
insert products;
for (integer i = 0; i < pbes.size(); i++) {
pbes[i].Product2Id = products[i].Id;
}
insert pbes;
//If successful clear the list and display an informational message
apexPages.addMessage(new ApexPages.message(ApexPages.Severity.INFO,productsToInsert.size()+' Inserted'));
productsToInsert.clear(); //Do not remove
AddRows(); //Do not remove
} catch (Exception e){
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);
pricebookEntryRecord = new pricebookEntry(Unitprice=0.0);
}
}
}
Please note that Questions about how to pass Trailhead challenges are not on topic, because these challenges are intended to be independent demonstrations of your abilities.
Trailhead Help (https://trailhead.salesforce.com/en/help?support=home)can provide assistance for situations where Trailhead does not appear to be functioning correctly. You can reach out to them if this is the case.
Please close the thread by selected as Best Answer so that we can keep our community clean
Thanks!