0

To follow best practices, I am writing Apex Class instead of Apex Trigger. I have a Object called Drawing__c and Drawing_Line_Items__c having Lookup relationship. I want to update count of Drawing_Line_Items__c on Drawing__c. As per my Code Count is updating, but when I delete Drawing_Line_Items__c record, Count did not updating. Below is the code of Updating Count.Kindly anyone help with how to write class for Delete method and update count.

Apex Class:

public class DrawingLineitemsSendCountonDrawing {
public static void AfterInsert(List<Drawing_Line_Items__c> newList){
        List<Drawing_Line_Items__c> DrawingList = new List<Drawing_Line_Items__c>();
        List<Drawing__c> DrawList = new List<Drawing__c>();
        Map<Id, List<Drawing_Line_Items__c>> DrawingLineItemsList = new Map<Id, List<Drawing_Line_Items__c>>();
        Set<Id> DrawingIds = new Set<Id>();
        //Map<String,String> DrawingLineItemsMap = New Map<String,String>();
        for(Drawing_Line_Items__c draw :newList){
            if(String.isNotBlank(draw.Drawing_Number__c) ){
                DrawingIds.add(draw.Drawing_Number__c);  
            } 
        } 
        if(DrawingIds.size() > 0){
            DrawingList = [SELECT Id, Shop_Drawing_Quantity__c, Drawing_Number__c FROM Drawing_Line_Items__c WHERE  Shop_Drawing_Quantity__c != 0 AND Shop_Drawing_Quantity__c != NULL  AND Drawing_Number__c IN : DrawingIds ];
            for(Drawing_Line_Items__c draw : DrawingList) {  
                if(!DrawingLineItemsList.containsKey(draw.Drawing_Number__c)){ 
                    DrawingLineItemsList.put(draw.Drawing_Number__c, new List<Drawing_Line_Items__c>());
                }
                DrawingLineItemsList.get(draw.Drawing_Number__c).add(draw);    
            } 
            System.debug('Drawing Id and Drawing Line Items List Map is ' + DrawingLineItemsList);
            DrawList = [SELECT Drawing_Line_Items_Count__c FROM Drawing__c WHERE Id IN : DrawingIds];
            for(Drawing__c Drawing : DrawList) {
                List<Drawing_Line_Items__c> DrawingList1 = new List<Drawing_Line_Items__c>();
                DrawingList1 = DrawingLineItemsList.get(Drawing.Id);
                Drawing.Drawing_Line_Items_Count__c= DrawingList1.size();    
            } 
            System.debug('Drawing List is ' + DrawList);
            update DrawList;    
        }
    }   
  }

Trigger Class:

trigger DrawingLineItems on Drawing_Line_Items__c (after insert, after Update, after delete, after undelete) {
// After Insert Count of Drawing Line Item goes to Drawing.
        // Calling Class from Trigger 
        if(Trigger.isAfter){
        DrawingLineitemsSendCountonDrawing.AfterInsert(Trigger.New);
    } 
}
8
  • 1
    You probably have already considered this, but is there a reason you're not using a master-detail relationship between your objects and a roll-up summary to get the COUNT?
    – nbrown
    Commented Feb 17, 2022 at 16:33
  • 1
    +1 to changing the relationship to use roll-up summaries. If that isn't possible, then there are several open source options for implementing rollups. github.com/jamessimone/apex-rollup and github.com/SFDO-Community/declarative-lookup-rollup-summaries Commented Feb 17, 2022 at 16:42
  • No we purposely using Lookup relationship. Is there any way we can delete drawing line items and count update on Drawing by using apex class?
    – apz
    Commented Feb 17, 2022 at 17:30
  • DLRS works on lookup relationships and handles deletes as well as insert/updates; it's free and widely used
    – cropredy
    Commented Feb 17, 2022 at 17:33
  • Can you provide code?
    – apz
    Commented Feb 17, 2022 at 17:33

1 Answer 1

0

So as my code was working fine, the issue was in passing the parameters. Here is the Whole Working Code.

public class DrawingLineitemsSendCountonDrawing {
    
public static void AfterInsert(List<Drawing_Line_Items__c> newList){
        List<Drawing_Line_Items__c> DrawingList = new List<Drawing_Line_Items__c>();
        List<Drawing__c> DrawList = new List<Drawing__c>();
        Map<Id, List<Drawing_Line_Items__c>> DrawingLineItemsList = new Map<Id, List<Drawing_Line_Items__c>>();
        Set<Id> DrawingIds = new Set<Id>();
        //Map<String,String> DrawingLineItemsMap = New Map<String,String>();
        for(Drawing_Line_Items__c draw :newList){
            if(String.isNotBlank(draw.Drawing_Number__c) ){
                DrawingIds.add(draw.Drawing_Number__c);  
            } 
        } 
        if(DrawingIds.size() > 0){
            DrawingList = [SELECT Id, Shop_Drawing_Quantity__c, Drawing_Number__c FROM Drawing_Line_Items__c WHERE  Shop_Drawing_Quantity__c != 0 AND Shop_Drawing_Quantity__c != NULL  AND Drawing_Number__c IN : DrawingIds ];
            for(Drawing_Line_Items__c draw : DrawingList) {  
                if(!DrawingLineItemsList.containsKey(draw.Drawing_Number__c)){ 
                    DrawingLineItemsList.put(draw.Drawing_Number__c, new List<Drawing_Line_Items__c>());
                }
                DrawingLineItemsList.get(draw.Drawing_Number__c).add(draw);    
            } 
            System.debug('Drawing Id and Drawing Line Items List Map is ' + DrawingLineItemsList);
            DrawList = [SELECT Drawing_Line_Items_Count__c FROM Drawing__c WHERE Id IN : DrawingIds];
            for(Drawing__c Drawing : DrawList) {
                List<Drawing_Line_Items__c> DrawingList1 = new List<Drawing_Line_Items__c>();
                DrawingList1 = DrawingLineItemsList.get(Drawing.Id);
                Drawing.Drawing_Line_Items_Count__c= DrawingList1.size(); 
                 System.debug('Drawing.Drawing_Line_Items_Count__c : ' + Drawing.Drawing_Line_Items_Count__c );
            } 
            System.debug('Drawing List is ' + DrawList);
           
            update DrawList;    
        }
    }
}

Trigger

//-----------------------------Using Below FrameWork for DrawingLineitemsSendCountonDrawing Class-------------------------//
            if(Trigger.isAfter){
                if(Trigger.isInsert){
            DrawingLineitemsSendCountonDrawing.AfterInsert(Trigger.New);
        } 
    }
        if(Trigger.isAfter){
                if(Trigger.isUndelete){
            DrawingLineitemsSendCountonDrawing.AfterInsert(Trigger.New);
        } 
    }
        if(Trigger.isAfter){
                if(Trigger.isUpdate){
            DrawingLineitemsSendCountonDrawing.AfterInsert(Trigger.New);
        } 
    }
        if(Trigger.isAfter){
                if(Trigger.isUpdate){
            DrawingLineitemsSendCountonDrawing.AfterInsert(Trigger.Old);
        } 
    }
        if(Trigger.isAfter){
                if(Trigger.isDelete){
            DrawingLineitemsSendCountonDrawing.AfterInsert(Trigger.Old);
        } 
    }
      

  

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .