2

I am a newby of Apex code programming. Can someone please help me on

whether Apex code supports MVC architecture? If Yes, How we can write Models, Views and Controllers in Apex Code?

4

From what I think, MVC in SFDC development roughly means:

View: Visualforce page
Model: SObject
Controller: Apex class

Referece: http://www.salesforcetutorial.com/model-view-controller-mvc/

  • Yes, Thanks for this, But i am confused where we can write Models(sobjects). If Possible can you please explain me with some code of model? – anusha Jun 3 '14 at 3:57
  • 1
    SObjects are just like the first class citizens in Apex. You use them just like class and other language elements. As a reference, you can take a look into this: developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/… – Lance Shi Jun 3 '14 at 4:04
  • Thanks for this,My doubt is can we define this sObjects in any .cls file or can we have different files to define sObjects and do we need to extends any sobject super class to define sObjects.Sorry if my question is wrong – anusha Jun 3 '14 at 4:14
  • 2
    That means we can create any object and can assign fields using dot notation without any prior specification of field names in any Apex class file. Please correct me if i am wrong – anusha Jun 3 '14 at 4:26
  • 1
    That's right. You can use it in that way. – Lance Shi Jun 3 '14 at 4:28
3

Lance's answer is correct but it looks like things may still be unclear.

Model: sObjects. To view/create/edit these via the web interface:

  1. log into your org
  2. click on your name in the top right right corner of the screen
  3. Click 'Setup' from the drop down menu
  4. In the setup page, on the left hand bar click 'Create'
  5. Click 'Objects'
  6. This will show you a list of objects Click 'New' and give your new Custom sObject a name.
  7. You'll have an opporunity to add your fields of various types (Text/String, Numbers, Decimals, Booleans, Etc.);

Now, to access these via an Apex class you will call them by their API. By Convention the platform will add '__c' to the name of the object or field. If you enter an object 'Duck' with a text field 'Color,' your code to work with ducks could look like this:

public class DuckController{

    public void createAndInsertGreenDuck(){
        Duck__c thisDuck = new Duck__c();    //create a duck 
        thisDuck.Color__c='green';              //set the color
        insert thisDuck;                     //insert the duck into the database
    }

    public List<Duck__c> getAllDucksByColor(String desiredColor){
        //gets all the green ducks in the database (up to 10,000 per query)
        List<Duck__c> duckList = [Select Id, Color__c FROM Duck__c WHERE Color__c == :desiredColor];
        return duckList;
    }

    public void deleteAllGreenDucks(){
        //use the earlier method to get the green ducks from the Database.
        List<Duck__c> greenDucks = getAllDucksByColor('green');
        delete greenDucks; //delete the Ducks
    }

}

Hopefully that helps you understand how the Model(SObject) and the Controller(Apex CLass) interact with each other. Let me know if this is unclear.

  • 1
    Thank You Paul.Can you please clarify my doubt on this, we will have .object files in apex application right. can we treat them as models? – anusha Jun 5 '14 at 8:55
  • .object files represent the metadata of an object. Its just another interface and yes, because they are they object, they represent the model. – The Paul Allen Jun 5 '14 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.