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
S Balaji 8S Balaji 8 

Sharing the files to external users.

On Case object under attachment related lists section , we have to override the upload files button and when we click it we should have two options one is public and another one is private , if user clicks public then he should be able to add the attachment should be visible both in Salesforce as well as in community portal.
If it is private then it has to be visible only in salesforce ( right now the default is private which is out of the box ).
DevidDevid

write a trigger and edit the Visibility field of the ContentDocumentLink that is created when the file is uploaded. Although you might want this to be context aware so that you don't do this whenever any file is created (and also requires some technical knowledge).

trigger shareFilesWithCommunityUsers on ContentDocumentLink(before insert){

	Schema.DescribeSObjectResult r = YOUROBJECTAPI.sObjectType.getDescribe();
	String keyPrefix = r.getKeyPrefix();

	for(ContentDocumentLink cdl:trigger.new){
		if((String.valueOf(cdl.LinkedEntityId)).startsWith(keyPrefix)){
			cdl.ShareType = 'I';
			cdl.Visibility = 'AllUsers';
		} 
	}
}

https://salesforce.stackexchange.com/questions/149553/how-to-share-files-uploaded-in-files-related-list-to-community-users
Prateek Prasoon 25Prateek Prasoon 25
To achieve this requirement, you need to create a Visualforce page that overrides the standard attachment upload functionality for the Case object. You will also need to create a custom controller to handle the logic for uploading attachments as public or private.
Here's an example of how you can implement this:
Create a Visualforce page that overrides the standard attachment upload functionality for the Case object. In the Visualforce page, you will add two radio buttons to allow the user to select either public or private for the attachment. You can use the following code:
<apex:page standardController="Case" extensions="CaseAttachmentController">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:outputLabel value="Attachment Visibility:" />
                <apex:selectRadio value="{!attachmentVisibility}">
                    <apex:selectOption itemValue="Public" itemLabel="Public" />
                    <apex:selectOption itemValue="Private" itemLabel="Private" />
                </apex:selectRadio>
            </apex:pageBlockSection>
            <apex:pageBlockSection >
                <apex:inputFile value="{!attachment.Body}" filename="{!attachment.Name}" />
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton action="{!uploadAttachment}" value="Upload" />
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

In this code, we are using the selectRadio component to allow the user to choose either Public or Private for the attachment visibility. We are also using the inputFile component to allow the user to select the file to upload. Finally, we are using the commandButton component to trigger the upload process.
Create a custom controller to handle the logic for uploading attachments as public or private. In the controller, you will define a method to handle the upload process based on the selected visibility. You can use the following code:
public with sharing class CaseAttachmentController {
    public Attachment attachment { get; set; }
    public String attachmentVisibility { get; set; }
    private final Case currentCase;
    
    public CaseAttachmentController(ApexPages.StandardController stdController) {
        currentCase = (Case)stdController.getRecord();
        attachment = new Attachment();
    }
    
    public PageReference uploadAttachment() {
        if(attachment.Body != null) {
            if(attachmentVisibility == 'Public') {
                attachment.ParentId = currentCase.Id;
                attachment.IsPrivate = false;
            } else {
                attachment.ParentId = currentCase.Id;
                attachment.IsPrivate = true;
            }
            insert attachment;
        }
        return new PageReference('/'+currentCase.Id);
    }
}

In this code, we are defining a custom controller called CaseAttachmentController. We are using the attachment variable to store the attachment information, and the attachmentVisibility variable to store the selected visibility. We are also using the currentCase variable to get the current Case record.
In the uploadAttachment() method, we are checking whether the attachment file has been selected or not. If the file has been selected, we are then checking the selected visibility. If the visibility is Public, we are setting the IsPrivate field to false so that the attachment will be visible in both Salesforce and the community portal. If the visibility is Private, we are setting the IsPrivate field to true so that the attachment will be visible only in Salesforce. Finally, we are inserting the attachment

If you find this answer helpful,Please mark it as the best answer.