3

I'm trying to seek some help with the following VF page and the controller behind it, Basically, the outline of this controller is 3 pages...page 1 to capture account info, page 2 to capture multiple contacts for this acct and page 3 to just show the confirmation page....and give users the option to delete the contacts one by one by with a command button.

Now, neither the OnClick action is working, nor is the method getting invoked (I'm unable to see the "inside deleteCont" message in the debug logs) and the re-rerender is not working too...Please advise on what is it I'm missing.

<apex:page controller="newOpportunityController">
    <apex:sectionHeader title="Confirmation" subtitle="Step 3 of 3"/>
    <apex:form >
    <apex:pageMessages />
        <apex:variable var="rowNumber" value="{!0}"/>
        <apex:pageBlock title="Household Information" id="pb1">

            <apex:pageBlockTable id="pbt1" value="{!acct}" var="a" title="Household Information">
            <apex:column value="{!a.name}" />
            <apex:column value="{!a.phone}" />
            <apex:column value="{!a.Email_address__c}" />
            <apex:column headerValue="Action" >
                <apex:commandButton value="Delete" action="{!deleteAcct}"
                                    onclick="return confirm('Are you sure? This will delete Household and all members') " >
                </apex:commandButton>
            </apex:column>    
        </apex:pageBlockTable>

         <apex:pageBlockTable id="pbt2" value="{!contactstoadd}" var="c" title="Member Information">
            <apex:column value="{!c.firstname} {!c.lastname}" headerValue="Name" />
            <apex:column value="{!c.Gender__c}" />
            <apex:column value="{!c.birthdate}" />
            <apex:column value="{!c.Birthstar__c}" />
            <apex:column headerValue="Action" >
                <apex:commandButton value="Delete" action="{!deleteCont}" reRender="pbt2"
                                    onclick="return confirm('Are you sure? This will delete member info') " >

                    <apex:param name="rowIndex" value="{!rowNumber}"/>
                </apex:commandButton>
                <apex:variable var="rowNumber" value="{!rowNumber+1}"/>
            </apex:column> 
        </apex:pageBlockTable>

       </apex:pageBlock>

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

//SPN
public class newOpportunityController {

 public Account acct {get;set;}
 public contact cont {get;set;}
 public List<Contact>contactstoadd{get;set;}
private static final integer maxContacts=5;
public Integer rowIndex {get;set;}
public pagereference page3;

 public static String str1{
    get{
        str1 =  'Are you sure? This will delete Household and all members'; 
        return str1;
    }
  set;
}
 public static String str2{
    get{
        str2 =  'Are you sure? This will delete Member Information'; 
        return str2;
    }
  set;
}
 public static String err1{
     get{
        err1 =  'Cannot insert more than 10 contacts. Please contact the admin';
        return err1;
    }
  set;
}         

 public newOpportunityController() {
  if(acct == null) 
      {acct = new Account();}
  if(cont == null)
      {cont = new Contact();}
   if (contactstoadd == null) 
      {contactstoadd = new list<contact>();}

  }

 public PageReference page1Next() {
  PageReference Page1;
    if (acct != null) {
        Page1 = new pagereference('/apex/page1?id='+acct.id);
        Page1.setRedirect(false);
    }
    else {
        Page1 = new pagereference('/apex/page1');
        page1.setRedirect(true);
    }

    return page1;

   }


 public PageReference page2Next() {
    PageReference Page2 = new pagereference('/apex/page2');
    Page2.setRedirect(false);
    return page2;
  }

public PageReference page3Next() {
    addOneContact();
    Page3 = new pagereference('/apex/page3');
    Page3.setRedirect(false);
    return page3;
 }



 public void addmorecontacts() {
   addOneContact();
   page2Next();

   }

public void addOneContact(){
    if (contactstoadd.size() < maxContacts)
    {
        contactstoadd.add(cont);
        cont = new contact();
    }
    else {

            throw new appHandlerException(err1);
   }
  }


  public pagereference deleteAcct() {
    acct=null;
    contactstoadd=null;
    cont=null;
    return page1Next();
   }

  public pagereference deleteCont() {
   system.debug('inside deleteCont');

   rowIndex =     
   Integer.valueOf(ApexPages.currentPage().getParameters().get('rowIndex'));
    System.debug('row to be deleted ' + rowIndex );
    System.debug('rowm to be deleted '+Contactstoadd[rowIndex]);
    contactstoadd.remove(rowIndex);
   return page3;

   }


  public void saveacct() {
      try
     {
      upsert acct;
      savecont();
      }
      catch (exception e)
      {
           ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity.ERROR, 'Account insert/update failed:' 
           + e.getMessage());
        ApexPages.addMessage(myMsg);
      }
     }

    public void savecont(){
     for (Contact con: contactstoadd)
       {
           con.accountid = acct.id;
        }
     try
       {
         upsert contactstoadd;
        }
     catch (exception e)
      {
           ApexPages.Message myMsg = new  
  ApexPages.Message(ApexPages.severity.ERROR, 'Contact insert/update failed'
           + e.getMessage());
        ApexPages.addMessage(myMsg);
      }
     }   

     }
  • Check if you have disabled popups because of which you may not be seeing js popups, debug messages may be missing depending on debug size – Rao Jun 23 '15 at 17:26
  • I think the problem u r facing because u r trying to call two events on command button. 1. OnClick event 2. Action method on command button So I think if you try to seperate these two events you can get exact output. – tandonprateek Jun 23 '15 at 17:30
  • TandonPrateek - Even without the Onclick event, the action method isn't firing. – JJSHHS Jun 23 '15 at 18:11
2

Your <apex:commandButton> onclick event should only return on the "cancel" action (i.e. return false).

The page is not being posted as the onclick event is returning control to the page rather than executing the form post, which is happening via additional javascript appended to the button after your script due to the rerender attribute, to the server.

You only want the return statement to be executed by the browser when the user cancels the action, which is written like this: if (!confirm('Are you sure? This will delete member info')){ return false; }.

If the user confirms the action, the Salesforce javascript appended to the onclick event will post the form data normally and the expected rerender behavior will take place with no return statement being executed.

<apex:commandButton value="Delete" action="{!deleteCont}" reRender="pbt2"
    onclick="if (!confirm('Are you sure? This will delete member info')){ return false; }">
  • Thanks Mark...the Onclick Prompt seems to be coming up. But the Delete action isn't working (I'm unable to see the debug statements in the log) and the contacts are not getting deleted. Thoughts? – JJSHHS Jun 23 '15 at 17:52
1

This might be a shot in the dark, but try wrapping your pageBlockTable in an outputPanel and pointing your button's rerender attribute to that. Something like:

   <apex:outputPanel id="pbt2wrapper">
       <apex:pageBlockTable id="pbt2" value="{!contactstoadd}" var="c" title="Member Information">
            <apex:column value="{!c.firstname} {!c.lastname}" headerValue="Name" />
            <apex:column value="{!c.Gender__c}" />
            <apex:column value="{!c.birthdate}" />
            <apex:column value="{!c.Birthstar__c}" />
            <apex:column headerValue="Action" >
                <apex:commandButton value="Delete" action="{!deleteCont}" reRender="pbt2wrapper"
                                    onclick="return confirm('Are you sure? This will delete member info') " >

                    <apex:param name="rowIndex" value="{!rowNumber}"/>
                </apex:commandButton>
                <apex:variable var="rowNumber" value="{!rowNumber+1}"/>
            </apex:column> 
        </apex:pageBlockTable>
   </apex:outputPanel >

I remember running into a similar issue myself, and I stumbled upon this blog post from Bob Buzzard which gave me the idea to wrap elements in an outputPanel which, IIRC, solved my issue.

  • I tried using an outputpanel wrapper and it still isn't working...Any other thoughts/ideas? – JJSHHS Jun 24 '15 at 5:44
  • Maybe try wrapping the entire pageblock? – kbentsen Jun 25 '15 at 20:04
1

In the onclick event, you are directly calling confirm dialog which will return whether the user clicked OK or cancel. But this return will stop the action method to invoke. So onclick="if(!confirm('Are you sure?')){return false};" will return only if the user click cancel. Else it won't return and the action will get invoked.

How to add a confirm dialog to a command button?

  • Thanks Vigneshwaran....I tried removing the Onclick event from the apex:command button and still the action is not firing...not sure, what's the error and no debug logs are showing up too. Here's my revised command btn snippet, <apex:commandButton value="Delete" action="{!deleteCont}" reRender="pbt2" > – JJSHHS Jun 23 '15 at 18:05
1

Change your onclick event as given in below snippet

        <apex:pageBlockTable id="pbt1" value="{!acct}" var="a" title="Household Information">
        <apex:column value="{!a.name}" />
        <apex:column value="{!a.phone}" />
        <apex:column value="{!a.Email_address__c}" />
        <apex:column headerValue="Action" >
            <apex:commandButton value="Delete" action="{!deleteAcct}"
                                onclick="if(!confirm('Are you sure? This will delete Household and all members?')){return};" >
            </apex:commandButton>
        </apex:column>    
    </apex:pageBlockTable>

     <apex:pageBlockTable id="pbt2" value="{!contactstoadd}" var="c" title="Member Information">
        <apex:column value="{!c.firstname} {!c.lastname}" headerValue="Name" />
        <apex:column value="{!c.Gender__c}" />
        <apex:column value="{!c.birthdate}" />
        <apex:column value="{!c.Birthstar__c}" />
        <apex:column headerValue="Action" >
            <apex:commandButton value="Delete" action="{!deleteCont}" reRender="pbt2"
                                onclick="if(!confirm('Are you sure? This will delete Household and all members?')){return}; " >

                <apex:param name="rowIndex" value="{!rowNumber}"/>
            </apex:commandButton>
            <apex:variable var="rowNumber" value="{!rowNumber+1}"/>
        </apex:column> 
    </apex:pageBlockTable>

   </apex:pageBlock>

  • Shebin...I have now removed the Onclick event but still the action method isn't being called nor is the re-render working....What puzzles me is why I'm not able to see anything in the debug log? Is there a way around it? This is just a navigation across 3 pages and I'm guessing a lot of logging isn't done. – JJSHHS Jun 24 '15 at 16:04
  • Can you update the code with onclick removed? The above code which I gave was perfectly working for me according to your need – Shebin Mathew Jun 24 '15 at 20:37
  • Shebin, can you post the code which was working...As I said, even after I removed the onclick event itself, the re-render is not working nor is the deleteCont method called. – JJSHHS Jun 25 '15 at 15:28
1

Below is the minimised code which is working for me please add your code line by line see where it getting affected.

<apex:page controller="newOpportunityController">
<apex:sectionHeader title="Confirmation" subtitle="Step 3 of 3"/>
<apex:form >
<apex:pageMessages />
    <apex:variable var="rowNumber" value="{!0}"/>
    <apex:pageBlock title="Household Information" id="pb1">

        <apex:pageBlockTable id="pbt1" value="{!acct}" var="a" title="Household Information">

        <apex:column headerValue="Action" >
            <apex:commandButton value="Delete" action="{!deleteAcct}"  onclick="return confirm('Are you sure? This will delete Household and all members') " >
            </apex:commandButton>
        </apex:column>    
    </apex:pageBlockTable>

     <apex:pageBlockTable id="pbt2" value="{!contactstoadd}" var="c" title="Member Information">

        <apex:column headerValue="Action" >
            <apex:commandButton value="Delete" action="{!deleteCont}" reRender="pbt2" onclick="if(!confirm('Are you sure?')){return};" >

             </apex:commandButton>
         </apex:column> 
    </apex:pageBlockTable>

   </apex:pageBlock>

Associated controller code.

    public class newOpportunityController{

  public List<contact> contactstoadd{get;set;}
  public List<contact> acct{get;set;}


  public newOpportunityController(){
      contactstoadd = new List<contact>();

      contactstoadd.add(new Contact());
      contactstoadd.add(new Contact());
      contactstoadd.add(new Contact());
      contactstoadd.add(new Contact());
      contactstoadd.add(new Contact());

  }

   public void deleteAcct(){


  }
   public void deleteCont(){

     system.debug('xyz');
  }
}

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.