Tuesday 2 September 2014

WrapperClass to delete Selected Account in Salesforce

Hi Guys..

This time lets start with wrapper class in salesforce. Like java, we also have wrapper class in salesforce. to know more about wrapper class Clickhere . So I got some requirements on social networking groups. Let me walk you throgh the scenario.


Scenario:

Lets say you have some list of accounts and you have to delete some of the selected accounts using check box. 

Solution: 

I ll provide you with the whole code but first lets understand the way how we ll be going to proceed . Basically  a roadmap.

1. we need to first display the listy of acccounts.
2. We need to have a column of check boxes to select the accounts that we need to display.
3. Once a check box is checked then we need to delete them by clicking on delete  the button .

Here is the code for you guys


public class DeleteWrapper{
    public List<wrapperDelete> wrp;
    public  List<wrapperDelete> getAccnt(){
        //List<wrapperDelete> wrp = new List<wrapperDelete>();
        if(wrp == null){
        wrp = new List<wrapperDelete>();
        for(account acct:[Select id, name from Account ]){
        wrp.add(new wrapperDelete(Acct));
        }
      }
        system.debug(wrp);
        return wrp;
    }
    public class wrapperDelete{
        public Boolean selected {get; set;}
        //public  List<Account> lst {get; set;}
        public Account lst {get; set;}
        //public wrapperDelete(List<Account> lstact){
        public wrapperDelete(Account lstact){
            selected = false;
            lst = lstact;
        }
    }
    public pageReference Deletes(){
        list<Account> SelectedAccount = new List<Account>();
        system.debug('=='+getAccnt());
        for(wrapperDelete wd : getAccnt()){
            if(wd.selected == true){
                SelectedAccount.add(wd.lst);
            }
        }
        system.debug('=='+SelectedAccount);
        delete SelectedAccount;
        wrp = null;
        system.debug('===='+SelectedAccount);
        return null;
    } 
}


Let me explain you line by line .

We have wrapper class with name wrapperDelete which contains list of account  and checkboxes. In its constructor we have passed account object as parameter and made selection as false . So whenever the  page is loaded check boxes are unchecked.

Now above we have created a list of wrapper class to add accounts.

here is the visual force code :

<apex:page controller="DeleteWrapper">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:pageBlockTable value="{!Accnt}" var="it" >
                      <apex:column >
                         <apex:inputCheckbox value="{!it.selected}"/>
                      </apex:column>
                      <apex:column value="{!it.lst.name}"/>
                      <apex:column value="{!it.lst.id}"/>
                </apex:pageblockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
      <apex:commandButton action="{!Deletes}" value="Deletes" />
    </apex:form>    
</apex:page>

If you have any querry regarding the above post. please Feel free to contact me


happy coding!!


Cheers
Ankush
ankushsalesforce@gmail.com

oAuth Use Case -  Headless API Explained in the Easiest way Wondering why? and When we use the Headless API flow Use case example: Imagine a...