Managing Student Data with Salesforce Apex

 

Managing Student Data with Salesforce Apex

In this blog post, we will learn how to manage student data using Salesforce Apex. We'll create a custom object for students, insert student records, and understand each line of code step by step. This example includes students with different courses, ages, phone numbers, and email addresses. Let's dive in!

Creating a Custom Object

First, we need to create a custom object called Student__c to store student data. Follow these steps to create the custom object in Salesforce:

  1. Go to Setup: Click on the gear icon in the top right corner and select "Setup".
  2. Object Manager: In the Quick Find box, type "Object Manager" and click on it.
  3. Create Custom Object: Click on the "Create" button and select "Custom Object".
  4. Object Details:
    • Label: Student
    • Plural Label: Students
    • Object Name: Student
    • Record Name: Student Number (Auto Number)
    • Data Type: Auto Number
    • Display Format: STTI-{0000}
    • Starting Number: 1
  5. Save: Click on the "Save" button.

Adding Fields to the Custom Object

Next, add the necessary fields to the Student__c object:

  1. Age: Data Type: Number
  2. Phone Number: Data Type: Phone
  3. Email: Data Type: Email
  4. Course: Data Type: Picklist (Values: Salesforce, AWS, Python, Java, Spoken English)
  5. Batch Timing: Data Type: Picklist (Values: 8 AM, 10 AM, 12 PM)

Apex Code to Insert Student Records

Now, let's write the Apex code to insert student records into the custom object. This code will demonstrate how to create and insert records programmatically in Salesforce.

public class StudentDataController {

    public static void insertStudentRecords() {

        // Creating a list to hold student records

        List<Student__c> students = new List<Student__c>();

        

        // Adding student records to the list

        students.add(new Student__c(

            Name = 'Ritesh',

            Age__c = 28,

            Phone__c = '1234567890',

            Email__c = 'ritesh@gmail.com',

            Course__c = 'Salesforce',

            Batch_Timing__c = '8 AM'

        ));

        

        students.add(new Student__c(

            Name = 'Sanjay',

            Age__c = 30,

            Phone__c = '0987654321',

            Email__c = 'sanjay@gmail.com',

            Course__c = 'AWS',

            Batch_Timing__c = '10 AM'

        ));

        

        students.add(new Student__c(

            Name = 'Sujay',

            Age__c = 26,

            Phone__c = '1122334455',

            Email__c = 'sujay@gmail.com',

            Course__c = 'Python',

            Batch_Timing__c = '12 PM'

        ));

        

        students.add(new Student__c(

            Name = 'Prashanth',

            Age__c = 19,

            Phone__c = '2233445566',

            Email__c = 'prashanth@gmail.com',

            Course__c = 'Java',

            Batch_Timing__c = '8 AM'

        ));

        

        students.add(new Student__c(

            Name = 'Ramu',

            Age__c = 21,

            Phone__c = '3344556677',

            Email__c = 'ramu@gmail.com',

            Course__c = 'Spoken English',

            Batch_Timing__c = '10 AM'

        ));

        

        // Inserting the records into the Student__c object

        insert students;

    }

}


Explanation of the Apex Code

Let's break down the code step by step to understand its purpose and functionality.

  1. Class Declaration: public class StudentDataController { }

    • This declares a public class named StudentDataController.
  2. Method Declaration: public static void insertStudentRecords() { }

    • This declares a public static method named insertStudentRecords. The method does not return any value (void).
  3. Creating a List: List<Student__c> students = new List<Student__c>();

    • This line creates a list named students to hold Student__c records.
  4. Adding Student Records:

    • students.add(new Student__c(Name = 'Ritesh', Age__c = 28, Phone__c = '1234567890', Email__c = 'ritesh@gmail.com', Course__c = 'Salesforce', Batch_Timing__c = '8 AM'));
      • This line adds a new Student__c record to the students list with the specified details for Ritesh.
    • Similarly, additional lines add records for Sanjay, Sujay, Prashanth, and Ramu.
  5. Inserting Records: insert students;

    • This line inserts all the records in the students list into the Student__c object in Salesforce.
Stay tuned for more tutorials and join our community SFDC Telugu!

Above Apex Program PDF Free Downloads For Channel Members Join Channel Membership Join Channel Membership 

For more detailed explanations, practical examples, and resources, refer your friends to SFDC Telugu:










  • www.sfdctelugu.in

Post a Comment

0 Comments