Salesforce

Salesforce Trigger – Insert Scenarios in Real Time

In this guide, we will discuss the real time “Insert” scenarios to use the Salesforce triggers. You can go through this tutorial that discusses about Salesforce trigger and creates and implements triggers with examples. We will consider the Student Details App in this entire guide and apply all the insert scenarios on this application (three objects).

Configuration Setup

Let’s create three objects (college, subject, and hostel) in Salesforce with some fields before discussing the example scenarios.

1. College

Crate an object named “College” in Salesforce (Go to Setup | Object Manager | Create | Custom Object).

Fields and Relationships: 

1. Address

2. Course

The courses in this picklist are B.Tech, M.Tech, MBA, and MSc.

3. Speciality

This “Picklist” field is dependent on “Course”. The picklist values are: Finance, Computer Sciences, Data Science, Electronics, Electrical, Civil, Mechanical, Chemical Engineering, Food Processing, and Information Technology.

View the field dependencies:

4. Stay in Hostel?

5. Student ID

6. Student Name

2. Subject

Fields and Relationships:

We can see here the following category (Picklist), college (Master-Detail relationship with College), and subject name (Text).

The category holds the Java, Python, PHP, Microwave engineering, Structural engineering, Robotics, Finance, and Machine Learning picklist values.

3. Hostel

Fields and Relationships:

We can see here the college (Master-Detail relationship with College), student name (Text), student course (Text), and hostel fee (Text).

Let’s create an app named “Student Details” and add these three objects in it.

Steps:

  1. Go to “Quick Find” (from Setup) and search for “App Manager”.
  2. Choose “New Lightning App”.
  3. Enter the app name as “Student Details”.
  4. Select the “Standard Navigation” and choose College, Subject, and Hostel from the “Available Items” section and place them in the “Selected Items” section.
  5. Click “Save & Finish” by selecting the “System Administrator” under the “Profiles” section.

Go to the App Launcher and search for “Student Details”. Now, the app holds three objects.

Example 1:

Write a trigger on the “College” object that inserts the speciality based on the course if the speciality is not created while creating a record. So, we create the “College_Trigger” trigger on the “College__c” object and create the “Course_Handler” Apex class with the “updateSpeciality” method which is used to handle the trigger. In this scenario, the trigger event is “Before Insert”.

  1. The speciality is “Data Science” if the course is “B.Tech”.
  2. The speciality is “Mechanical” if the course is “M.Tech”.
  3. The speciality is “Food Processing” if the course is “MSc”.
  4. The speciality is “Finance” if the course is “MBA”.

College_Trigger.apxt

trigger College_Trigger on College__c (before insert) {
    if(Trigger.isInsert){
        if(Trigger.isBefore){
        // Trigger handler class that will set the Speciality field
        // based on the Course field, if the Speciality field is blank.
        Course_Handler.updateSpeciality(Trigger.New);
        }
    }
}

Course_Handler.apxc

public class Course_Handler {
public static void updateSpeciality(List collegeList){
for(College__c college:collegeList){
if(college.Speciality__c == null && (college.Course__c == 'B.Tech')){
college.Speciality__c='Data Science';
}
 else if(college.Speciality__c == null && (college.Course__c == 'M.Tech')){
college.Speciality__c='Mechanical';
}
    else if(college.Speciality__c == null && (college.Course__c == 'MSc')){
college.Speciality__c='Food Processing';
}
    else if(college.Speciality__c == null && (college.Course__c == 'MBA')){
college.Speciality__c='Finance';
}
}
}

}

Testing:

Case 1: Let’s create a record by opting to course as “B.Tech”.

The record is saved with the speciality as “Data Science”.

Case 2: Let’s create a record by opting to course as “MSc”.

The record is saved with the speciality as “Food Processing”.

Example 2:

Write a trigger on the “College” object that creates a Hostel record if “Stay in Hostel?” is checked in the “College” object. So, we use the existing “College_Trigger” trigger on the “College__c” object and create the “Hostel_handler” Apex class with the “createHostel” method which is used to handle the trigger. In this scenario, the trigger event is “After Insert”.

  1. The student name is the College student’s name.
  2. The student course is [College – Course] – [College – Speciality].
  3. The hostel name is “Vignan Vihar Boys-Hostel”.
  4. The hostel fee is 50000.

College_Trigger.apxt

trigger College_Trigger on College__c (after insert) {
    if(Trigger.isInsert){
        if(Trigger.isAfter){
        // Trigger handler class that will create Hostel record
        // with Student Name from College,
        // Student Course as Course - Speciality,
        // College as College Name,
        // Hostel Fee as 50000 and
        // Hostel Name as Vignan Vihar Boys-Hostel'.
        Hostel_Handler.createHostel(Trigger.New);
        }
    }
}

Hostel_handler.apxc

public class Hostel_handler {
 public static void createHostel(List accList){
      List hosList = new List();
      for(College__c college: accList){
         
        if(college.Stay_in_Hostel__c == true){
        // Create Hostel object
        Hostel__c hos= new Hostel__c();
        hos.Name = 'Vignan Vihar Boys-Hostel';
        hos.Student_Name__c = college.Student_Name__c;
        hos.Student_Course__c = college.Course__c + ' - ' + college.Speciality__c;
        hos.College__c = college.Id;
        hos.Hostel_fee__c = '50000';
        hosList.add(hos);
          }
      }
     // insert DML
      if(!hosList.isEmpty()){
          insert hosList;
}
}
}

Testing:

Let’s create a record in the “College” object by selecting the “Stay in Hostel?” checkbox.

A record is created in the “Hostel” object with the specified details.

Example 3:

Write a trigger on the “Subject” object that inserts the category based on the subject name if the category is not created while creating a record. So, we create the “Subject_Trigger” trigger on the “Subject__c” object and create the “Category_Handler” Apex class with the “updateCategory” method which is used to handle the trigger. In this scenario, the trigger event is “Before Insert”.

  1. The category is “Java” if the subject name is “Programming”.
  2. The category is “Machine Learning” if the subject name is “Data Analytics”.

Subject_Trigger.apxt

trigger Subject_Trigger on Subject__c (before insert) {
if(Trigger.isInsert){
        if(Trigger.isBefore){
        // Trigger handler class that will set the Category field
        // based on the Subject Name field, if the Category field is blank.
        Category_Handler.updateCategory(Trigger.New);
        }
    }

}

Category_Handler.apxc

public class Category_Handler {
public static void updateCategory(List subjectList){
    for(Subject__c subj:subjectList){
        if(subj.Category__c == null && (subj.Name == 'Programming')){
            subj.Category__c='java';
        }
        else if(subj.Category__c == null && (subj.Name == 'Data Analytics')){
        subj.Category__c='Machine Learning';
        }
    }
    }
}

Testing:

1. Create a subject with subject name as “Programming” without selecting the category. After saving the record, the category is “Java”.

2. Create a subject with subject name as “Data Analytics” without selecting the category. After saving the record, the category is “Machine Learning”.

Example 4:

Create the “Latest Category” field on the “College” object with text type.

Modify the code components as discussed in Example 3. Write a trigger on the “Subject” object that assigns its latest category to the “Latest category” field on “College”. The method name is “populateCategory” which is used to handle the trigger. In this scenario, the trigger event is “After Insert”.

Subject_Trigger.apxt

trigger Subject_Trigger on Subject__c (after insert) {
if(Trigger.isInsert){
        if(Trigger.isAfter){
        // Trigger handler class that will set the Latest Category from the Subject
        // in the College - Latest_Category__c field.
        Category_Handler.populateCategory(Trigger.New);
        }
    }

}

Course_Handler.apxc

public class Category_Handler {
public static void populateCategory(List subjectList){
    List accLIst = new List();
    for(Subject__c cs : subjectList){
      if(cs.College__c != null){
       College__c acc = new College__c();
       acc.Id = cs.College__c;
       acc.Latest_Category__c = cs.Category__c;
       accList.add(acc);
 }
 }
      if(!accList.isEmpty()){
        update accList;
        }
    }
}

Testing:

First, create a college with name as “College A”. Now, go to the “Subjects” tab and create one subject under this college with the category as “Python”. You will see that “College A” holds “Python” as its latest category.

Let’s create another subject with the same college with the “Java” category. Now, the latest category is “Java”.

Conclusion

We learned how to use the trigger in different inset scenarios with examples and test cases. Each scenario is uniquely considered and tested by inserting the records on the UI. It is good to separate the trigger criteria (in trigger) and the trigger logic (in Apex class). For better understanding, we considered the “Student Details” database that includes three objects: Hostel, Subject, and College. Both objects (Subject and Hostel) are having a master-detail relationship with the “College” object.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain