+ Start a Discussion
Shubham Panda 7Shubham Panda 7 

copying assignments account to contact and opportunity

i want to attach a file in account and want to show the same attached file in the related contact and related opportunity.
i know this can be achieved by trigger and i tried also but i'm getting the reverse of it.
can somebody help me to achieve this??
Best Answer chosen by Shubham Panda 7
Jose Lopez 18Jose Lopez 18

You can accomplish this by creating a new ContentDocumentLink and assigning the contactId and the opportunityId to the ContentDocumentLink.LinkedEntityId. Below is a brief sample of how this can be accomplished. You need some understanding of how salesforce files work. This is a good start. https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_erd_content.htm#sforce_api_erd_content
 

trigger AccountTrigger on Account (after update) {
  List<ContentDocumentLink> newLinks = new List<ContentDocumentLink>();
  //Store uploaded file's link to this account
  ContentDocumentLink accDocLink= [select ContentDocumentId, LinkedEntityId, 
    ShareType from ContentDocumentLink where LinkedEntityId = :trigger.new.Id];

  //Clone the link(s)
  //Give these new content document links the id of the contact and the opportunity
  ContentDocumentLink contactFileLink = accDocLink.clone();
  contactFileLink.LinkedEntityId = contactId; // you need to use a different soql to get this
  newLinks.add(contactFileLink);
  ContentDocumentLink oppFileLink = accDocLink.clone();
  opportunityFileLink.LinkedEntityId = opportunityId; //you need to use a different soql to get it
  newLinks.add(oppFileLink);

  //and finally finish creating these new links
  insert newLinks;
}
Hope this helps answer your question.

--Jose

All Answers

Jose Lopez 18Jose Lopez 18

You can accomplish this by creating a new ContentDocumentLink and assigning the contactId and the opportunityId to the ContentDocumentLink.LinkedEntityId. Below is a brief sample of how this can be accomplished. You need some understanding of how salesforce files work. This is a good start. https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_erd_content.htm#sforce_api_erd_content
 

trigger AccountTrigger on Account (after update) {
  List<ContentDocumentLink> newLinks = new List<ContentDocumentLink>();
  //Store uploaded file's link to this account
  ContentDocumentLink accDocLink= [select ContentDocumentId, LinkedEntityId, 
    ShareType from ContentDocumentLink where LinkedEntityId = :trigger.new.Id];

  //Clone the link(s)
  //Give these new content document links the id of the contact and the opportunity
  ContentDocumentLink contactFileLink = accDocLink.clone();
  contactFileLink.LinkedEntityId = contactId; // you need to use a different soql to get this
  newLinks.add(contactFileLink);
  ContentDocumentLink oppFileLink = accDocLink.clone();
  opportunityFileLink.LinkedEntityId = opportunityId; //you need to use a different soql to get it
  newLinks.add(oppFileLink);

  //and finally finish creating these new links
  insert newLinks;
}
Hope this helps answer your question.

--Jose
This was selected as the best answer
Shubham Panda 7Shubham Panda 7
thank you jose lopez.
its working