Wrapper Class

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

public class wrapperClassController {

02
03
//Our collection of the class/wrapper objects cContact
04
public List<cContact> contactList {get; set;}
05
06
//This method uses a simple SOQL query to return a List of Contacts
07
public List<cContact> getContacts() {
08
if(contactList == null) {
09
contactList = new List<cContact>();
10
for(Contact c: [select Id, Name, Email, Phone from Contact limit 10]
) {
11
// As each contact is processed we create a new cContact object
and add it to the contactList
12
contactList.add(new cContact(c));
13
}
14
}
15
return contactList;
16
}
17
18
19
public PageReference processSelected() {
20
21
//We create a new list of Contacts that we be populated only wit
h Contacts if they are selected
22
List<Contact> selectedContacts = new List<Contact>();
23
24
//We will cycle through our list of cContacts and will check to see if t
he selected property is set to true, if it is we add the Contact to the selected
Contacts list
25
for(cContact cCon: getContacts()) {
26
if(cCon.selected == true) {
27
selectedContacts.add(cCon.con);
28
}

29
}
30
31
// Now we have our list of selected contacts and can perform any type of
logic we want, sending emails, updating a field on the Contact, etc
32
System.debug('These are the selected Contacts...');
33
for(Contact con: selectedContacts) {
34
system.debug(con);
35
}
36
contactList=null; // we need this line if we performed a write operation
because getContacts gets a fresh list now
37
return null;
38
}
39
40
41
// This is our wrapper/container class. A container class is a class, a data
structure, or an abstract data type whose instances are collections of other ob
jects. In this example a wrapper class contains both the standard salesforce obj
ect Contact and a Boolean value
42
public class cContact {
43
public Contact con {get; set;}
44
public Boolean selected {get; set;}
45
46
//This is the contructor method. When we create a new cContact object we
pass a Contact that is set to the con property. We also set the selected value
to false
47
public cContact(Contact c) {
48
con = c;
49
selected = false;
50
}
51
}
52
}

You might also like