Exploring Lightning Web Components (LWC) and Apex Triggers

Hasan Sajedi
sajedi
Published in
4 min readJul 14, 2023

Lightning Web Components (LWC) and Apex triggers are powerful tools in the Salesforce ecosystem that enable developers to create dynamic and interactive user interfaces while maintaining business logic and data integrity. In this article, we’ll delve into the basics of LWC and Apex triggers and demonstrate how they work together to enhance the functionality of Salesforce applications.

What are Lightning Web Components (LWC)?

Lightning Web Components (LWC) is a modern, lightweight framework provided by Salesforce that allows developers to build web applications using standard web technologies like HTML, CSS, and JavaScript. LWC provides a component-based architecture, enabling reusable and modular development.

LWC offers several advantages, including:

  1. Performance: LWC leverages a highly efficient rendering engine, resulting in fast loading and rendering of components.
  2. Developer-Friendly: LWC embraces standard web development practices, making it easy for developers with web development experience to get started.
  3. Reusability: LWC promotes reusability of components, allowing developers to build modular and maintainable codebases.
  4. Seamless Integration: LWC seamlessly integrates with the Salesforce Lightning Experience, empowering developers to enhance user interfaces and extend functionality.

What are Apex Triggers?

Apex triggers are pieces of code in Salesforce that execute before or after data manipulation operations, such as insert, update, or delete, on Salesforce objects. Apex triggers enable developers to implement custom business logic and automate processes based on specific data events.

Triggers operate on the database level, providing a way to enforce complex business rules, maintain data integrity, and perform additional operations beyond what is available through configuration options.

Example: Apex Trigger for the Account Object

Let’s take a look at an example of an Apex trigger for the Account object. This trigger sends an email notification to “info@test.com” whenever an Account record is inserted, updated, or deleted. The email includes information about the affected Accounts.

trigger AccountUpdateDeletionNotification on Account (after insert, after update, after delete) {

/**
* Sends an email notification for the updated or deleted accounts.
*
* @param affectedAccounts List of Account records affected by the operation.
* @param operation The operation type (insert, update, delete).
*/
public void sendEmailNotification(List<Account> affectedAccounts, String operation) {
List<Messaging.SingleEmailMessage> emailList = new List<Messaging.SingleEmailMessage>();

for (Account affectedAccount : affectedAccounts) {
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setToAddresses(new List<String>{ 'test@test.com' });
email.setSubject('Account ' + operation + ': ' + affectedAccount.Name);

String operationDescription;
if (operation == 'Insert') {
operationDescription = 'has been inserted.';
} else if (operation == 'Update') {
operationDescription = 'has been updated.';
} else if (operation == 'Delete') {
operationDescription = 'has been deleted.';
} else {
operationDescription = 'has been modified.';
}

email.setPlainTextBody('The Account "' + affectedAccount.Name + '" ' + operationDescription);
emailList.add(email);
}

Messaging.sendEmail(emailList);
}

// Trigger handlers

/**
* Handles the trigger execution for after insert event.
*/
public void handleAfterInsert(List<Account> insertedAccounts) {
if (!insertedAccounts.isEmpty()) {
sendEmailNotification(insertedAccounts, 'Insert');
}
}

/**
* Handles the trigger execution for after update event.
*/
public void handleAfterUpdate(List<Account> updatedAccounts) {
if (!updatedAccounts.isEmpty()) {
sendEmailNotification(updatedAccounts, 'Update');
}
}

/**
* Handles the trigger execution for after delete event.
*/
public void handleAfterDelete(List<Account> deletedAccounts) {
if (!deletedAccounts.isEmpty()) {
sendEmailNotification(deletedAccounts, 'Delete');
}
}

// Trigger execution
List<Account> insertedAccounts = new List<Account>();
List<Account> updatedAccounts = new List<Account>();
List<Account> deletedAccounts = new List<Account>();

if (Trigger.isAfter && Trigger.isInsert) {
insertedAccounts = Trigger.new;
handleAfterInsert(insertedAccounts);
}

if (Trigger.isAfter && Trigger.isUpdate) {
updatedAccounts = Trigger.new;
handleAfterUpdate(updatedAccounts);
}

if (Trigger.isAfter && Trigger.isDelete) {
deletedAccounts = Trigger.old;
handleAfterDelete(deletedAccounts);
}
}

In this example, we have an Apex trigger named AccountUpdateDeletionNotification that executes after insert, after update, and after delete events on the Account object. The trigger calls the respective handler methods (handleAfterInsert, handleAfterUpdate, handleAfterDelete) to send email notifications for the affected Accounts.

The sendEmailNotification method constructs the email message with the appropriate subject and body based on the operation type ('Insert', 'Update', or 'Delete'). It then sends the email using the Messaging.sendEmail method.

Please remember to update the recipient email address and customize the email subject and body to fit your specific requirements.

With this example, you can enhance your Salesforce application by sending email notifications whenever Account records are inserted, updated, or deleted, providing real-time updates on the affected data.

Remember to activate the trigger in your Salesforce organization for it to take effect.

Conclusion

Lightning Web Components (LWC) and Apex triggers are valuable tools for Salesforce developers. LWC enables the creation of dynamic user interfaces, while Apex triggers provide powerful data manipulation and business logic capabilities. By combining LWC and Apex triggers, developers can build robust and interactive applications that meet the specific needs of their organizations.

Resources

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers.htm

--

--