You need to sign in to do that
Don't have an account?
CloudGeek
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 ?
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 ?
Below is the modified class that helps you to complete the delete functionality.
Hope this might help you.
Thanks.
All Answers
Below is the modified class that helps you to complete the delete functionality.
Hope this might help you.
Thanks.
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)
Change this line if(!obj.Attachments.isEmpty) as if(!obj.Attachments.isEmpty())
Hope this might help you!
Thanks