在Salesforce中的Document对象中上载图像

时间:2015-12-10 13:30:46

标签: salesforce visualforce apex

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

我需要这样做,因为图像不在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;
    
    }
}

1 个答案:

答案 0 :(得分:1)

我使用通配符保持@RestResource urlMapping参数相同,因此URI将保持不变。方法名称也保持不变。如果您更新这两个以及上游的呼叫,说“文档”而不是“附件”,那将是理想的。

ParentId上的Document上没有字段Attachment,因此不包括父母逻辑。您还需要将每个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;
    }
}