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:
- Go to Setup: Click on the gear icon in the top right corner and select "Setup".
- Object Manager: In the Quick Find box, type "Object Manager" and click on it.
- Create Custom Object: Click on the "Create" button and select "Custom Object".
- 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
- Save: Click on the "Save" button.
Adding Fields to the Custom Object
Next, add the necessary fields to the Student__c
object:
- Age: Data Type: Number
- Phone Number: Data Type: Phone
- Email: Data Type: Email
- Course: Data Type: Picklist (Values: Salesforce, AWS, Python, Java, Spoken English)
- 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.
Class Declaration:
public class StudentDataController { }
- This declares a public class named
StudentDataController
.
- This declares a public class named
Method Declaration:
public static void insertStudentRecords() { }
- This declares a public static method named
insertStudentRecords
. The method does not return any value (void
).
- This declares a public static method named
Creating a List:
List<Student__c> students = new List<Student__c>();
- This line creates a list named
students
to holdStudent__c
records.
- This line creates a list named
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 thestudents
list with the specified details for Ritesh.
- This line adds a new
- Similarly, additional lines add records for Sanjay, Sujay, Prashanth, and Ramu.
Inserting Records:
insert students;
- This line inserts all the records in the
students
list into theStudent__c
object in Salesforce.
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:
- YouTube Channel: SFDC Telugu
- Website: www.sfdctelugu.in
- Store: SFDC Telugu Store
- www.sfdctelugu.in
0 Comments