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
Ali Husain 3Ali Husain 3 

Using visualforce, how do i tie customObject__c to Products?

Most of my scenario is complete but am having difficulities with a few areas (bottom of post).

I have a custom object Deal_Reg__c. The business flow for this object is that a user can go to this object's page and add a Product to this Deal Reg (related list of DealRegistrationProducts). I have created a junction object called Deal_Reg_Products__c that has a Deg Reg Lookup as well as a Product Lookup along with other custom fields. After you press the "Add" button on the Deal Reg Products Related List section of the Deal Reg page, i would like the first VF page to be displayed.

The first VF page should basically only be a selectable list of all the Products with the name of the Deal_Reg__c that we were coming from. After the user selects one or multiple records to add to the specific Deal Reg, they will press a button called "Add Product". Pressing that button should fire off the second VF page.

The second VF page should consist of the selected list of Products from the first VF page along with a Deal_Reg_Products__c.Forecast_Amount__c custom field, "Forecast Amount", next to each record where the user can input the value. After a value has been entered for each Product, the user will press a "Confirm" button which will send us back to the Deal Reg page where in the Deal Reg Products Related List section the selected Products are now visible with the Forecast Amount we entered and are now tied to that Deal Reg.

Here is my code for VF1:
<apex:page standardController="Deal_Registration_Products__c" extensions="wrapperClassController" tabstyle="Deal_Reg__c" id="myPage" recordSetVar="Deal_Registration_Products__r">
    <apex:form id="myForm">
       <!-- <apex:pageBlock title="Edit Account for {!$Deal_Reg__c.id}">
        </apex:pageBlock> -->
        <br/>
        <br/>
        <apex:pageBlock title="Find Products" id="mypageBlock">
            <table width="100%">
                <tr>
                    <td width="25%" align="right">
                        <apex:pageBlockSection columns="1">
                            <b>By Keyword </b><br/>
                            <apex:inputText value="{!inputText1}" html-placeholder="Product Name"/>
                            <apex:commandButton value="Search" action="{!searchProduct}"/>   
                        </apex:pageBlockSection>
                    </td>
                </tr>
            </table>
            <br/>
            <p/>
        </apex:pageBlock>
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Add Products" action="{!DisplayDetails}"/>
            </apex:pageBlockButtons>
            <!-- In our table we are displaying the cProduct records -->
            <apex:pageBlockTable value="{!products}" var="p" id="table">
                <apex:column >
                    <!-- This is our selected Boolean property in our wrapper class -->
                    <apex:inputCheckbox value="{!p.selected}"/>
                </apex:column>
                <!-- This is how we access the contact values within our cProduct container/wrapper -->
                <apex:column value="{!p.prod.Name}" />
                <apex:column value="{!p.prod.ProductCode}" />
                <apex:column value="{!p.prod.Description}" />
                <apex:column value="{!p.prod.Family}" />
            </apex:pageBlockTable>
            <apex:outputLabel >{!SelectedProd}</apex:outputLabel>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Here is my code for VF2:
<apex:page standardController="Deal_Registration_Products__c" extensions="wrapperClassController" recordSetVar="products">
   <apex:form >
       <apex:pageBlock title="Deal Registration Products">
            <apex:pageBlockButtons >
                <apex:commandButton value="Confirm"/>
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!selectedproductList}" var="p">
                <apex:column value="{!p.prod.Name}" headerValue="Product"/>
                <apex:column headerValue="Forecast Amount">
                    <apex:inputText value="{!p.forecastAmount}" />
                </apex:column> 
                <!-- <apex:column headerValue="Forecast Amount2">
                    <apex:inputText value="{!p.prod.Deal_Registration_Products__r}" />
                </apex:column>  -->
            </apex:pageBlockTable> 
            <apex:outputPanel >
                <apex:outputLabel >{!selectedproductList}</apex:outputLabel> 
            </apex:outputPanel>
        </apex:pageBlock>
    </apex:form>  
</apex:page>

And here is my controller:
public with sharing class wrapperClassController {

    public wrapperClassController(ApexPages.StandardSetController controller) {

    }


    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
            
                    setCon = new ApexPages.StandardSetController(Database.getQueryLocator([Select Id,Name,ProductCode,Description,Family from Product2 Order by Name]));
            }
            system.debug('setConSize-----'+setCon.getResultSize());
            return setCon;
        }
        set;
    }
    
    public String inputText1 {get;set;}
    public String productName {get;set;}
    public Boolean isListEmpty {get;set;}
    public List<cProduct2> productWrapperlist = new List<cProduct2>();
    public List<List<cProduct2>> listOfProductWrapperlist = new List<List<cProduct2>>();
    public Deal_Registration_Products__c dealRegProds;
    
    //Our collection of the class/wrapper objects cProduct2 
    public List<cProduct2> productList {get; set;}
    public List<cProduct2> selectedproductList {get; set;}
    public List<Product2> selectedProducts {get; set;}
    public List<Product2> selectedProd = new List<Product2>();

    //This method uses a simple SOQL query to return a List of Products
    public List<cProduct2> getProducts() {
        if(productList == null) {
            productList = new List<cProduct2>();
            for(Product2 p: [select Id, Name, ProductCode, Description, Family from Product2 limit 100]) {
                // As each product is processed we create a new cProduct2 object and add it to the productList
                productList.add(new cProduct2(p));
            }
        }
        return productList;
    }


    public void processSelected() {

        //We create a new list of Products that we be populated only with Products if they are selected
        List<Product2> 
        selectedProducts = new List<Product2>();

        //We will cycle through our list of cProducts and will check to see if the selected property is set to true, if it is we add the Product to the selectedProducts list
        for(cProduct2 cProd: getProducts()) {
            if(cProd.selected == true) {
                selectedProducts.add(cProd.prod);
            }
        }

        // Now we have our list of selected products and can perform any type of logic we want, sending emails, updating a field on the Product, etc
        System.debug('These are the selected Products...');
        for(Product2 prod: selectedProducts) {
            system.debug(prod);
        }
        //productList=null; // we need this line if we performed a write operation  because getProducts gets a fresh list now
        //return null;
    }


    public List<cProduct2> getdealregProductsDetails()
    {
        //If there are records to be displayed then show the records else display message
        if((setCon.getResultSize()) > 0)
        {
            if(listOfProductWrapperlist[setCon.getPageNumber()-1].size() == 0) {
                productWrapperlist = new List<cProduct2>();
                for(Product2 p : (List<Product2>)setCon.getRecords()) {
                    productWrapperlist.add(new cProduct2(p));
                }
                listOfProductWrapperlist[setCon.getPageNumber()-1] = productWrapperlist;
            }
            system.debug('productWrapperlist-----'+productWrapperlist);
            return listOfProductWrapperlist[setCon.getPageNumber()-1];
        }
        else
        {
            return null;
        }           
    }
    // This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Contact and a Boolean value
    public class cProduct2 {
        public Product2 prod {get; set;}
        public Boolean selected {get; set;}
        public String forecastAmount {get; set;}

        //This is the contructor method. When we create a new cProduct2 object we pass a Product that is set to the con property. We also set the selected value to false
        public cProduct2(Product2 p) {
            prod = p;
            selected = false;
        }
        
        public cProduct2(Product2 prd, boolean check) {
            prod = prd;
            selected = check;
        }
    }
    
    public class cDealRegProd {
        public Deal_Registration_Products__c dealRegProd {get; set;}
    }
        
    
    
    public void searchProduct() {
            system.debug('productName : '+productName);
            if(productName != null && productName != '') {
                setCon = new ApexPages.StandardSetController(Database.query('Select Id,Name,ProductCode,Description,Family from Product2 where Name like \'%'+productName+'%\' Order by Name limit 2000'));
                
            } else {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator([Select Id,Name,ProductCode,Description,Family from Product2 Order by Name]));
            }
            listOfProductWrapperlist.clear();
            setCon.setPageSize(25);
            Integer reminder = math.mod(setCon.getResultSize(),setCon.getPageSize());
            Integer listToCreateSize = (reminder == 0?(setCon.getResultSize()/setCon.getPageSize()):(setCon.getResultSize()/setCon.getPageSize())+1);
            for(Integer i=0; i < listToCreateSize ; i++){
                listOfProductWrapperlist.add(new List<cProduct2>());
            }
            if(listOfProductWrapperlist== null || listOfProductWrapperlist.isEmpty()) {
                isListEmpty = true;
            }       
        }
            
    public List<Product2> getSelectedProd() {
        if(selectedProd.size()>0) {
            System.debug(' selectProd '+selectedProd);
            return selectedProd;
        }
        else
        return null;
    }
    
    public PageReference DisplayDetails() {
        selectedproductList = new List<cProduct2>();
        
        for(cProduct2 prd : productList) {
            if(prd.selected == true) {
                selectedproductList.add(prd);
            }
        }
        system.debug(selectedproductList);
        PageReference reRend = new PageReference('/apex/DealRegProducts');
        reRend.setRedirect(false);
        return reRend;
    }
    
    public List<cProduct2> getProdDetails() {
        productList = new List<cProduct2>();
        List<Product2> prods = [Select Id,Name,ProductCode,Description,Family from Product2 Order by Name];
        
        for(Product2 p : prods) {
            productList.add(new cProduct2(p, false));
        }
        return productList;
    }
    
    public List<cProduct2> getSelectProdDetails() {
        selectedproductList = new List<cProduct2>();
        
        for(cProduct2 c : getProdDetails()) {
            System.debug(' selectProdDetails ' + c);
            
            if(c.selected == true)
            selectedproductList.add(c);
        }
        return selectedproductList;
    }
}


How do i display the name of the Deal_Reg__c on the 1st VF page?
How do i tie the selected Products and entered Forecast Amount to the corresponding Deal Reg after pressing "Confirm" and have those values displayed in the Related List section of the Deal_Reg__c page?

Thanks,
Balaji BondarBalaji Bondar
  1. You have to bind the table to Deal_Reg_Products__c records to see the related Deal_Reg
  2. You have to create the wrapper class of Deal_Reg_Products__c and update the value for corrosponding records.
  3.  Refer link https://developer.salesforce.com/page/Wrapper_Class
Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
Ali Husain 3Ali Husain 3
Thanks for the response.

How can i bind the table to Deal_Reg_Products__c records to see the related Deal_Reg?

Is done by adding: public Deal_Registration_Products__c dealRegProd {get; set;} to:
 
public class cProduct2 {
        public Product2 prod {get; set;}
        public Boolean selected {get; set;}
        public String forecastAmount {get; set;}
}
??

Thanks,