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
CloudGeekCloudGeek 

Trigger on Attachment : Need input to verify on SOQL LIMIT / Bulkify for Delete Event

Hi,

Here is my scenario:

I have a CUSTOM Object, when any attachment is uploaded for this CUSTOM OBJECT record, I am populating 2 fields with the values of attachment record onto the CUSTOM OBJECT via a trigger.

If the attachment is deleted there are below cases that needs to be implemented:
Case-1: If all the ATTACHMENT records are deleted for this CUSTOM OBJECT ---> CLEAR those fields
Case-2: If any other ATTACHMENT exists after deleteing a single attachment​ -- > Update these fields with the MOST RECENT ATTACHMENT values.

Here is the code which I have tried;


Below is the code I have tried - Can you help me understand whether this is going to hit any SOQL LIMIT ?

public void callAfterDeleteMethods(List<Attachment> AList)
    {         
        //Local variables        
        set<Id> policyIDs = new set<Id>();
        List<Attachment> attachList = new List<Attachment>();
        Map<CUSTOM_OBJECT__c,List<Attachment>> Policy_TO_AttachmentList_Map = new Map<CUSTOM_OBJECT__c,List<Attachment>>();  //Map PUG-Object To Respective Attachment List
        List<CUSTOM_OBJECT__c> pugList;
        
        for(Attachment att : AList)          //Taking All Policy IDs in Context
        {
            String objectAPIName = (String) att.ParentId.getSObjectType().getDescribe().getName();
            System.debug('FROM Delete Method - objectAPIName = '+objectAPIName);
            if(objectAPIName.equalsIgnoreCase('CUSTOM_OBJECT__c'))
                    policyIDs.add(att.ParentId);
        }

        
        if(policyIDs.size()>0)    //if list of incoming list records to be deleted are not ZERO
        {        
              pugList = [SELECT Id,Upload_Date__c,User__c  FROM CUSTOM_OBJECT__c WHERE Id in: policyIDs];  //List to Hold PUG Objects
        }      
        
         if(pugList.size()>0)      
                {                 

                    for(CUSTOM_OBJECT__c pug : pugList)  //For each PUG Object get Attachments listed and Mapped
                      {
                            for(Attachment Att : [SELECT Id,CreatedDate,CreatedById FROM Attachment WHERE ParentId =: pug.Id ORDER BY createdDate DESC])
                                {
                                    attachList.add(Att);  //Make the list
                                }
                    
                            Policy_TO_AttachmentList_Map.put(pug,attachList); //Policy-Object --> Corresponding Attachment List
                    
                      }
                      
                      for(CUSTOM_OBJECT__c pug : pugList)
                      {
                          if(Policy_TO_AttachmentList_Map.containsKey(pug))
                          {
                              if(Policy_TO_AttachmentList_Map.get(pug).size()>0)  //if attachments exists for policy
                              {
                                    Attachment temp = Policy_TO_AttachmentList_Map.get(pug).get(0);          
                                    pug.Upload_Date__c = (Datetime)temp.CreatedDate;
                                    pug.User__c = temp.CreatedById;

                                    policyUpdatelist.add(pug);
                              }
                              else
                              {     
                                    pug.Upload_Date__c = null;
                                    pug.User__c = null;
               
                                    policyUpdatelist.add(pug);
                              }
                              
                          }
                      }
                     

                }

          update policyUpdatelist;

          firstRun = false;        //To Avoid Recursive Calls in the Context

        
    }//End-Of-Delete Method



/*******************************************************************************/

In the above code I had to write NESTED For Loops to fetch the Attachment list for every CUSTOM OBJECT RECORD.


for(CUSTOM_OBJECT__c pug : pugList)  //For each PUG Object get Attachments listed and Mapped
                      {
                            for(Attachment Att : [SELECT Id,CreatedDate,CreatedById FROM Attachment WHERE ParentId =: pug.Id ORDER BY createdDate DESC])
                                {
                                    attachList.add(Att);  //Make the list
                                }
                    
                            Policy_TO_AttachmentList_Map.put(pug,attachList); //Policy-Object --> Corresponding Attachment List
                    
                      }


I think this may cause to hit SOQL LIMIT, Can any one suggest any other way to  this ?
Best Answer chosen by CloudGeek
SaranSaran
Hi venkat,

Below is the modified class that helps you to complete the delete functionality.
 
public void callAfterDeleteMethods(List<Attachment> AList)
    {         
        set<Id> policyIDs = new set<Id>();
        set<Id> attId = new set<Id>();
        list<CUSTOM_OBJECT__c> pugList = new list<CUSTOM_OBJECT__c>();
        
        for(Attachment att : AList)         
        {
            String objectAPIName = (String) att.ParentId.getSObjectType().getDescribe().getName();
            System.debug('FROM Delete Method - objectAPIName = '+objectAPIName);
            if(objectAPIName.equalsIgnoreCase('CUSTOM_OBJECT__c'))
            {
            	policyIDs.add(att.ParentId);
            	attId.add(att.Id);
            }
        }

        for(CUSTOM_OBJECT__c obj : [SELECT Id,Upload_Date__c,User__c, (select id, createdDate, createdById from Attachments order by createdDate DESC) FROM CUSTOM_OBJECT__c WHERE Id in: policyIDs AND Id NOT IN: attId])
        {
        	if(!obj.Attachments.isEmpty)
			{
				Attachment temp = obj.Attachments.get(0);
	            
	            obj.Upload_Date__c = (DateTime)temp.CreatedDate;
	            obj.Upload_User__c = temp.CreatedById;

	            pugList.add(obj);
			}else
			{
				obj.Upload_Date__c = null;
	            obj.Upload_User__c = null;

	            pugList.add(obj);
			}
        }

        if(pugList.size() > 0)
        	update pugList;
    }


Hope this might help you.

Thanks.

 

All Answers

SaranSaran
Hi venkat,

Below is the modified class that helps you to complete the delete functionality.
 
public void callAfterDeleteMethods(List<Attachment> AList)
    {         
        set<Id> policyIDs = new set<Id>();
        set<Id> attId = new set<Id>();
        list<CUSTOM_OBJECT__c> pugList = new list<CUSTOM_OBJECT__c>();
        
        for(Attachment att : AList)         
        {
            String objectAPIName = (String) att.ParentId.getSObjectType().getDescribe().getName();
            System.debug('FROM Delete Method - objectAPIName = '+objectAPIName);
            if(objectAPIName.equalsIgnoreCase('CUSTOM_OBJECT__c'))
            {
            	policyIDs.add(att.ParentId);
            	attId.add(att.Id);
            }
        }

        for(CUSTOM_OBJECT__c obj : [SELECT Id,Upload_Date__c,User__c, (select id, createdDate, createdById from Attachments order by createdDate DESC) FROM CUSTOM_OBJECT__c WHERE Id in: policyIDs AND Id NOT IN: attId])
        {
        	if(!obj.Attachments.isEmpty)
			{
				Attachment temp = obj.Attachments.get(0);
	            
	            obj.Upload_Date__c = (DateTime)temp.CreatedDate;
	            obj.Upload_User__c = temp.CreatedById;

	            pugList.add(obj);
			}else
			{
				obj.Upload_Date__c = null;
	            obj.Upload_User__c = null;

	            pugList.add(obj);
			}
        }

        if(pugList.size() > 0)
        	update pugList;
    }


Hope this might help you.

Thanks.

 

This was selected as the best answer
CloudGeekCloudGeek
Hi,

That above code causing an error :

Error: Compile Error: Invalid foreign key relationship: CUSTOM_OBJECT__c.Attachments at line 87 column 17

Due to the line -        if(!obj.Attachments.isEmpty)

 
SaranSaran
Hi Venkat

Change this line if(!obj.Attachments.isEmpty) as if(!obj.Attachments.isEmpty())

Hope this might help you!

Thanks