0

I have a custom object named News where i have custom text field called Image ids. I want to store Ids of all the images i would attach as attachment. I want to store the ids seperated by commas.

enter image description here

And i want to delete the Ids from text field if and attachment is deleted.

Here is my code;

    trigger PopulateIdValueinProduct on Attachment (after insert, after update, after delete, after undelete) {
    Map<Id,List<Attachment>> parent = new Map<Id,List<Attachment>>();
    set<id> attids = new set<id>();
    if(Trigger.new<>null){
       for(Attachment c:Trigger.new){
           FT_News__c l;
           if(c.ParentId != null)
               attids.add(c.parentid);
       }

   }else if(Trigger.old != null){
       for(Attachment c:Trigger.old){
           if(c.ParentId<>null)      
               attids.add(Trigger.oldMap.get(c.id).parentid);
       }
   }

    if(attids.size()>0){
       try{
           List<Attachment> a = new List<Attachment>();
           Map<id,FT_News__c> newsmap = new Map<id,FT_News__c>([select id,FCC_ImageIds__c from FT_News__c where id IN: attids]);
           a = [select id,parentid from Attachment where parentid IN:attids];

           for(Attachment at: a){
               List<Attachment> llist = new List<Attachment>();
               if(parent.get(at.parentid) == null){
                   llist = new List<Attachment>();
                   llist.add(at);
                   parent.put(at.parentid,llist);
               }else if(parent.get(at.parentid) != null){
                   llist = new List<Attachment>();
                   llist = parent.get(at.parentid);
                   llist.add(at);
                   parent.put(at.parentid,llist);
               }
           }

           for(Id i: attids){
               if(newsmap.get(i) != null && parent.get(i) != null){
                   for(Attachment eachNews: parent.get(i)){
                        newsmap.get(i).FCC_ImageIds__c += String.ValueOf(eachNews.Id) +','; 
                   }

               }else if(testmap.get(i) != null && parent.get(i) == null){
                  newsmap.get(i).FCC_ImageIds__c = ''; 
               }
           }

           update newsmap.values();
           System.Debug(testmap.values());
       }catch(Exception e){}
    }

}

thanks in advance!

2
  • What challenge are you facing.
    – Raul
    Nov 9, 2016 at 8:14
  • Be eaiser to get the parent ids in after trigger the query for attachments left and populate the string
    – Eric
    Nov 9, 2016 at 8:20

1 Answer 1

2

Please find the trigger and helper class

trigger PopulateIdValueinProduct on Attachment (after insert,after delete) {
    if(Trigger.isInsert())
        NewsAttachment.updateAttachmentid(Trigger.new);
    if(Trigger.isDelete())
        NewsAttachment.updateAttachmentid(Trigger.old);

}

public class NewsAttachment {

    public static updateAttachmentid(List<Attachment> attList) {
        List<Id> parentIdList = new List<Id>();
        for(Attachment att : attList) {
            parentIdList.add(att.Parentid);         
        }
        List<FT_News__c> newsList = [select id,FCC_ImageIds__c,(Select id from Attachments) From FT_News__c Where id in :parentidList];
        for(FT_News__c news : newsList) {
            String temp = '';
            for(Attachment att : news.Attachments) {
                temp = att.id + ',';
            }
            news.FCC_ImageIds__c = temp;    
        {
        update newsList;

    }

}
2
  • Thank You :) one more thing , after deletion of attachment, that speficic id should be removed from Image Ids text field
    – Ranjank
    Nov 9, 2016 at 8:48
  • That code works for inser and delete
    – S B
    Nov 9, 2016 at 8:54

You must log in to answer this question.

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