Skip to main content

CRUD Operation in Salesforce using Custom Object, Visualforce, and APEX Controller with MVC Design Pattern

CRUD Operation in Salesforce using Custom Object, Visualforce, and APEX Controller with MVC Design Pattern

This tutorial provide step by step implementation to perform CRUD Operation into Course Object.

In Salesforce development, building applications that follow standard architectural principles is essential for maintainability, scalability, and clarity. One of the most powerful combinations is implementing CRUD operations (Create, Read, Update, Delete) on a Custom Object using Visualforce Pages, Apex Controllers, and adhering to the MVC (Model-View-Controller) Design Pattern.




Step1st:

Create Course Object using Admin Dashboard it contain Name, Mode(PICKLIST), Fees, CourseId as an ExternalId.


Step2nd:

Create APEX Controller to perform CRUD Operation similar this


public class CourseController {

    public Course__c course1 { get; set; }

    public List<Course__c> course { get; set; }

    public Id selectedCourseId { get; set; }


    public CourseController() {

        course1 = new Course__c();

        loadCourses();

    }


    public void loadCourses() {

        course = [SELECT Id, CourseId__c, Coursemode__c, course_fee__c FROM Course__c];

    }


    public PageReference saveCourse() {

        try {

            if (course1.Id == null) {

                insert course1;

            } else {

                update course1;

            }

            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Course saved successfully.'));

            course1 = new Course__c(); // reset form

            loadCourses(); // refresh list

        } catch (Exception e) {

            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Error: ' + e.getMessage()));

        }

        return null;

    }


    public PageReference editCourse() {

        System.debug('Edit selected id is: ' + selectedCourseId);

        if (selectedCourseId != null) {

            course1 = [SELECT Id, CourseId__c, Coursemode__c, course_fee__c FROM Course__c WHERE Id = :selectedCourseId LIMIT 1];

        }

        return null;

    }


    public PageReference deleteCourse() {

        System.debug('Delete selected id is: ' + selectedCourseId);

        if (selectedCourseId != null) {

            try {

                delete [SELECT Id FROM Course__c WHERE Id = :selectedCourseId LIMIT 1];

                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Course deleted successfully.'));

                loadCourses(); // refresh list

            } catch (Exception e) {

                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Delete failed: ' + e.getMessage()));

            }

        }

        return null;

    }

}


Step3rd:

Create Visualforce Page and Write this code

<apex:page controller="CourseController">
    <apex:form>
        <apex:pageMessages />

        <apex:pageBlock title="Course Insert Form">
            <table style="width:100%">
                <tr>
                    <td>
                        <apex:outputLabel value="Course ID" /><br/>
                        <apex:inputField value="{!course1.CourseId__c}" />
                    </td>
                    <td>
                        <apex:outputLabel value="Course Mode" /><br/>
                        <apex:inputField value="{!course1.Coursemode__c}" />
                    </td>
                    <td>
                        <apex:outputLabel value="Course Fee" /><br/>
                        <apex:inputField value="{!course1.course_fee__c}" />
                    </td>
                    <td style="vertical-align: bottom;">
                        <apex:commandButton value="Save" action="{!saveCourse}" />
                    </td>
                </tr>
            </table>
        </apex:pageBlock>
 </apex:form>
 <apex:form>
        <apex:pageBlock title="Course Information">
            <apex:pageBlockTable value="{!course}" var="c">
                <apex:column value="{!c.Id}" />
                <apex:column value="{!c.CourseId__c}" />
                <apex:column value="{!c.Coursemode__c}" />
                <apex:column value="{!c.course_fee__c}" />
                
               <apex:column>
                    <apex:commandLink value="Delete" action="{!deleteCourse}">
                        <apex:param name="selectedId" value="{!c.Id}" assignTo="{!selectedCourseId}" />
                    </apex:commandLink> 
                    |
                    <apex:commandLink value="Edit" action="{!editCourse}">
                        <apex:param name="selectedId" value="{!c.Id}" assignTo="{!selectedCourseId}" />
                    </apex:commandLink>
                </apex:column>
                       
                    
              
            </apex:pageBlockTable>
        </apex:pageBlock>
   </apex:form >
</apex:page>




Comments

Popular posts from this blog

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...

JDBC using JSP and Servlet

JDBC means Java Database Connectivity ,It is intermediates from Application to database. JDBC has different type of divers and provides to communicate from database server. JDBC contain four different type of approach to communicate with Database Type 1:- JDBC-ODBC Driver Type2:- JDBC Vendor specific Type3 :- JDBC Network Specific Type4:- JDBC Client-Server based Driver  or JAVA thin driver:- Mostly we prefer Type 4 type of Driver to communicate with database server. Step for JDBC:- 1  Create Database using MYSQL ,ORACLE ,MS-SQL or any other database 2   Create Table using database server 3   Create Form according to database table 4  Submit Form and get form data into servlet 5  write JDBC Code:-     5.1)   import package    import java.sql.*     5.2)  Add JDBC Driver according to database ide tools     5.3)  call driver in program         ...