1

Here i am not clear about the apex logic,

the flow is :

i have one child record Bundle__c and Product2 is parent, and i need to store multiple Product2 under Bundle__c (Product2 is located in related list under Bundle__c record)

To achieve this scenario i wrote visualforce page for multiselect product2 using checkbox and once user click on ADD Button then those all selected Products have to store under that related list of Bundle__c

here is my Page code :

<apex:page standardController="Product2" extensions="ProductSelectController">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockButtons location="TOP">
            <apex:commandButton value="Add" action="{!AddProduct}"/>
        </apex:pageBlockButtons>
        <apex:pageBlockTable value="{!productlist}" var="prlist">
            <apex:column >
                <apex:inputCheckbox value="{!selectedCheckBox}" />
            </apex:column>
            <apex:column headerValue="Product Name">
                {!prList.Name}
            </apex:column>
            <apex:column headerValue="Description">
                {!prList.Description}
            </apex:column>
            <apex:column headerValue="Price">
                {!prList.Cost__c}
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:form>

Apex Class :

Public Class ProductSelectController{

public List<Product2> productlist{get;set;}
public boolean selectedCheckBox{get;set;}
//public Product2 pr{get;set;}
public Bundle__c bundle{get;set;}

public ProductSelectController(ApexPages.StandardController Controller){

    productlist = [select Id, Name, Description, Cost__c from Product2];
    bundle = new Bundle__c();
    //pr = new Product2();

}

public Void AddProduct(){
    for(Product2 p : productlist){
        if(selectedCheckBox == True){
            p.Bundle__c = bundle.Id;
            system.debug('====P====='+p.Bundle__c);
        }
    }
}
}

Can anyone please help me !!

3
  • You need to use wrapper to achieve this functionality. Commented Dec 26, 2016 at 6:43
  • thanks tushar !! honestly i am new to apex and need to know above code is correct or need some correction ?
    – HSdev
    Commented Dec 26, 2016 at 6:45
  • Nope its incorrect. You will get same checkbox value for all records thats why I am suggesting you to go through with wrapper class. Commented Dec 26, 2016 at 7:37

2 Answers 2

1

Assuming that Bundle__c is the record being passed into the standard controller and that you are simply updating a field on product2 (those you selected) to populate the Bundle__c lookup, this illustrates the wrapper class in its most basic form:

class

Public Class ProductSelectController{

public Id bundleId{get;set;}

public productWrapper[] productList {
    get{
        if(productList == null){
            productList = New ProductWrapper[]{};

            for(Product2 p : [select Id, Name, Description, Cost__c, Bundle__ c from Product2])
                productList.add(New productWrapper(p,bundleId));

        }

        return productList;
    }
    set;
}

//Assuming the standard controller is on bundle
public ProductSelectController(ApexPages.StandardController Controller){        
    bundleId = controller.getId();
}

public Void save(){
    Product2[] tbu = New Product2[]{};

    for(ProductWrapper p : productlist){
        if(p.isSelected == True){
            p.prod.Bundle__c = bundleId;
            system.debug('====P====='+p.prod.Bundle__c);
            tbu.add(p.prod);
        }
    }

    update tbu;
}

public class productWrapper{
    public product2 prod {get;set;}
    public boolean isSelected {get;set;}

    public productWrapper(Product2 prod, Id bundleId){
        this.prod = prod;
        isSelected = prod.Bundle__c == bundleId; //If the product already is associated with the BundleId then default to already selected
    }
}
}

Then your page would use the wrapper and its values to iterate and ultimately call the save() method

<apex:page standardController="Bundle__c" extensions="ProductSelectController">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockButtons location="TOP">
            <apex:commandButton value="Add" action="{!save}"/>
        </apex:pageBlockButtons>
        <apex:pageBlockTable value="{!productlist}" var="prlist">
            <apex:column >
                <apex:inputCheckbox value="{!prList.isSelected}" />
            </apex:column>
            <apex:column headerValue="Product Name">
                {!prList.prod.Name}
            </apex:column>
            <apex:column headerValue="Description">
                {!prList.prod.Description}
            </apex:column>
            <apex:column headerValue="Price">
                {!prList.prod.Cost__c}
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:form>

I assumed you had the standardController set incorrectly since you were updating the product2 records Bundle__c lookup to be associated with the Bundle__c record. In the Example the Bundle__c is the singular record that you will be using to populated the lookup on your selected Product2 records.

11
  • Hey Eric, productList.add(p,bundleId); can you please try to make me understand ?
    – HSdev
    Commented Dec 26, 2016 at 9:36
  • @HSdev - Updated, forgot to add the constructor in the line of code.
    – Eric
    Commented Dec 26, 2016 at 9:37
  • bundleId = con.getRecordId(); & p.Bundle__c = bundleId;
    – HSdev
    Commented Dec 26, 2016 at 9:49
  • is there a question? the first assigns the value of the standard controller record Id (bundle__c) to a property. the second assigns that id to the product2 if selected (like you were trying to do in your example )...
    – Eric
    Commented Dec 26, 2016 at 9:52
  • 2
    Eirc, that error was solved (API is OK) only need to add object variable which is declared in Wrapper p.prod.Bundle__c = bundleId;
    – HSdev
    Commented Dec 26, 2016 at 10:23
-1

Here is the Wrapper Class outline code. Little explanations on the concept of Wrapper class. You can think of it as the structure of the C/C++ languages.

This is how you need to declare it:- Your object relationships are not very clear. Still, I made my own assumptions while coding this. Please follow it as an example.

Public Class ProductSelectController{

public ProductSelectController(ApexPages.StandardController Controller){

public List<ProductBundleSelectedWrapper> productBundlewrap = new List<ProductBundleSelectedWrapper>(); 
public ProductBundleSelectedWrapper wrap = new ProductBundleSelectedWrapper();
public Product2 productrec = new Product2();
public Bundle__c Bundlerec = new Bundle__c();
public List<Product2> productlst = new List<Product2>();
public List<Product2> productlst2 = new List<Product2>();
public List<Bundle__c> bundlelst = new List<Bundle__c>([select Id, Product__c, Name, Description, Cost__c from Bundle__c WHERE Id = (Bundle__c)controller.getRecord().id]);

public Void AddProduct(){
    for (Bundle__c tmpbundle : bundlelst){
        productlst = [SELECT ProductCode, isSelected, Name, Description FROM Product2 WHERE Product__c =: (Id)tmpbundle.Product__c]; 

        for (Product2 tmpprod : productlst)
        {
            if (tmpprod.isSelected == true)
                productlst2.add(tmpprod);
        }
        wrap.productlst = productlst2;
        wrap.buldlerec = tmpbundle;
        ProductBundleSelectedWrapper.add(wrap);
    }
    insert ProductBundleSelectedWrapper;
}   


public class ProductBundleSelectedWrapper{
    public List<Product2> productlst = new List<Product2>();
    public Boolean isSelected = false;
    public Buldle__c buldlerec = new Bundle__c();

    //Constructor
    public productSelectedWrapper()
    {
    }

}

}

4
  • Thank u @Prolay for your response & i apologies for relationship which is not clear.. exact relationship is.. 1. 'Bundle__c'(Custom Object) 2. 'Product2'(Standard Object), i have created lookUp relationship of Bundle__c in Product2. Now, i can see Product related list under Bundle__c record and i am going to create custom Button in related list which will open up as a visualforce page with multiple product select option and that visualforce page has Add Button which will pick those all selected values and put into Product related list under Bundle__c record. Hope This is Clear Now :)
    – HSdev
    Commented Dec 26, 2016 at 9:11
  • @HSdev, I need Little changes in my code. Please specify the custom fields which are part of the Custom Object Bundle__c. It will help me to develop the correct optimized code. Commented Dec 26, 2016 at 11:49
  • Thank you Prolay but i am almost close to result, if you don't mind can you please help me get record id in constructor ?
    – HSdev
    Commented Dec 26, 2016 at 11:51
  • public ProductSelectController(ApexPages.StandardController Controller){ //Your code here public Void AddProduct(ApexPages.StandardController Controller){ productlst = [SELECT ProductCode, isSelected, Name, Description FROM Product2 WHERE Product__c =: (Id)controller.getRecord().id]; Commented Dec 26, 2016 at 12:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .