2

I want to write a trigger to insert a feed in chatter which could be a link to the library when I create a content in a library.{ Link to the content in the library preferably .But even a link to the library will do}

trigger ContentVersionTrigger on ContentVersion (after insert) 
{ 
   ContentVersion[] cv=Trigger.new; 
   InsertToChatter.InsertContent(cv); 
} 


public class InsertToChatter
{ 
   public static void Insertcontent (ContentVersion[] cv) 
   { 
     FeedItem post = new FeedItem(); post.Body = 'New file has been created.Pls check link'; 
     post.LinkUrl= URL('cs18.salesforce.com/sfc/#version?selectedDocumentId='+ cv.ContentDocumentId' ;
     insert post; 
    } 
} 

2 Answers 2

0

You can do this by creating a trigger on ContentDocument SObject on after insert event to get the Id of the document and create a FeedItem record with a link to your ContentDocument (/sfc/#version?selectedDocumentId=yourDocumentId)

Give it a try and post your code here if you need help with it

6
  • I have written something but I am completely new to this and hence there might be a lot of errors. Pls help: trigger ContentVersionTrigger on ContentVersion (after insert) { ContentVersion[] cv=Trigger.new; InsertToChatter.InsertContent(cv) } public class InsertToChatter{ public static void Insertcontent (ContentVersion[] cv) { FeedItem post = new FeedItem(); post.Body = 'New file has been created.Pls check link'; post.LinkUrl= URL('cs18.salesforce.com/sfc/#version?selectedDocumentId='+ cv.ContentDocumentId' insert post; } }
    – sahitya
    Commented Apr 25, 2014 at 11:54
  • I am sorry but I cannot answer the question myself within 8 hours of poating and hence i had to write a comment . The spacing has gone for a toss and code appears cluttered. Sorry
    – sahitya
    Commented Apr 25, 2014 at 11:55
  • I updated you question with your code, I don't think this will work as you expected. first the trigger must be on ContentDocument and not ContentVersion? what are the errors you're getting? Commented Apr 25, 2014 at 12:06
  • I thot since a version is more specific than a document..It showss an error for the url.I cannot save it to the post url.The error is : Compile Error: line breaks not allowed in string literals at line 7 column -1 for this line post.LinkUrl= URL(cs18.salesforce.com/sfc/#version?selectedDocumentId='+ cv.ContentDocumentId) ;
    – sahitya
    Commented Apr 28, 2014 at 5:11
  • If I do this: string s = 'cs18.salesforce.com/sfc/#versionselectedDocumentId='+ cv.ContentDocumentId; post.LinkUrl= URL(s) ;...........error is Compile Error:Initial term of field expression must be a concrete SObject: LIST<ContentVersion> at line 6 column 73
    – sahitya
    Commented Apr 28, 2014 at 5:15
0

I have tried writting trigger on ContentDocument. It is working as of now..

Here it is the code :-

trigger PublishlinkOnChatter on ContentDocument (After insert,After Update) {
    list<FeedItem> lst=new List<feedItem>();
    for(ContentDocument cd:trigger.new){
            FeedItem fi= new FeedItem();
            fi.LinkUrl='https://na15.salesforce.com/'+cd.LatestPublishedVersionId;
            fi.parentid=userinfo.getUserID();
            fi.body='Modified the Content';
            lst.add(fi);


        }
        insert lst;
}

You must log in to answer this question.

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