i am trying to append the case id to attachment using trigger i have written correct logic but not getting any error and not appending case id to the attachment
here is my code
Trigger:
trigger AttachmentTrigger on Attachment (after insert,after update) {
AttachmentTriggerHandler handler = new AttachmentTriggerHandler();
if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)){
handler.appendingCaseNumberToAttachment(trigger.new);
}
}
Handler :
/*public class AttachmentTriggerHandler {
public void appendingCaseNumberToAttachment(list<Attachment> attaches){
list<Attachment> listOfAttachments = new list<Attachment>();
set<Id> parentIds = new set<Id>();
for(Attachment newAttachments:attaches){
system.debug('attaches====>'+newAttachments);
if(((String)newAttachments.ParentID).startswith('500')){
parentIds.add(newAttachments.ParentID);
System.debug('parentIds===='+parentIds);
}
}
Map<id,Case> casemap=new map<Id,Case>([select id,CaseNumber,(SELECT id,Name from Attachments) from case where id IN:parentIds]);
for(Case cc:casemap.values()){
for(Attachment attach:cc.Attachments){
System.debug('Attachments========'+attach);
attach.name=cc.CaseNumber+'-'+attach.Name;
listOfAttachments.add(attach);
//Attachments.Name = Attachments.Name+'-'+Attachments.(case.id).ParentId;
}
}
}
}*/
public class AttachmentTriggerHandler {
public void appendingCaseNumberToAttachment(list<Attachment> attaches){
list<Attachment> listOfAttachments = new list<Attachment>();
set<Id> parentIds = new set<Id>();
for(Attachment newAttachments:attaches){
system.debug('attaches====>'+newAttachments);
if(((String)newAttachments.ParentID).startswith('500')){
parentIds.add(newAttachments.ParentID);
System.debug('parentIds===='+parentIds);
}
}
Map<id,Case> casemap=new map<Id,Case>([select id,CaseNumber,(SELECT id,Name from Attachments) from case where id IN:parentIds]);
for(Case cc:casemap.values()){
for(Attachment attach:cc.Attachments){
System.debug('Attachments========'+attach);
attach.name=cc.CaseNumber+'-'+attach.Name;
listOfAttachments.add(attach);
//Attachments.Name = Attachments.Name+'-'+Attachments.(case.id).ParentId;
}
}
update listOfAttachments;
}
}
Please suggest me where i am doing mistake
AttachmentTriggerHandler
class. It could be that you simply need to add a DML statement, but it's hard to say if you need that (or if that'd even fix your problem) without seeing the trigger too.