在 Salesforce 的文档对象中上传图像

Upload image in Document object in Salesforce

我需要以下代码方面的帮助。我需要将我的图像放在 Salesforce 的文档对象中。目前我正在放入笔记和附件,这很好,我需要放入文档对象。

我需要这样做,因为图像不显示在 Word 文件中,但它们显示在 PDF 中。

@RestResource(urlMapping='/SyncAttachments/*')
global with sharing class AssessmentApp_SyncAttachmentsWebService {

global class Image {
   public String primaryKey;
   public String base64;
   public String parentId;
}

@HttpPost   
    global static Map<String, String> syncAttachments(Image image) {
        System.debug(LoggingLevel.Info, 'image ' + image);
        List<Attachment> attachments = [SELECT Id, Name, Body FROM Attachment WHERE Id =:image.primaryKey];
        System.Debug('attachments ' + attachments);
                    //check if attachment is already present. If not, create a new one.
                    Attachment myAttachment;
                    if (attachments.size() == 0) {
                        //Check the parentId of the attachment. Check if parentId belongs to notes
                        myAttachment = new Attachment();
                        myAttachment.Body = EncodingUtil.base64Decode(image.base64);
                        myAttachment.ContentType = 'image/jpg';
                        
                        myAttachment.Name = image.parentId;
                        
                        myAttachment.ParentId = image.parentId;
                        insert myAttachment;
                        
                    }
                    else {
                        myAttachment = attachments[0];
                    }

                
        Map<String, String> responseMap = new Map<String, String>();
        responseMap.put('Success', '1');
        responseMap.put('Message', 'Sync Attachment ' + myAttachment.Name + ' Successfully');
        return responseMap;
    
    }
}

我将 @RestResource urlMapping 参数保持不变,使用通配符,因此 URI 将保持不变。方法名称也保持不变。如果您更新这两个以及上游的调用,说 "Document" 而不是 "Attachment."

,那将是理想的

Document 上没有像 Attachment 上那样的字段 ParentId,因此不包括父子逻辑。您还需要将每个 Document 分配到一个 Folder,您可以更改 FolderId 以正确分配它们。如果您的组织中没有 Folders,这将引发异常。

@RestResource(urlMapping='/SyncAttachments/*')
global with sharing class AssessmentApp_SyncAttachmentsWebService {

    global class Image {
       public String primaryKey;
       public String base64;
       public String parentId;
    }

    @HttpPost   
    global static Map<String, String> syncAttachments(Image image) {
        List<Document> documents = [SELECT Id, Name, Body FROM Document WHERE Id =:image.primaryKey];
        Folder dummyFolder = [SELECT Id FROM Folder LIMIT 1];
        Document myDocument;
        if (documents.size() == 0) {
            myDocument = new Document();
            myDocument.FolderId = dummyFolder.id;
            myDocument.Body = EncodingUtil.base64Decode(image.base64);
            myDocument.ContentType = 'image/jpg';
            myDocument.Name = image.parentId;
            insert myDocument;
        }
        else {
            myDocument = documents[0];
        }       
        Map<String, String> responseMap = new Map<String, String>();
        responseMap.put('Success', '1');
        responseMap.put('Message', 'Sync Document ' + myDocument.Name + ' Successfully');
        return responseMap;
    }
}