Hey guys, today in this post we are going to learn about How to write a Trigger on custom object to prevent delete the related list record based on lookup relation whenever trying to delete parent record, display error message using apex class handler trigger in Salesforce
Real time scenarios:- Write a trigger to prevent delete the related list record. Whenever trying to delete parent record, trigger should be throw an error.
- Don’t forget to check out:- Trigger to check duplicate name to custom object in Salesforce Click Here For More Information
Files we used in this post example
scoreCardTrigger.apxt | Apex Class Trigger | It will be fired whenever trying to delete related list record. |
scoreCardTriggerHandler.apxc | Apex Class Controller | Apex handler trigger to prevent delete the related list record whenever trying to delete parent record. |
Custom Object:- scoreCard__c Custom Fields:- scoreCardLookup__c (Lookup Relation) |
Trigger to prevent delete the related list record based on lookup relation. |
Final Output
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
- Find the below steps for this post.
Create Apex Trigger
Step 1:- Apex Class Trigger : scoreCardTrigger.apxt
From Developer Console ➡ File ➡ New ➡ Apex Trigger
scoreCardTrigger.apxt [Apex Class Trigger]
TRIGGER scoreCardTrigger ON scoreCard__c (BEFORE INSERT, after INSERT, BEFORE DELETE, after DELETE) {
IF(TRIGGER.isBefore){
system.debug('trigger before trigger');
IF(TRIGGER.isDelete){
scoreCardTriggerHandler.delRelSore(TRIGGER.old, TRIGGER.oldMap);
}
}
ELSE IF(TRIGGER.isAfter){
IF(TRIGGER.isUpdate){
system.debug('trigger after trigger');
}
}
}
Create Apex Class Trigger Handler Controller
Step 2:- Create Apex Class : scoreCardTriggerHandler.apxc
From Developer Console ➡ File ➡ New ➡ Apex Class
scoreCardTriggerHandler.apxc [Apex Class Controller]
public class scoreCardTriggerHandler {
public static void delRelSore(List<scoreCard__c> scoreListObj, Map<Id, scoreCard__c> scoreObjRel){
Set<Id> scoreSetId = scoreObjRel.keySet();
system.debug('scoreSetId ' + scoreSetId);
List<scoreCardChild__c> childObj= [SELECT Id, Name, scoreCardLookup__c FROM scoreCardChild__c WHERE scoreCardLookup__c IN:scoreSetId];
Map<Id, scoreCardChild__c> childMapList = NEW Map<Id, scoreCardChild__c>();
FOR(scoreCardChild__c ch1:childObj){
IF(ch1.scoreCardLookup__c !=NULL){
childMapList.put(ch1.scoreCardLookup__c, ch1);
}
}
system.debug('childMapList# ' + childMapList);
FOR(scoreCard__c s1:scoreListObj){
IF(childMapList.containsKey(s1.Id)){
s1.addError('We can not delete related record!!');
}
}
}
}
Further post that would you like to learn in Salesforce
How do you prevent duplicate records?
You can use the DISTINCT or DISTINCTROW identifier to eliminate duplicate records. DISTINCT and DISTINCTROW are synonyms and specify removal of duplicate rows from the result set.
How do I manage duplicate leads in Salesforce?
To manage duplicates that aren't surfaced by a duplicate rule, create a duplicate record set.
How do I stop inserting duplicate records in MySQL?
Use the INSERT IGNORE command rather than the INSERT command. If a record doesn't duplicate an existing record, then MySQL inserts it as usual. If the record is a duplicate, then the IGNORE keyword tells MySQL to discard it silently without generating an error.
Related Topics | You May Also Like
Our Free Courses →
👉 Get Free Course →
📌 Salesforce Administrators 📌 Salesforce Lightning Flow Builder 📌 Salesforce Record Trigger Flow Builder |
👉 Get Free Course →
📌 Aura Lightning Framework 📌 Lightning Web Component (LWC) 📌 Rest APIs Integration |