前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Salesforce 如何使用Trigger改变上传后的文件名

Salesforce 如何使用Trigger改变上传后的文件名

原创
作者头像
repick
发布2022-03-29 12:31:24
1.1K0
发布2022-03-29 12:31:24
举报
文章被收录于专栏:SalesforceSalesforce

关于文件上传,以下三个Object之间的关系,我们在之前提到过,并且试着开发了完全自定义的文件上传功能的Lwc组件,今天我们使用Trigger看看可以解决什么样的问题。

·ContentVersion

·ContentDocumentLink

·ContentDocument

1.需求描述:

以下使用Lightning标准组件上传文件时,文件名一定是我们上传时选择的文件名。如果需要文件名自定义的情况下,比如文件名用当前Contact的【LastName】+固定文言【-consent】要如何实现呢,当然用我们之前做的自定义Lwc可以实现这个需求,但是开发量有点大,如果继续使用标准上传功能的基础上,我们试着用Trigger来实现这个需求。

1.Trigger类

通常对自己Object的来说应该使用BeforeInsert来实现这一需求,但是我们需要Contact表中LastName,然后Contact中的数据又必须通过ContentDocumentLink表中的【LinkedEntityId】来取得,因为在BeforeInsert中还没有建立关联关系,所以考虑使用【AfterInsert】

ContentVersionTrigger.Trigger

代码语言:javascript
复制
trigger ContentVersionTrigger on ContentVersion (before insert, after insert) {
    if(Trigger.isinsert && Trigger.isAfter ){
        ContentVersionHelper.AfterInsert(Trigger.new,Trigger.newMap);
    }
}

2.Apex类

通过ContentDocumentLink表中的【LinkedEntityId】来取得Contact表中的LastName。

ContentVersionHelper.cls

代码语言:javascript
复制
public with sharing class ContentVersionHelper {
    /**
     * @name AfterInsert
     * @description
     * @param List<ContentVersion> newList
     * @param Map<Id, ContentVersion> newMap
     * @return void
    **/
    public static void AfterInsert(List<ContentVersion> newList, Map<Id, ContentVersion> newMap){
        Set<Id> condoIdSets = new Set<Id>();
        for(ContentVersion newContentVersion : newList){
            condoIdSets.add(newContentVersion.ContentDocumentId);
        }
        Map<Id, Id> documentId2LinkedEntityIdMaps = new Map<Id, Id>();
        Set<Id> linkedEntityIdSets = new Set<Id>();
        
        if (condoIdSets !=null && !condoIdSets.isEmpty()) {
            List<ContentDocumentLink> conDocuList = [SELECT Id, LinkedEntityId, ContentDocumentId
                                FROM ContentDocumentLink where ContentDocumentId IN :condoIdSets];
            if (conDocuList != null && conDocuList.size() > 0) {
                for (ContentDocumentLink conDocuLink : conDocuList) {
                    String sobjectType = conDocuLink.LinkedEntityId.getSObjectType().getDescribe().getName();
                    if (sobjectType == 'Contact') {
                        linkedEntityIdSets.add(conDocuLink.LinkedEntityId);
                        documentId2LinkedEntityIdMaps.put(conDocuLink.ContentDocumentId, conDocuLink.LinkedEntityId);
                    }
                }
            }
        }
        List<ContentVersion> newContentVersionList = new List<ContentVersion>();
        if (documentId2LinkedEntityIdMaps != null && !documentId2LinkedEntityIdMaps.isEmpty()) {
            List<Contact> contactList = [SELECT Id, Name, LastName FROM Contact WHERE Id IN :linkedEntityIdSets];
            Map<Id, Contact> contactId2ContactMap = new Map<Id, Contact>(contactList);
            for(ContentVersion newContentVersion : newList){
                ContentVersion newContentVersionItem = new ContentVersion();
                Id contactId = documentId2LinkedEntityIdMaps.get(newContentVersion.ContentDocumentId);
                if (contactId2ContactMap != null && !contactId2ContactMap.isEmpty()) {
                    newContentVersionItem.Id = newContentVersion.Id;
                    newContentVersionItem.Title = contactId2ContactMap.get(contactId).LastName + '-consent';
                    newContentVersionList.add(newContentVersionItem);
                }
            }
            update newContentVersionList;
        }
    }

    /**
     * @name BeforeInsert
     * @description
     * @param List<ContentVersion> newList
     * @return void
    **/
    public static void BeforeInsert(List<ContentVersion> newList){

    }
}

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档