Object Classes in Salesforce
Object class in salesforce

Object Classes in Salesforce

At the heart of OOP is the type of class which represents a single object . These classes store attributes as well as methods specific to that object. Object classes can be created as stand-alone classes,complete with constructors, or they can be nested within other classes—commonly referred to as wrapper classes —and used to “wrap” a subset of attributes into an object class that is only relevant when used within the specific class (or context) where it appears.

A common example for using object classes on Force.com is to create representations of request and response objects from external services. However, they can also be used to “wrap” together certain pieces of information in order to facilitate some other functionality. For example, let us assume we are building a custom Visualforce page that will allow us to select multiple Contact records. We would need to store a boolean attribute for selected. With that in mind, the following wrapper class would ease our task.

public class contactWrapper{ 
	public Contact wrappedContactRecord; 
	public boolean selected = false;
}

With this wrapper class in place, we can now instantiate a list collection of contactWrapper instead of Contacts: list myListOfContacts rather than list myListOfContacts. Again, remember that this contactWrapper class can be a stand-alone class, or nested within another class. It can also, but does not necessarily need to, have a constructor, or multiple overloaded constructors as shown below:

/**
 * contactWrapper 
 * @description class to contain a "selected" boolean
   matched to a specific Contact record 
*/ 
public class contactWrapper{ 
public Contact wrappedContactRecord;
 public boolean selected = false;
//simple constructor to assign the contact
 public contactWrapper(Contact inputContact){
 wrappedContactRecord = inputContact; 
}

//constructor to assign contact and selected boolean 
public contactWrapper(Contact inputContact, boolean inputBoolean){ 
wrappedContactRecord = inputContact;
 selected = inputBoolean;
 }

 //default empty constructor, instantiates with a new blank Contact
 public contactWrapper(){ 
wrappedContactRecord = new Contact();
 } 
}

Now that we can instantiate new contactWrappers, it might make sense to include methods which will allow us to toggle the selected state. Let us add methods for select and deselect operations to the wrapper class as shown below:

//mark this wrapper as selected 
public void select(){ 
this.selected = true; 
} 
//mark this wrapper as deselected 
public void deselect(){
 this.selected = false; 
} 

// can you tell what this does? 
public void toggleSelectedState(){
 this.selected = (this.selected)?false:true;
 }
 

We have added a third method called toggleSelectedState. Can you tell what it does? Using a ternary logic operator, it flips the value of selected. If it is currently selected, it deselects, and vice versa, which is akin to how checkboxes work.

 

Previous Post 

Leave a Reply

Close Menu