1

Was anyone successful in getting their after insert trigger on Attachment working? I don't see any debug logs for the trigger when I try to invoke from the UI

Updating post to add my code:

trigger AttachmentTrigger on Attachment (after insert) {
   Map<id,List<Attachment>> taskAttachmentMap = new Map<id,List<Attachment>>();
    for(Attachment attachment: [SELECT Body,BodyLength,ContentType,Description,Id,IsPrivate,Name,ParentId FROM Attachment where Id IN :Trigger.new]){           
        String text= String.valueof(attachment.ParentId);
        if(text.startswith('00T')){
            List<Attachment> la = new List<Attachment>();
            if(taskAttachmentMap.get(attachment.parentId)==null){
                la.add(attachment);
            }else{
                la = taskAttachmentMap.get(attachment.parentId);
                la.add(attachment);
            }
            taskAttachmentMap.put(attachment.parentId, la);                     
        }

    }

    List<Attachment> attachmentsToInsertonOpp = new List<Attachment>();
    //Find all tasks that are associated to opportunities
    if(taskAttachmentMap!=null){
        for(Task t: [select id, whatid from Task where id IN :taskAttachmentMap.keyset()]){
                String text1 = String.valueof(t.whatid);
                if(text1.startswith('006')){
                    for(Attachment att:taskAttachmentMap.get(t.id) ){
                        att.parentid=t.whatid;
                        attachmentsToInsertonOpp.add(att);                        
                    }
                }
        }           

    }

    //Now insert attachments
    if(attachmentsToInsertonOpp.size()!=0){
        insert attachmentsToInsertonOpp;
    }


}
  • What are you trying to do in the after insert event ? What actually do you see in the debug logs ? – Jarvis Feb 8 '16 at 21:17
  • 1
    I have an After Insert trigger working on Attachment in my company's orgs. Please edit your question to include more details. – Derek F Feb 8 '16 at 21:19
  • I am trying to copy an attachment attached to a task on Opportunity to Note and attachments section of opportunity itself. Here is the code: – shilpa Feb 8 '16 at 21:29
  • @user28253 welcome to the community. Make sure that when you post questions that you provide information on what you have tried and your current code. This will prevent your question from being voted down and help you get an answer quicker. Welcome! – Blair Kristin Feb 8 '16 at 21:31
  • @BlairKristin Thanks! Code i am trying to add is too long for the post – shilpa Feb 8 '16 at 21:33
-1

Salesforce provides an example of an after insert trigger in the following link http://help.salesforce.com/HTViewSolution?id=000181538 . Here is the answer they provide.

A trigger can be written on Attachment using Force.com IDE or ANT.

A sample code would be:

trigger SetTitleToAttachment on Attachment (after insert) {

String Title;
Id pId;

for(Attachment att: Trigger.new){
Title=att.Name;
pId=att.ParentId;
}

List<Case> c=[select Id , Title__c from Case where Id=:pId];

//assuming one record is fetched.
c[0].Title__c=Title;

update c[0];

}

Note: There is no way to write a trigger using the Salesforce.com UI (Portal).

| improve this answer | |
  • Thanks everyone! Please find the code in the post. This code doesnt seem to get invoked at all. Any ideas? – shilpa Feb 8 '16 at 21:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.