+ Start a Discussion
Amit Visapurkar 5Amit Visapurkar 5 

Display Pop up when a record is checked on visualforce page using checkbox

User-added image
If unit price for a product is not present and user clicks the associated product checkbox a popup should appear.
Pankaj_GanwaniPankaj_Ganwani
Hey Amit,

I believe you are creating the above structure using wrapper class something like this:

public class ProductWrapper
{
    public Boolean isSelect;
    public Product2 objProd;
}

All you need to do is just call javascript method on onchange event of checkbox.

function validatePrice(price, value)
{
      if(value==true && price == null)
         alert('Price cannot be changed.');
}

Call above method from <apex:inputCheckbox onChange="validatePrice('{!objProd.UnitPrice}',this.value);"/>
Amit Visapurkar 5Amit Visapurkar 5
@Pankaj

Not working

Below is my visualforce code

<apex:page controller="ContactSelectClassController" sidebar="false">
    <script type="text/javascript">
    function validatePrice(price, value)
{
      if(value==true && price == null)
         alert('Price cannot be changed.');
}
        function selectAllCheckboxes(obj,receivedInputID){
            var inputCheckBox = document.getElementsByTagName("input");
            for(var i=0; i<inputCheckBox.length; i++){
                if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
                    inputCheckBox[i].checked = obj.checked;
                }
            }
        }
        
    </script>
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Add products" action="{!processSelected}" rerender="table2"/>
                <apex:commandButton value="Add to Order" action="{!orderP}"/>
            </apex:pageBlockButtons>
 
            <apex:pageblockSection title="All Products" collapsible="false" columns="2">
 
             <apex:pageBlockTable value="{!wrapProductList}" var="accWrap" id="table" title="All Products">
                    <apex:column >
                        <apex:facet name="header">
                            <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                        </apex:facet>
                        <apex:inputCheckbox value="{!accWrap.selected}" id="inputId" onChange="validatePrice('{!acc.UnitPrice}',this.value);"/>                   
                    </apex:column>    
                    <apex:column value="{!accWrap.acc.Name}" />
                    <apex:column value="{!accWrap.acc.UnitPrice}" headerValue="Unit Price" />                     
                </apex:pageBlockTable>
    
                <apex:pageBlockTable value="{!selectedAccounts}" var="c" id="table2" title="Selected Products">                                                   
                    <apex:column value="{!c.Name}" headerValue="Product Name"/>
                    <apex:column value="{!c.UnitPrice}" headerValue="Product Price" />                                   
                </apex:pageBlockTable>
 
            </apex:pageblockSection>
            
        </apex:pageBlock>
    </apex:form>
 
</apex:page>

CONTROLLER

public class ContactSelectClassController{

    String OrderId= ApexPages.currentPage().getParameters().get('id');
    public List<wrapProduct> wrapProductList {get; set;}
    public List<PricebookEntry> selectedAccounts{get;set;}
 
    //private  List<Id> contactids=new list<Id>();
    //public List<Contact> conts;
  
    public ContactSelectClassController()
    {
            
        if(wrapProductList == null)
         {
            wrapProductList = new List<wrapProduct>();
            for(PricebookEntry a: [SELECT Name,UnitPrice,Pricebook2Id FROM PricebookEntry WHERE Pricebook2Id IN (SELECT Pricebook2Id FROM Order WHERE Id='801m0000001XjDX')])
             {
                wrapProductList.add(new wrapProduct(a));
             }
                      
         }
     }
 
    public void processSelected()
     {
    selectedAccounts= new List<PricebookEntry>();
 
        for(wrapProduct wrapAccountObj : wrapProductList)
         {
            if(wrapAccountObj.selected == true)
             {
                selectedAccounts.add(wrapAccountObj.acc);
                           
             }
         }
      
     }
     
    public PageReference orderP(){
    
      PageReference orderP = new PageReference('https://cs20.salesforce.com/home/home.jsp');
      
      orderP.setRedirect(true);
    
      return orderP;
     
     }
    public class wrapProduct{
        public PricebookEntry acc {get; set;}
        public Boolean selected {get; set;}
 
        public wrapProduct(PricebookEntry a) {
            acc = a;
            selected = false;
        }
    }
}

Please help its urgent
Pankaj_GanwaniPankaj_Ganwani
hey Amit,

Simply pass this.checked from this statement:

<apex:inputCheckbox value="{!accWrap.selected}" id="inputId" onChange="validatePrice('{!acc.UnitPrice}',this.checked);"/>  

 function validatePrice(price, value)
{
      if(value==true && price == null)
         alert('Price cannot be changed.');
}
Amit Visapurkar 5Amit Visapurkar 5
@pankaj can you please check at your end if it's working.