1

I have a visualforce page where records display with a checkbox, When i will select checkbox and click on a button , i want a popup where it will say no of records inserted.

Visualforce Page -

    <apex:page controller="soption1" sidebar="false"> 
<apex:messages style="color:red"></apex:messages>
<script>
  function confirmInsertion() {
      var numRecords = 0;      
      var checkboxes = document.getElementsByTagName("input");
      for (var i = 0; i < checkboxes.length; i++) {
      if (checkboxes[i].getAttribute("checked") == 'checked') {
          numRecords++;
      }
      }
      return confirm('This will create ' + numRecords + ' records.');
  }
</script>
    <apex:form id="theform">


        <apex:pageblock mode="edit">
        <apex:pageBlockSection title="Opportunity Information">
    <apex:panelGrid columns="2">
      <apex:outputLabel value="Opportunity Name"
                for="opportunityName"/>
      <apex:inputField id="opportunityName"
               value="{!Contact.name}"/>

    </apex:panelGrid>
      </apex:pageBlockSection>
        <apex:pageblockSection >
           <apex:inputfield value="{!objAct.Coe__c}" />
           <apex:inputfield value="{!objAct.Dept__c}" />
           <apex:inputfield value="{!objAct.OpCo__c }" />
        </apex:pageblockSection>

        <apex:pageBlockButtons >
            <apex:commandButton value="Search" action="{!search}" rerender="theform"/>
        </apex:pageBlockButtons>
        </apex:pageblock>

        <apex:pageBlock >
        <apex:pageBlockButtons >
            <apex:commandButton value="Process Selected" action="{!processSelected}" rerender="table" onclick="if(!confirmInsertion()){return};"/>
        </apex:pageBlockButtons>
        <apex:pageBlockTable value="{!MSAWrapperList}" var="obj" id="table">
             <apex:column >
            <apex:inputCheckbox value="{!obj.selected}" id="input"/>
            </apex:column>
            <apex:column value="{!obj.msaObj.Name}" />
        </apex:pageBlockTable>
        </apex:pageBlock>

    </apex:form>
</apex:page>

Apex Class -

public with sharing class soption1
    {

  public soption1() {
    account=new SLA__c();
    contact=new SOC__c();
    opportunity=new MSA__c();
    records=new List<MSA__C>();
    objAct=new MSA__c();
    msawrapperlist=new List<MSAWrapper>();
    }

    public SLA__c account{set;get;}
    public SOC__c contact{set;get;}
       public MSA__c opportunity{set;get;}

    public List<MSA__C> records{get;set;}
    public MSA__c objAct{get;set;}
    public List<MSAWrapper> MSAWrapperList {get; set;}

    public soption1(ApexPages.StandardController controller) 
    {
        objAct = new MSA__C();
        MSAWrapperList = new List<MSAWrapper>();
    }

    public SLA__c getAccount() {
    if(account == null) account = new SLA__c();
    return account;
    }

    public SOC__c getContact() {
    if(contact == null) contact = new SOC__c();
    return contact;
       }

    public PageReference step1() {
    return Page.newOpptyStep11;
    }

    public PageReference step2() {
      return Page.testmulti1;
    }


    public pagereference search()
    {
        MSAWrapperList.clear();
        records=[Select ID,Name,Coe__c,Dept__c,OpCo__c from MSA__C  where Coe__c=: objAct.Coe__c and Dept__c=: objAct.Dept__c];
        for(MSA__C obj : records)
        {
        MSAWrapperList.add( new MSAWrapper(obj) );
        }
       return null;
    }

    public PageReference processSelected() 
    {

      account.Name = contact.Name;
      insert account;

        List<MSA__C> selectedMSA = new List<MSA__C>();
        for(MSAWrapper cCon: MSAWrapperList ) 
        {
        if(cCon.selected == true) 
        {
            selectedMSA.add(cCon.msaObj);
        }
        }
        for(MSA__C msa: selectedMSA) 
        {
            system.debug(msa);
            SOC__c so = new SOC__c();
            so.Name = msa.Name;
            so.SLA__c = account.id;
            msa.SLA__c = account.id;
            insert so;
            update msa;
        // Please add your login here
        }
        return null;


    }


    public class MSAWrapper 
    {
        public MSA__C msaObj {get; set;}
        public Boolean selected {get; set;}
        public MSAWrapper(MSA__C c) {
        msaObj = c;
        selected = false;
        }
    }

    }

Please help.

Regards

2
  • Could you post what you have tried? Commented Jun 12, 2015 at 18:25
  • Hi , I have updated my code.. Please check Commented Jun 12, 2015 at 18:34

2 Answers 2

1

You could add onclick handler to the button, where you would run a javascript function counting all of your checked checkboxes, and then popup an alert() or confirm().

Something like this placed on your VF page should work. You may need to fix it up a little - I didn't compile or test it.

<apex:commandbutton value="Create Records" action="{!createRecords}" onclick="if(!confirmInsertion()){return};"/>
<script>
  function confirmInsertion() {
      var numRecords = 0;      
      var checkboxes = document.getElementsByTagName("input");
      for (var i = 0; i < checkboxes.length; i++) {
          if (checkboxes[i].getAttribute("checked") == 'checked') {
              numRecords++;
          }
      }
      return confirm('This will create ' + numRecords + ' records.');
  }
</script>

/////////////// UPDATE ////////////

I dunno why, but for some reason clicking checkboxes on this particular page doesn't update the "checked" attribute in the source of the page. Weird. So, a different solution, and this one i did compile and test is this:

change the checkbox:

<apex:inputCheckbox value="{!obj.selected}" id="input" onclick="incrementCounter(this.checked);"/>

change the button:

<apex:commandButton value="Process Selected" action="{!processSelected}" rerender="table" onclick="if(!confirmInsertion()){return};"/>

change the script section:

var counter = 0;

  function incrementCounter(checked) {
      if (checked) {
          counter++;
      } else {
          counter--;
      }
  }

  function confirmInsertion() {
      return confirm('This will create ' + counter + ' records.');
  }
2
  • Hi Kirill.. I have updated the code.. but still it is not working...can u plzz check.. Commented Jun 12, 2015 at 18:57
  • ok, i've updated the answer, should work for you now. Commented Jun 12, 2015 at 20:17
0

you can take a look at this link to know how to get a popup in vf page. https://developer.salesforce.com/forums/?id=906F0000000AYCyIAO

You can change the functionality in the controller to get the count of selected records.

You must log in to answer this question.

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