function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Haneef SHaneef S 

Post an attachment on a chatter group automatically using Trigger

Hello All,

I have a requirement to write a trigger and also I am new to salesforce.
Can anyone pls help me 
Requirement is:
I have custom object say test__c which is also having notes and attachments related list on it.
when a field called "state__c" is updated to "complete" on test__c object then an attachment(in notes & attchments section) starting with "abc-xxxx" needs to be posted automatically in a group called "my chatter".

My code is:
trigger AttachmentTriggger on Attachment (after insert) {
   
    Map<Id,Id> testAttachmentMap = new Map<Id,Id>(); 
    set<Id> testIds = new set<Id>();
    for(Attachment att : Trigger.New)
    {                                                                     
        if(att.ParentId.getSobjectType() == test__c.SobjectType){
              accountAttachmentMap.put(att.ParentId,att.Id);
         }
    }
   //fetching the account to check the condition
    List<test__c> testList = new List<test__c>();
    testList =[select id,state from test__c where Id IN : testAttachmentMap.keySet()];
   
    //checking if account is meting the required condition
    for(test__c testatt : testList){
        if(testatt.state='completed'){
            testIds.add(testatt.Id);
        }
//add code here to post attachments in group

    }
   
    
Any help much appreciated.
Thanks
Best Answer chosen by Haneef S
PawanKumarPawanKumar
Hi Haneef,

In your case you need to write code on test__c object instead of Attachment. 

Please try below code and take care any syntax, object error if any as i do not know exact object and field name of your instance.

-----------------------------------------
trigger AttachFileToTestObjectFeed on test__c (after update) {    

    List<String> testIdList = new List<String>();
    
    for (test__c newTest : Trigger.New)    {
        // only completed one 
        if(newTest!=null && newTest.state__c='complete'){
            testIdList.add(newTest.Id);
        }
    }
    
    // query all attachment for test__c.status='complete'
    List<Attachment> attachmentList = [Select Id,ParentId,Body,Name from Attachment where ParentId=:testIdList];
    
    
    list<FeedItem> listOfFeedFiles = new List<FeedItem>();
    if(Trigger.isAfter){
        for(Attachment attachment : attachmentList){
            //Adding a Content post
            FeedItem post = new FeedItem();
            post.ParentId = attachment.ParentId; //eg. test__c id
            post.Body = 'Attachment added';
            post.Type = 'ContentPost';
            post.ContentData = attachment.body; 
            post.ContentFileName = attachment.Name;
            post.Title = attachment.Name;
            listOfFeedFiles.add(post);               
          }
    }
    
    if(listOfFeedFiles!=null){
        insert listOfFeedFiles;    
    }    
    
}
------------------

Please let me know if it works for you. 

Regards,
Pawan Kumar

All Answers

PawanKumarPawanKumar
Hi Haneef,

In your case you need to write code on test__c object instead of Attachment. 

Please try below code and take care any syntax, object error if any as i do not know exact object and field name of your instance.

-----------------------------------------
trigger AttachFileToTestObjectFeed on test__c (after update) {    

    List<String> testIdList = new List<String>();
    
    for (test__c newTest : Trigger.New)    {
        // only completed one 
        if(newTest!=null && newTest.state__c='complete'){
            testIdList.add(newTest.Id);
        }
    }
    
    // query all attachment for test__c.status='complete'
    List<Attachment> attachmentList = [Select Id,ParentId,Body,Name from Attachment where ParentId=:testIdList];
    
    
    list<FeedItem> listOfFeedFiles = new List<FeedItem>();
    if(Trigger.isAfter){
        for(Attachment attachment : attachmentList){
            //Adding a Content post
            FeedItem post = new FeedItem();
            post.ParentId = attachment.ParentId; //eg. test__c id
            post.Body = 'Attachment added';
            post.Type = 'ContentPost';
            post.ContentData = attachment.body; 
            post.ContentFileName = attachment.Name;
            post.Title = attachment.Name;
            listOfFeedFiles.add(post);               
          }
    }
    
    if(listOfFeedFiles!=null){
        insert listOfFeedFiles;    
    }    
    
}
------------------

Please let me know if it works for you. 

Regards,
Pawan Kumar
This was selected as the best answer
Haneef SHaneef S
Thank you so much for your response,
Please see My code:
trigger accounttrigger on account (after update) {    

    List<String> accountlist = new List<String>();
    
    for (account newTest : Trigger.New)    {
        // only completed one 
         if(newTest!=null && newTest.state__c='completed'){
        {
            accountlist.add(newTest.Id);
        }
    }
    
    // query all attachment for account.status=completed
    List<Attachment> attachmentList = [Select Id,ParentId,Body,Name from Attachment where ParentId=:accountlist];
    
    
    list<FeedItem> listOfFeedFiles = new List<FeedItem>();
    if(Trigger.isAfter){
        for(Attachment attachment : attachmentList){
            //Adding a Content post
            FeedItem post = new FeedItem();
            post.ParentId = attachment.ParentId; //eg. account id
            post.Body = 'Attachment added';
            post.Type = 'ContentPost';
            post.ContentData = attachment.body; 
            post.ContentFileName = attachment.Name;
            post.Title = attachment.Name;
            listOfFeedFiles.add(post);               
          }

    }
    
    if(listOfFeedFiles!=null){
        insert listOfFeedFiles;    
    }    
    
}
}


Please see the error below:

User-added image

I am facing this error .
Thanks in advance
 
PawanKumarPawanKumar
use '==' instead of '='
if(newTest!=null && newTest.state__c=='completed'){

Second and third is problem. To make work reduce your class api to 34.0.

User-added image
PawanKumarPawanKumar
Please let me know if it works for you. thanks.
Haneef SHaneef S
Hi Pawan,

Thank you so much.. this is working for me.. Marking this as best answer.
Thanks,
Haneef