前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >salesforce开发之 文件系统浅析

salesforce开发之 文件系统浅析

原创
作者头像
zcx
修改2022-08-03 22:42:54
7910
修改2022-08-03 22:42:54
举报

序号

内容

对象结构、关系

Classic的附件、Lighting的附件

外部系统与SF文件系统的交互

.对象结构,关系

Salesforce的文件系统依赖于对象,每个对象的每一条记录下都可以关联若干个附件,主要由通过三个标准对象组成sfdc附件系统:

ContentVersion

ContentDocument

ContentDocumentLink

ContentVersion:

使用 ContentVersion 对象用于创建、查询、更新salesforce文件或 Salesforce 文件的特定版本。

字段

描述

ContentLocation

文档的来源。有效值为: S 文档位于 Salesforce 中。标签是Salesforce。(常用) E 文档位于 Salesforce 之外。标签是外部。 L 文档位于社交网络上,可通过社交客户服务访问。

PathOnClient

文档的完整路径(客户端路径)。指定包含路径扩展名的完整路径,以便文档在“预览”选项卡中可见。

Title

文件名

VersionData

可以包括格式正确的 HTML 或纯文本。当通过接口方式上传或下载文档时,它应该是 base64 编码(EncodingUtil.base64Decode)或解码(EncodingUtil.base64Encode)。内容字段中纯文本中的任何特殊字符都必须进行转义。转义特殊字符content.escapeHtml4()

ContentDocumentId

Lookup字段存放ContentDocument的ID。若未设置则自动生成。

开发文档ContentVersion

ContentDocument:

ContentVersion的父对象,使用ContentDocument对象用于检索、查询、更新和删除库或 Salesforce 文件中文档的最新版本

开发文档ContentDocument

ContentDocumentLink:

用于与对象与ContentDocument的关联。

字段

描述

ContentDocumentId

Lookup字段存放ContentDocument的ID。

LinkedEntityId

Lookup字段存放主对象的ID。

ShareType

必需。授予库中共享文件的用户的权限。这是由用户在库中已有的权限决定的。 V:用户可以显式查看但不能编辑文件。 C:用户可以显式查看和编辑文件。 I:用户的权限由相关记录决定。

Visibility

AllUsers:该文件可供所有有权查看该文件的用户使用。 InternalUsers: 该文件仅供有权查看该文件的内部用户使用。 SharedUsers:仅用于与用户共享的文件,并且仅当组织默认启用了私有组织范围共享时才可用。

.clssic的附件、Lighting的附件

VF:

代码语言:javascript
复制
<apex:page controller="FileUploadController" showHeader="false" sidebar="false" >
 <apex:form >
 <apex:pagemessages />
 <apex:pageBlock >
 <apex:pageBlockSection columns="4">
 <apex:inputFile value="{!csvFileBody}" filename="{!csvAsString}"/>
 <apex:commandButton value="Import file" action="{!importFile}"/>
 </apex:pageBlockSection>
 </apex:pageBlock>
 </apex:form>
</apex:page>
public class FileUploadController {
 public transient Blob csvFileBody{get;set;}
 public String csvAsString{get;set;}
 public String oppId{get;set;}
 public FileUploadController(){
 oppId = ApexPages.currentPage().getParameters().get('oppId');
}
 public void importFile(){
 ContentVersion contentVersion_1 = new ContentVersion();
 contentVersion_1.Title = csvAsString;
 contentVersion_1.PathOnClient = csvAsString;
 contentVersion_1.VersionData = csvFileBody;
 insert contentVersion_1;
 List<ContentVersion> list_version = SELECT contentdocumentid,ownerid FROM ContentVersion WHERE id =: contentVersion_1.Id;
 ContentDocumentLink link = new ContentDocumentLink();
 link.ContentDocumentId = list_version0.contentdocumentid;
 link.LinkedEntityId = oppId;
 link.ShareType = 'V';
 insert link;
}
}

因为VF页面最大允许内存135K,如果上传一个超过135K的文件,页面会崩溃。所以Blob对象加上transient瞬态关键字修饰可避免此问题。将变量声明为瞬态变量可以减小视图状态大小。

代码语言:javascript
复制
Lighting Aura:
<div>
 <center>
 <aura:iteration items="{!v.uploadedFiles}" var="item" indexVar="index">
 <tr class="slds-hint-parent">
 <th data-label="" scope="row">
 <ui:outputText value="{!item.name}"></ui:outputText>
 </th>
 </tr>
 </aura:iteration>
 <lightning:fileUpload label="" multiple="true"   
 accept="{!v.accept}" recordId="{!v.recordId}"   
 onuploadfinished="{!c.UploadFinished}" /> 
 </center>
</div>
UploadFinished : function(component, event, helper) {  
 var recordId = component.get("v.recordId");
 var fNameList=event.getParam("files");  
 var toastEvent = $A.get("e.force:showToast");
}   

LWC:

网上有很多。不赘述啦。

与外部系统的交互

上传方法示例:从外部接口获取文件内容(字符串)

EncodingUtil.base64Decode转码base64 转成blob类型放在 contentVersion VersionData字段中

设置文件名,客户端路径等属性

代码语言:javascript
复制
Inert ContentVersion后,生成ContentDocumentLink与对象链接起来
 public static void generateContentFile(string objectId, String fileName, Blob contentData) {
 ContentVersion conVer = new ContentVersion();
 conVer.ContentLocation = 'S';
 conVer.PathOnClient = fileName;
 conVer.Title = fileName;
 conVer.VersionData = contentData;
 insert conVer;
 Id conDoc = [SELECT ContentDocumentId FROM ContentVersion WHERE Id = :conVer.Id].ContentDocumentId;
 ContentDocumentLink conDocLink = New ContentDocumentLink();
 conDocLink.LinkedEntityId = objectId;
 conDocLink.ContentDocumentId = conDoc;
 conDocLink.shareType = 'V';
 insert conDocLink;
    }

下载:生成公开地址链接 :

在ContentVersion触发器里自动关联ContentDistribution对象

在文件生成时自动创建下载链接

代码语言:javascript
复制
trigger ContentVersionTrigger on ContentVersion (after insert) 
{
 if (trigger.isAfter && trigger.isInsert) 
    {
 List<ContentDistribution> itemList = new List<ContentDistribution>();
 for (ContentVersion cv : trigger.new) 
        {
 ContentDistribution newItem = new ContentDistribution();
 newItem.Name = cv.Title;
 newItem.ContentVersionId = cv.Id;
 newItem.PreferencesAllowViewInBrowser= true;
 newItem.PreferencesLinkLatestVersion=true;
 newItem.PreferencesNotifyOnVisit=false;
 newItem.PreferencesPasswordRequired=false;
 newItem.PreferencesAllowOriginalDownload= true;
 itemList.add(newItem);
        }
 INSERT itemList;
    }
}   
代码语言:javascript
复制
 public static ContentDistribution createContentDistribution(Id contentVersionId, String ContentName, Boolean ifDownLoad) {
 ContentDistribution newDist = new ContentDistribution();
 newDist.ContentVersionId = id.valueOf(contentVersionId);
 newDist.Name = ContentName;
 newDist.PreferencesAllowViewInBrowser = true;
 newDist.PreferencesLinkLatestVersion = true;
 newDist.PreferencesNotifyOnVisit = false;
 newDist.PreferencesPasswordRequired = false;
 newDist.PreferencesAllowPDFDownload = false;
 newDist.PreferencesAllowOriginalDownload = ifDownLoad;
 return newDist;
 ContentDistribution newItem = createContentDistribution(cv.id,cv.Title,true);
 insertList.add(newItem);
 distributionList = [SELECT Id,DistributionPublicUrl, PdfDownloadUrl, ContentDownloadUrl,ContentVersionId,PreferencesAllowOriginalDownload FROM ContentDistribution WHERE ContentVersionId IN: cvIdList];

生成DistributionPublicUrl链接传递给外部系统即可。(预览/下载)

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云服务器
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档