Triggers
Triggers
if (!contactList.isEmpty()) {
update contactList;
}
}
trigger PreventAccountDeletion on Account (before delete) { trigger LogDeletedAccount on Account (after delete) {
for (Account acc : Trigger.old) { List<Account_Log__c> logList = new List<Account_Log__c>();
Integer contactCount = [SELECT COUNT() FROM Contact WHERE AccountId = :acc.Id];
if (contactCount > 0) { for (Account acc : Trigger.old) {
acc.addError('Cannot delete Account with associated Contacts.'); Account_Log__c log = new Account_Log__c(Name = acc.Name, Account_Id__c = acc.Id);
} logList.add(log);
} }
}
if (!logList.isEmpty()) {
insert logList;
}
}
trigger UpdateIndustry on Account (before update) { trigger UpdateTotalOrders on Order__c (after insert) {
for (Account acc : Trigger.new) { Map<Id, Account> accMap = new Map<Id, Account>();
if (acc.Industry == null) {
acc.Industry = 'Technology'; for (Order__c ord : Trigger.new) {
} if (ord.Account__c != null) {
} if (!accMap.containsKey(ord.Account__c)) {
} accMap.put(ord.Account__c, new Account(Id = ord.Account__c));
}
}
}
if (!accMap.isEmpty()) {
for (Account acc : [SELECT Id, Total_Orders__c FROM Account WHERE Id IN
:accMap.keySet()]) {
acc.Total_Orders__c = (acc.Total_Orders__c != null ? acc.Total_Orders__c : 0) + 1;
accMap.put(acc.Id, acc);
}
update accMap.values();
}
}
9. Trigger to Call a Helper Class (Best Practice) 10. Trigger with Future Method
Scenario: Use a handler class to manage logic for inserting Contacts when an Account is Scenario: Send an email when a Case is closed.
inserted.
trigger CaseClosedTrigger on Case (after update) {
trigger AccountTrigger on Account (after insert) { for (Case c : Trigger.new) {
AccountTriggerHandler.createContacts(Trigger.new); if (c.Status == 'Closed' && Trigger.oldMap.get(c.Id).Status != 'Closed') {
} EmailHandler.sendCaseClosureEmail(c.Id);
}
Handler Class: }
}
public class AccountTriggerHandler {
public static void createContacts(List<Account> accList) { Email Handler Class:
List<Contact> contactList = new List<Contact>();
public class EmailHandler {
for (Account acc : accList) { @future
Contact con = new Contact(LastName = acc.Name, AccountId = acc.Id); public static void sendCaseClosureEmail(Id caseId) {
contactList.add(con); Case c = [SELECT Id, Contact.Email FROM Case WHERE Id = :caseId];
} if (c.Contact.Email != null) {
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
if (!contactList.isEmpty()) { email.setToAddresses(new String[]{c.Contact.Email});
insert contactList; email.setSubject('Your Case has been Closed');
} email.setPlainTextBody('Dear Customer, your case has been closed. Thank you!');
} Messaging.sendEmail(new Messaging.SingleEmailMessage[]{email});
} }
}
}
******How to fire custom event*****
1. Child Component (childComponent.js) 2. Parent Component (parentComponent.js)
javascript javascript
export default class ChildComponent extends LightningElement { export default class ParentComponent extends LightningElement {
handleClick() { handleCustomEvent(event) {
// Creating a custom event with name 'customclick' alert(event.detail.message); // Display the message from the child
const event = new CustomEvent('customclick', { }
detail: { message: 'Hello from Child Component' } }
});
Parent Component HTML (parentComponent.html)
// Dispatching the event html
this.dispatchEvent(event); <template>
} <c-child-component oncustomclick={handleCustomEvent}></c-child-component>
} </template>
<template>
<lightning-button label="Click Me" onclick={handleClick}></lightning-button>
</template>