Trigger on custom object to prevent delete the related list record based on lookup relation whenever trying to delete parent record, throw an error message using apex class handler trigger in Salesforce | How to prevent delete if parent object has child in Salesforce

3,074 views


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.

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)

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!

 
Trigger to prevent delete the related list record based on lookup relation.

Final Output

trigger to prevent delete related record in salesforce -- w3web.net

 

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

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 

scoreCardTrigger.apxt [Apex Class Trigger]

  1.    TRIGGER scoreCardTrigger ON scoreCard__c (BEFORE INSERT, after INSERT, BEFORE DELETE, after DELETE) {
  2.     IF(TRIGGER.isBefore){
  3.       system.debug('trigger before trigger');     
  4.  
  5.          IF(TRIGGER.isDelete){
  6.              scoreCardTriggerHandler.delRelSore(TRIGGER.old, TRIGGER.oldMap);
  7.          }
  8.  
  9.  
  10.         }
  11.     ELSE IF(TRIGGER.isAfter){
  12.         IF(TRIGGER.isUpdate){
  13.             system.debug('trigger after trigger'); 
  14.         }
  15.     }
  16.  
  17.  
  18.  
  19.   }

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]

  1.    public class scoreCardTriggerHandler {
  2.  
  3.     public static void delRelSore(List<scoreCard__c> scoreListObj, Map<Id, scoreCard__c> scoreObjRel){       
  4.  
  5.          Set<Id> scoreSetId = scoreObjRel.keySet();
  6.              system.debug('scoreSetId ' + scoreSetId);
  7.              List<scoreCardChild__c> childObj= [SELECT Id, Name, scoreCardLookup__c FROM scoreCardChild__c WHERE scoreCardLookup__c IN:scoreSetId];
  8.  
  9.               Map<Id, scoreCardChild__c> childMapList = NEW Map<Id, scoreCardChild__c>(); 
  10.  
  11.              FOR(scoreCardChild__c ch1:childObj){
  12.                  IF(ch1.scoreCardLookup__c !=NULL){
  13.                    childMapList.put(ch1.scoreCardLookup__c, ch1);    
  14.                  }
  15.  
  16.                }
  17.              system.debug('childMapList# ' + childMapList);
  18.  
  19.              FOR(scoreCard__c s1:scoreListObj){
  20.                  IF(childMapList.containsKey(s1.Id)){
  21.                      s1.addError('We can not delete related record!!');
  22.                  }
  23.              }
  24.  
  25.     }
  26.  
  27.  
  28. }

trigger to prevent delete related record in salesforce -- w3web.net

Further post that would you like to learn in Salesforce

 

 

 

FAQ (Frequently Asked Questions)

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

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

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



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment