View

Wednesday, May 1, 2013

Apex Code : Add and Delete accounts....................



Add And Delete accounts using page::

page:
----------

<apex:page controller="DataTableEditRemoveController" sidebar="false" showHeader="false">
<apex:form id="form" >
    <apex:pageBlock title="Accounts">
        <apex:pageBlockTable value="{!accs}" var="row">
         <apex:column >
           <apex:outputLink value="https://ap1.salesforce.com/003/e?retURL=%2F003%2Fo" style="font-weight:bold">Add</apex:outputLink>&nbsp;|&nbsp;
           <apex:outputLink title="" value="/{!row.id}/e?retURL=/apex/{!$CurrentPage.Name}" style="font-weight:bold">Edit</apex:outputLink>&nbsp;|&nbsp;
           <a href="javascript:if (window.confirm('Are you sure?')) DeleteAccount('{!row.Id}');" style="font-weight:bold">Del</a>
         </apex:column>
         <apex:column value="{!row.Name}"/>
         <apex:column value="{!row.BillingStreet}"/>
         <apex:column value="{!row.BillingCity}"/>
         <apex:column value="{!row.BillingPostalCode}"/>
         <apex:column value="{!row.BillingCountry}"/>
  </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>


controller::
-----------------

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 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;
          break;
       }
     
      //if account record found delete it
      if (tobeDeleted != null) {
       Delete tobeDeleted;
      }
   
      //refresh the data
      LoadData();
   }  
 
 
}

No comments:

Post a Comment

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