1

I have to create a select all checkbox in below visualforce code to selct/deselect mass record, i tried with Javascript but its not working.

Page

<apex:page Controller="Copy_Opportunity_Line_Item_Into_Quote" standardStylesheets="true">
    <apex:pageMessages />
    <script>
        var isClicked = false;
        function checkDoubleSubmit(obj){
            if (isClicked) {
                //alert('You clicked before.');//For testing message only.
                return false;
            }else {
                isClicked = true;
                obj.className = 'btnDisabled';//only shows the button as disabled.
            }
        }

          var $j = jQuery.noConflict();
           function toggleCheckAll(checkAllBox) {
            $j('.checkableTable :checkbox').prop('checked', checkAllBox.checked);
          }
    </script>    
    <apex:sectionHeader title="Select Available Opportunity Line Items"/>
    <apex:form id="theSelectionForm">
        <apex:pageBlock rendered="{!OLIExist}">
            <apex:outputText value="Select Opportunity Line Item to copy into Quotes (maximum 1000 records can be copied)" styleClass="blackChar"/>
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="Copy" action="{!selectedCopy}" onclick="checkDoubleSubmit(this);"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>    
            <apex:pageBlockTable value="{!OLIs}" var="Lineitem">

                   <apex:column >
                    <apex:facet name="Select">
                     <input type="checkbox" id="checkAllBox" onclick="toggleCheckAll(this)"/>
                    </apex:facet> 
                   <apex:inputCheckbox value="{!qlines.checked}"/>
                   </apex:column>

                <apex:column value="{!Lineitem.sOLI.PricebookEntry.ProductCode}" headerValue="Product"/>
                <apex:column value="{!Lineitem.sOLI.PricebookEntry.Product2Id}" headerValue="Product Id"/>
                <apex:column value="{!Lineitem.sOLI.Quantity}" headerValue="Quantity"/>
            </apex:pageBlockTable>
        </apex:pageBlock>

        <apex:pageBlock rendered="{!NOT(OLIExist)}">
            <apex:outputText value="There are no new opportunity line items to copy" />
            <apex:pageBlockButtons >    
                <apex:commandButton value="Back" action="{!cancel}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
    <style>
        .blackChar{
            color: black;
            font-weight: bold;
            font-size: 14px;
            padding:5px;
        }
    </style>
</apex:page>

Controller :

public with sharing class Copy_Opportunity_Line_Item_Into_Quote {
    public Quote__c quote;
    public Quote_Line_Item__c[] quoteLineItems;
    public Opportunity opp;
    public boolean OLIExist{get;set;} 
    public List<OLI_Select_Wrapper> wrapOLI = New List<OLI_Select_Wrapper>();
    public Boolean testMode = false;

    public Copy_Opportunity_Line_Item_Into_Quote() {
        quote = [Select Id, Opportunity__c From Quote__c where Id = :ApexPages.CurrentPage().getParameters().get('id')];
        if(quote.Opportunity__c == null){
            OLIExist = false;
        } else {
            opp = [Select Id From Opportunity Where Id = :quote.Opportunity__c];
            OLIExist = false;

            Set<id> qliIds = new Set<id>();   
            List<Quote_Line_Item__c> QLIProd = [Select s.Product__c From Quote_Line_Item__c s where s.Quote_Holcim__c = :quote.Id];

            for(Quote_Line_Item__c qli: QLIProd) {
                qliIds.add(qli.Product__c);
            } 

            if(wrapOLI.size() == 0)  {
                List<OpportunityLineItem> OppLines =  [Select id,Quantity,PricebookEntry.Product2Id,PricebookEntry.Name, OpportunityId, UnitPrice,Customer_Material_Description__c,
                                                              Plant_Description__c, Description, Create_CMIR_for_Ship_to__c,Copy_price_for_similar_mixes_Prod_Hi__c,
                                                              Copy_price_for_similar_mixes_MPG__c, PricebookEntry.ProductCode
                                                         From OpportunityLineItem where OpportunityId = :opp.Id and PricebookEntry.Product2Id NOT IN :qliIds];
                for(OpportunityLineItem line: OppLines) {
                    OLI_Select_Wrapper objWrap = new OLI_Select_Wrapper(line);
                    wrapOLI.add(objWrap);
                }
            }
            if (wrapOLI.size()>0){
                OLIExist = true;
            }
            else{
                OLIExist = false;
            }
        }
    }

   /* public String getOLIExist() {
        return null;
    }*/

    public PageReference selectedCopy() {
        List<Quote_Line_Item__c> qlines = new List<Quote_Line_Item__c>();
        system.debug('before copy and wrapOLI :: ' +wrapOLI);
        for (OLI_Select_Wrapper selItems : wrapOLI ){
            if(selItems.selected==true){        
                Quote_Line_Item__c ql = new Quote_Line_Item__c(Quote_Holcim__c = quote.id,Quantity__c = selItems.sOLI.Quantity,
                                                               Override_Price_per_unit__c=selItems.sOLI.UnitPrice,Product__c = selItems.sOLI.PricebookEntry.Product2Id,
                                                               Customer_Material_Description__c = selItems.sOLI.Customer_Material_Description__c,
                                                               Create_CMIR_for_Ship_to__c = selItems.sOLI.Create_CMIR_for_Ship_to__c, 
                                                               Opportunity_line_Description__c = selItems.sOLI.Description,
                                                               Copy_price_for_similar_PH__c = selItems.sOLI.Copy_price_for_similar_mixes_Prod_Hi__c, 
                                                               Copy_price_for_similar_MPG__c = selItems.sOLI.Copy_price_for_similar_mixes_MPG__c);                                                                
                qlines.add(ql); 
            }
        }
        if(qlines.size() > 0){
            try{
                insert qlines;
                PageReference quotePage = new ApexPages.StandardController(quote).view();
                quotePage.setRedirect(true);
                return quotePage;
            }catch(System.DMLException e){
                 ApexPages.addMessages(e);
                 return null;
            }
        }
        PageReference quotePage = new ApexPages.StandardController(quote).view();
        quotePage.setRedirect(true);
        return quotePage; 
    }

    public List<OLI_Select_Wrapper> getOLIs() {
        for (OLI_Select_Wrapper selItems : wrapOLI){
            selItems.selected = true;
        }
        return this.wrapOLI;
    }

    public PageReference cancel() {
        PageReference quotePage = new ApexPages.StandardController(quote).view();
        quotePage.setRedirect(true);
        return quotePage;        
    }

    public static testmethod void testCopyOLIintoQuote(){
        Plant__c plantX = new Plant__c (Name='SF13402',Plant__c='Tstplnt1');
        insert plantX; 
        Plant__c plantX1 = new Plant__c (Name='SF13403',Plant__c='Tstplnt2');
        insert plantX1;

        Zone__c zneX = new Zone__c(Name='TstZne1a',SAP_Zone_Identifier__c='SAP12341');
        insert zneX;

        Product2 prodX = new Product2(name='Cement Test1',Plant__c=plantX.Id,ProductCode='1231212',SAP_Plant_Product_Key__c='SAP123123',SAP_Material_Number__c='Inder100034',Unit_of_Measure__c='Tonne',Description='Test product description');
        insert prodX;
        Product2 prodX1 = new Product2(name='Cement Test2',Plant__c=plantX.Id,ProductCode='1231213',SAP_Plant_Product_Key__c='SAP123124',SAP_Material_Number__c='Inder100035',Unit_of_Measure__c='KG',Description='Test product description');
        insert prodX1;
        Product2 prodX2 = new Product2(name='Cement Test3',Plant__c=plantX.Id,ProductCode='1231214',SAP_Plant_Product_Key__c='SAP123125',SAP_Material_Number__c='Inder100036',Unit_of_Measure__c='Ton',Description='Test product description');
        insert prodX2;

        list<Pricing__c> pricing = new list<Pricing__c>();
        Pricing__c temppri1 = new Pricing__c(price__c=45.00,product__c=prodX.Id,Pricing_End_date__c=date.newinstance(2011,12, 31),Pricing_Start_date__c=date.newinstance(2010,01, 01),SAP_Pricing_record__c='SAP12313');
        insert temppri1;
        Pricing__c temppri21 = new Pricing__c(price__c=145.00,product__c=prodX1.Id,Pricing_End_date__c=date.newinstance(2011,12, 31),Pricing_Start_date__c=date.newinstance(2011,01, 01),SAP_Pricing_record__c='SAP12314');
        insert temppri21;
        Pricing__c temppri22 = new Pricing__c(price__c=146.00,product__c=prodX2.Id,Pricing_End_date__c=date.newinstance(2012,12, 31),Pricing_Start_date__c=date.newinstance(2011,01, 01),SAP_Pricing_record__c='SAP12315');
        insert temppri22;

        PriceBook2 PB = [select Id from PriceBook2 where isStandard = true];
        PricebookEntry PBE = new PricebookEntry(UnitPrice=25.00,Product2Id=prodX.Id,Pricebook2Id=PB.Id,IsActive=true);
        insert PBE;
        PricebookEntry PBE1 = new PricebookEntry(UnitPrice=25.00,Product2Id=prodX1.Id,Pricebook2Id=PB.Id,IsActive=true);
        insert PBE1;       
        PricebookEntry PBE2 = new PricebookEntry(UnitPrice=26.00,Product2Id=prodX2.Id,Pricebook2Id=PB.Id,IsActive=true);
        insert PBE2;   

        Account acctX = new Account(name='Test Account');
        insert acctX;

        Opportunity oppX = new Opportunity(name='Test Opportunity',AccountId=acctX.Id,CloseDate=date.newInstance(2012, 12, 31),StageName='Prospecting');
        insert oppX;


        Quote__c qteX = new Quote__c(Start_Date_of_Material_Required__c =date.newinstance(2011,10,01),Quote_Stage__c='Draft',Quote_Date__c=date.newinstance(2011,09,01),Opportunity__c=oppX.Id,
                                    End_Use_Category__c='Residential',Invoice_Split__c='030-Sold To/Ship To',End_Date_of_Material_Required__c=date.newinstance(2012,12,01),Account__c=acctX.Id,Zone__c=zneX.Id);
        insert qteX;  
        Quote_Line_Item__c qteLine = new Quote_Line_Item__c(Quote_Holcim__c=qteX.Id,Quantity__c = 5003,Product__c=prodX2.Id,Copy_price_for_similar_PH__c = false, Copy_price_for_similar_MPG__c=true);
        insert qteLine;

        PageReference pageRef = Page.CopySelectedOpportunityLineItemIntoQuote;
        pageRef.getParameters().put('Id',qteX.Id);
        Test.setCurrentPageReference(pageRef);      
        Copy_Opportunity_Line_Item_Into_Quote CopyOli = new Copy_Opportunity_Line_Item_Into_Quote();

        OpportunityLineItem OliX = new OpportunityLineItem(UnitPrice=34.00,Quantity=5000,PricebookEntryId=PBE.Id,OpportunityId=oppX.Id,Description='test copy opp line items');
        insert OliX;
        OpportunityLineItem OliX1 = new OpportunityLineItem(UnitPrice=44.00,Quantity=5001,PricebookEntryId=PBE1.Id,OpportunityId=oppX.Id,Description='test copy opp line items1');
        insert OliX1;
        OpportunityLineItem OliX2 = new OpportunityLineItem(UnitPrice=41.00,Quantity=5002,PricebookEntryId=PBE2.Id,OpportunityId=oppX.Id,Description='test copy opp line items2');
        insert OliX2;

        CopyOli = new Copy_Opportunity_Line_Item_Into_Quote();
        List<OLI_Select_Wrapper> selOLI = CopyOli.getOLIs();
        pageReference savePage = CopyOli.selectedCopy();

        CopyOli = new Copy_Opportunity_Line_Item_Into_Quote();
        pageReference savePage1 = CopyOli.selectedCopy();

        CopyOli = new Copy_Opportunity_Line_Item_Into_Quote();
        pageReference cancelPage = CopyOli.cancel();
    }
}
2
  • From looking at your code, it's unclear if you still want to be able to select and unselect individual lines. It appears as though you've lost that ability. Please clarify your question as to what you're having the difficulty with. You potentially don't need JQuery to do this for you and can do it all in your controller simply by using the value of that select option box to tell the controller to select all records in the select option list.
    – crmprogdev
    Dec 26 '16 at 14:35
  • Yes, i want that functionality as well as the mass select functionality.
    – Rakesh
    Dec 27 '16 at 7:31
2

The problem is this line right here:

$j('.checkableTable :checkbox').prop('checked', checkAllBox.checked);

First, you're not using the checkableTable style in your page markup, and secondly, :checkbox is not a valid pseudo class.

Instead, you're looking for something like this:

               <apex:column >
                <apex:facet name="Select">
                 <input type="checkbox" id="checkAllBox" onclick="toggleCheckAll(this)"/>
                </apex:facet> 
               <apex:inputCheckbox styleClass="lineItemCheckbox" value="{!qlines.checked}"/>
               </apex:column>

With the appropriate change to your script:

$j('.lineItemCheckbox').prop('checked', checkAllBox.checked);
2
  • Thanks for the reply. i tried the above changes but getting the error as the " Unknown property 'Copy_Opportunity_Line_Item_Into_Quote.qlines' which i think should not come, as i have already declared the method name in my controller. Please suggest.
    – Rakesh
    Dec 27 '16 at 7:34
  • @Rakesh This answer doesn't address any additional errors; you're using getOLIs to generate the data from the controller. You have many issues with your code you'll need to address to get a working page. It should probably be something more like <apex:inputcheckbox styleClass="lineItemCheckbox" value="{!Lineitem.selected}" /> instead. You might need to add additional questions on this site to sort through all the problems you have.
    – sfdcfox
    Dec 27 '16 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.