0

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

4
  • 1
    Welcome to SFSE! Please take the tour and read How to Ask. It's good that you've included your code (really hard to find issues without that). It'd also be helpful to include the code for the trigger that calls this 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.
    – Derek F
    Commented Jul 29, 2019 at 15:16
  • @salesforce crm - on what object is this trigger firing ? what is the trigger event ? Commented Jul 29, 2019 at 15:20
  • You need to avoid performing queries inside a loop - you query the cases in a loop over the attachments. If called from a trigger, you may get 200 attachments into a single trigger invocation and exceed your permitted number of query operations.
    – Phil W
    Commented Jul 29, 2019 at 15:25
  • The main reason this isn't working is because you update the "cc.Attachments" which are queried separately. You are not updating the actual input attachments.
    – Phil W
    Commented Jul 29, 2019 at 15:27

1 Answer 1

1

There are several things wrong with your code. The first one is a big no-no. You have a SELECT statement inside of your for loop. This will potentially hit governor limits if your trigger fires with a sufficiently large number of records.

The second is that you never persist your changes to the database.

This is a potential correct code for you.

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;
    }
}

The code above assumes you're running in an after context. If you are in a before context, you wouldn't need to use listOfAttachments and you could update the attaches list directly (with no need for DML) to see the attachment names updated.

PS: As Phil pointed out, if this is an Attachment trigger, you may run into exceptions while trying to update the records already in the trigger. But without seeing your code and getting more info about your logic I am hesitant to update the code any further.

8
  • Note that there is an implication that the handler is called from a trigger, so the update at the end is plain wrong. What must be done is to update the attachments in the input "attaches" list instead (a DML is then not needed). Update your answer and I'll upvote it.
    – Phil W
    Commented Jul 29, 2019 at 15:28
  • It is most definitely not "plain wrong" if this is an "after" trigger, as stated in my answer. There could be good reasons for this and, since the OP has not provided neither context nor detail, I cannot assume that this code was meant to run in the "before" and therefore stuck as close as I can to the code that was provided. Commented Jul 29, 2019 at 15:31
  • You can't update the trigger objects themselves in the after phase. You will get an exception.
    – Phil W
    Commented Jul 29, 2019 at 15:31
  • Now you're assuming the trigger is on the attachment object. Which I can't do. Once proper detail is provided, I will be happy to adjust my code to what the OP's scenario looks like. Commented Jul 29, 2019 at 15:32
  • 1
    I've seen some things in my life, dude. :-) I'm sure you've seen the same. "Assume nothing" is my motto. Commented Jul 29, 2019 at 15:33

You must log in to answer this question.

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