Our Blog

Showing Files in Related List in Salesforce

Showing Files in Related List in Salesforce

Showing Files in Related List in Salesforce

We will talk about Salesforce Related list in a Record and showing data into it in this Blog :

 

What is Related List ?

Related lists display records linked to the current record being viewed.

 

How to Show Related List in a Record ?

For Showing Related List to a record, you have to first add it to the Desired Object.Steps :

Move to SETUP -> Object Manager -> Desired Object (Product in this case) -> Page Layouts.

 

Select Related List from Left -> Drag & Drop, Notes & Attachments on the desired place -> Save

Now, It is showing on a record of Product Object as –

 

How to show Image or any other file type in this Related lists ?

We Need to insert data into some SObject for showing files in this Related list.

 

/** Code for adding a record in Content Version Object.
*
* @category  Salesforce
* @author    Webkul Software Pvt Ltd <[email protected]>
* @copyright 2010 webkul.com. All Rights Reserved.
* @license   GNU General Public License version 2 or later; see LICENSE.txt
* @link      http://webkul.uvdesk.com
*/
$contentVersion = new stdClass();
$contentVersion->Title = 'image.jpg';
$contentVersion->VersionData = base64_encode(file_get_contents('image.jpg'));
$contentVersion->PathOnClient = 'image.jpg';

Insert this data to ‘ContentVersion’ Object, you will get its Id in response.

Use this Id to get value of ContentDocumentId from ‘ContentVersion’ Object. Use Query as –

 

/** Code for getting value from a record in ContentVersion Object
*
* @category  Salesforce
* @author    Webkul Software Pvt Ltd <[email protected]>
* @copyright 2010 webkul.com. All Rights Reserved.
* @license   GNU General Public License version 2 or later; see LICENSE.txt
* @link      http://webkul.uvdesk.com
*/
$obtainedContentDocId = SELECT ContentDocumentId FROM ContentVersion WHERE Id='Obtained ContentVersion Id';

 

If you will check ContentDocumentLink Object’s records this time, you will find one entry already maintained for this file on behalf of the user.

/** 
* Code for creating record in ContentDocumentLink Object 
* 
* @category  Salesforce 
* @author    Webkul Software Pvt Ltd <[email protected]> 
* @copyright 2010 webkul.com. All Rights Reserved. 
* @license   GNU General Public License version 2 or later; see LICENSE.txt 
* @link      http://webkul.uvdesk.com 
*/ 
$ContentDocumentLink = new StdClass(); 
$ContentDocumentLink->ContentDocumentId = $obtainedContentDocId;// ContentDocumentId obtained by execution of above query 
$ContentDocumentLink->LinkedEntityId = $sfProductId; //This is the Id of Record of 'Product' Object in which I want to show File in related list. 
$ContentDocumentLink->ShareType = 'I';

Now, Insert this data to ‘ContentDocumentLink’ Object, if no error received in return then you are done.

 

Go back and check the added file in the Related List of Product Object Record. in related list in salesforce.

In conclusion, You can show your own file in the related list of any Object by just entering file
values to its sObject fields.

Support

That’s all for Related List. If you have any issue feel free to add a ticket and let us know your views to make it better https://webkul.uvdesk.com/en/customer/create-ticket/

Webkul is a proud salesforce partner and offering “salesforce consulting services” https://webkul.com/salesforce-consulting-services/

Leave a Comment

Comments (0)

Please verify that you are not a robot.

Welcome back

Welcome back! Please enter your details

One or more fields have an error. Please check and try again.

Forgot Password?

Tell us about Your Company

How can we help you with your business?