[SalesForce] Command button and re-render not working

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);
      }
     }   

     }

Best Answer

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; }">
Related Topic