[SalesForce] Delete Row with confirm dialogbox

In JavaScript function all the alert showing and id is getting but for controller method is show's " DML statement found null SObject at position 0" Exception.

Controller

       public class ContactListViewController{

    Public List<Contact> conList{get;set;}
    Public String text{get;set;}
    public String delRecordId{get;set;}

    public ContactListViewController(){
        conList = new List<Contact>();
        conList = [SELECT id,FirstName,LastName,Email,Phone From Contact];    
    }

    public pageReference ShowRecord(){   

        String QueryString= 'SELECT id,FirstName,LastName,Email,Phone From Contact Where FirstName Like \''+text+'%\''; 
        conList = Database.query(QueryString);
        return null;
    }

    public void DelRecord(){
        System.debug('-->del'+delRecordId);
        Database.delete(delRecordId);

    }
}

VisualForce Page

          <apex:page controller="ContactListViewController">
            <apex:form >

      <apex:actionstatus id="counterStatus">
        <apex:facet name="start">
            <div class="waitingSearchDiv" id="el_loading" style="background-color: #fbfbfb; height:100%;opacity:0.65;width:100%;">
                <div class="waitingHolder" style="top: 100px; width: 91px;">
                <img class="waitingImage" src="/img/loading.gif" title="Please Wait..." />
                <span class="waitingDescription">Loading...</span>
                </div>
            </div>
        </apex:facet>
    </apex:actionstatus>

    <script>
        function deleteRow(recordId) {
            alert('hello'+recordId);
                var r = confirm("Are you Sure?");
                alert(r);
                if (r) {
                    alert('hi');
                    DelRecordAction(recordId);
                }
      }    
    </script>

     <apex:actionFunction name="DelRecordAction" action="{!DelRecord}" status="counterStatus">
                <apex:param assignTo="{!delRecordId}" name="prm" value="" />
            </apex:actionFunction>
      <apex:pageBlock id="pb">              
              <apex:pageblockTable value="{!conList}" var="con">
                  <apex:column headerValue="Action">
                      <apex:outputLink value="/{!con.id}/e" target="_blank"> 
                          Edit
                      </apex:outputLink> |
                      <a href="" onclick="deleteRow('{!con.id}')">Del</a>
                  </apex:column>
                  <apex:column headerValue="First Name" value="{!con.FirstName}"/>
                  <apex:column headerValue="Last Name" value="{!con.LastName}"/>
                  <apex:column headerValue="Email" value="{!con.Email}"/>
                  <apex:column headerValue="Phone" value="{!con.Phone}"/>              
              </apex:pageblockTable>
      </apex:pageBlock>
  </apex:form>
</apex:page>

Best Answer

You have forgot to add reRender="pb" in action function.

 <apex:actionFunction name="DelRecordAction" action="{!DelRecord}" status="counterStatus" reRender="pb">
            <apex:param assignTo="{!delRecordId}" name="prm" value="" />
 </apex:actionFunction>
Related Topic