+ Start a Discussion
Maheshh PatelMaheshh Patel 

RelatedContent on Opportunity

Hi Team,

On opportunity creation, I need to add a word document to related content related list.
I have written a trigger for the same and by reffering some blog to use contentdocumentlink.
But the document is being referenced in the Notes&attachment section instead of the RelatedContent.
Please find the below piece of code and give some insight...

Map<Id,ContentVersion> idToCvMap = new Map<Id,ContentVersion>([Select Id,ContentDocumentId from ContentVersion where Id IN: cvId]);
ContentDocumentLink cdl;
List<ContentDocumentLink> cdlLst = new List<ContentDocumentLink>();
for(Opportunity Opp: Trigger.New){
    for(string str:idToCvMap.keyset()){
              cdl =  new ContentDocumentLink (LinkedEntityId=Opp.Id,ContentDocumentId=                                     (idToCvMap.get(str)).ContentDocumentId,ShareType='V');
             cdlLst.add(cdl);
             idToCvMap.remove(str);
             break;
    }
 }
 insert cdlLst;


Thanks for the Info :)
 
Agustina GarciaAgustina Garcia
Hi,

Looking at your code seems to be fine. And also I did some researches trying to find out the issue. Unfortunatly I don't have an org with ContentDocument enabled, so I cannot test your code. However I would suggest you to start with something simpler, like this piece of code:
 
ID OPP_ID = 'yourOpportunityId';

ContentNote n = new ContentNote();
n.title = 'My Note from Apex';
insert n;

ContentDocumentLink link = new ContentDocumentLink();
link.LinkedEntityId = OPP_ID;
link.ContentDocumentId = n.id;
link.ShareType = 'V';
link.Visibility = 'AllUsers';
insert link;

Execute it from Developer Console and check where it is created. Maybe the issue comes from the ContentNote instead of the way you create the ContentDocumentLink.

Hope this gives you some ideas.