function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
CodeHeartsSFDCCodeHeartsSFDC 

Error with Deleting Parent - After Delete Trigger

Hello,

Can anyone help me build a trigger for the below requirement please? I'm new to using delete triggers and triggers in general. Thank you!

Requirement: I have 2 objects "Order__c" and "Detail__c". Order__c is the parent and Detail__c (the children) has a lookup to order. There is a field on Detail__c called "Class__c" which is a lookup to another Item object. The Class__c field can hold two types of values only. One is "Regular" and other is "Linear".

Now when a Detail__c record is deleted, if any of the remaining detail records contain Class__c = Linear, then nothing should happen. If the Detail__c records related to Order__c contains only Class__c = Regular, then the Detail__c record (Class__c = Regular) and the correspionding Order__c record should be deleted

Below is the trigger I tried, but it is giving a null-pointer exception. I do not know if this trigger will perform the requirement stated above even if the error is corrected. Quick help will be highly appreciated. Many thanks!
 

trigger deleteOrder on Detail__c (after delete) {

    List<Order__c> orderDelete = new List<Order__c>();
    Set<Id> orderId = new Set<Id>();  
    Integer count = 0;
    
    for(Detail__c d: trigger.old) {
        orderId.add(d.Order__c);
    }
    
    Map<Id,Order__c> orderMap = new Map<Id,Order__c>([select Id from Order__c where Id In :orderId]);

    for(Detail__c d :trigger.old) {
        if(d.Class__r.Name.contains('Linear')){
        count++;
        Order__c o = orderMap.get(d.Order__c);
        orderDelete.add(o);
        }
    }
    
    if(count == 0) {
       delete orderDelete;
    }
}

 

Tad Aalgaard 3Tad Aalgaard 3
You need to add ALL ROWS to your SOQL statement so it can reach into the recycle bin.
 
Map<Id,Order__c> orderMap = new Map<Id,Order__c>([select Id from Order__c where Id In :orderId ALL ROWS]);

 
CodeHeartsSFDCCodeHeartsSFDC
@Tad Aalgaard 3

Hi Tad,

The solution you mentioned is not resolving my issue. I'm getting the below error:

Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger deleteOrder caused an unexpected exception, contact your administrator: deleteOrder: execution of AfterDelete caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.deleteOrder: line 14, column 1". 

 if(d.Class__r.Name.contains('Linear')){
Tad Aalgaard 3Tad Aalgaard 3
Ahhh... that error you just added is way more helpful.  Please add as much information as possible when asking questions of others.

Change this
 
if(d.Class__r.Name.contains('Linear')){

to this in order to check for the null before doing the contains as the contains will throw a null object error if the value it is working with is null

if(d.Class__c != null && d.Class__r.Name.contains('Linear')){

 
CodeHeartsSFDCCodeHeartsSFDC

@Tad Aalgaard 3

Apologies for not adding the error before. Thank you for the criteria, the error is gone now, but the requirement is not fulfilled, the parent Order__c record did not get deleted when I deleted all the related Detail__c records which had class__c = Linear

I still need to fulfill the below:
1. When I delete all the Detail__c records having class__c = Linear and there are no more Detail__c records left with Class__c = Linear, then the remaining Detail__c record with Class__c = Regular should get deleted and the parent Order__c record should also get deleted.

Tad Aalgaard 3Tad Aalgaard 3
Why do you have 
 
if(count == 0) {

Don't you want
 
if(count > 0) {

Or better yet, remove the count and check to see if not empty
 
if(!orderDelete.isEmpty()) {
CodeHeartsSFDCCodeHeartsSFDC
@Tad Aalgaard 3

I don;t think it will not resolve it. I'm having count because only if the count is 0 i need to delete the records.
  1. for(Detail__c d :trigger.old) { .                                                
  2.         if(d.Class__r.Name.contains('Linear')){              //if Detail -> Class contains "Linear" i'm incrementing the count
  3.         count++;
  4.         Order__c o = orderMap.get(d.Order__c);
  5.         orderDelete.add(o);
  6.         }
  7.     }
  8.     
  9.     if(count == 0) {                                                       //so for my use case only if the count is 0, I need to delete the records
  10.        delete orderDelete;
  11.     }
I'm not incrementing the count if the Detail records contain class = Regular

Can you tell me how your solution is going to fulfill the requirement?