View

Wednesday, May 1, 2013

Apex Coding on Account Delete .....



apex class:
===========
public class DataTableEditRemoveController
{

   public List<Account> accs { get; set; }

   //used to get a hold of the account record selected for deletion
   public string SelectedAccountId { get; set; }

  public DataTableEditRemoveController()
  {
       //load account data into our DataTable
       LoadData();
   }

   private void LoadData() {
       accs = [Select id, name, BillingStreet, BillingCity, BillingPostalCode, BillingCountry ,(SELECT subject FROM tasks order by lastmodifieddate desc limit 1)from Account limit 20];
   }

   public void DeleteAccount()
   {
      // if for any reason we are missing the reference
      if (SelectedAccountId == null) {
   
         return;
      }
   
      // find the account record within the collection
      Account tobeDeleted = null;
      for(Account a : accs)
       if (a.Id == SelectedAccountId) {
          tobeDeleted = a;
           System.debug('tobeDeleted  Delete: ' + tobeDeleted );
          break;
       }
   
      //if account record found delete it
      if (tobeDeleted != null) {
       Delete tobeDeleted;
      }
   
      //refresh the data
      LoadData();
   }  

  }

Page:
=====
<apex:page controller="DataTableEditRemoveController">
<apex:form id="form" >
<apex:pageBlock title="Accounts">
  <apex:pageMessages ></apex:pageMessages>
  <apex:pageBlockTable value="{!accs}" var="row">
   
     <apex:column value="{!row.Name}"/>
   
     <apex:column value="{!row.BillingStreet}"/>
     <apex:column value="{!row.BillingCity}"/>
   
     <apex:column value="{!row.BillingCountry}"/>
   
     <apex:column headerValue="Recent Activity">
                  <apex:dataTable value="{!row.Tasks}" var="s" >
                      <apex:column value="{!s.subject}" style="border-bottom-color:transparent;" />
                  <a href="/00T/e?who_id={!row.id}&retURL=%2F{!row.id}">Next Activity </a>
                  </apex:dataTable>
                  </apex:column>
               
     <apex:column headerValue="Action"  >
     <apex:CommandButton value="New" onclick="openpopup1()"/>
     <script type="text/javascript">
     function openpopup1()
{
var AccountWindow = window.open('{!URLFOR($Page.QuickCreateAccount)}','Account','height=500,width=500,status=yes,scrollbars=yes,resizable=yes');
setTimeout("openpopup1()",100);
LeadWindow.focus();
}
</script>
      <a href="/00T/e?who_id={!row.id}&retURL=%2F{!row.id}">Next Activity </a>

       <a href="javascript:if (window.confirm('Are you sure?')) DeleteAccount('{!row.Id}');" style="font-weight:bold">Delete</a>
 
    <apex:outputLink title="" value="/{!row.id}/e?retURL=/apex/{!$CurrentPage.Name}" style="font-weight:bold">Edit</apex:outputLink>&nbsp;|&nbsp;
     </apex:column>
               
               
  </apex:pageBlockTable>
</apex:pageBlock>

<apex:actionFunction action="{!DeleteAccount}" name="DeleteAccount" reRender="form" >
   <apex:param name="accountid" value="" assignTo="{!SelectedAccountId}"/>
</apex:actionFunction>
</apex:form>
</apex:page>

No comments:

Post a Comment

Share your Comments .............