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 

This Post Has 10 Comments

  1. BestAnnette

    I see you don’t monetize mytutorialrack.com, don’t waste your traffic, you can earn additional cash every month with new monetization method.

    This is the best adsense alternative for any type
    of website (they approve all sites), for more info simply
    search in gooogle: murgrabia’s tools

  2. berkey

    I just like the helpful info you supply on your articles. I will bookmark your weblog and test once more here regularly. I am somewhat certain I’ll be informed a lot of new stuff proper right here! Good luck for the following!|

  3. Darius Litwiler

    I recently came across your website and have been reading along. I thought I could leave my first comment. I don’t know what to say except that I have enjoyed scaning what you all have to say…

  4. Iona Gerloff

    You seem to have a lot of confidense in the things you do. Nice post also! .

  5. feed

    You should be a part of a contest for one of the finest websites on the net. I’m going to highly recommend this web site!|

  6. the feed

    Wow, superb blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your web site is excellent, let alone the content!|

  7. feed

    May I just say what a comfort to discover an individual who genuinely knows what they are talking about on the internet. You definitely know how to bring a problem to light and make it important. A lot more people need to look at this and understand this side of your story. It’s surprising you are not more popular since you surely have the gift.|

  8. feed

    I love reading an article that will make people think. Also, many thanks for permitting me to comment!|

  9. vreyro linomit

    I’m often to running a blog and i actually respect your content. The article has actually peaks my interest. I’m going to bookmark your site and preserve checking for brand spanking new information.

Leave a Reply