我正在尝试将完整的DocuSign文档附加到启动该文档的联系人或客户记录中(在Notes或Notes&Attachments中)。
我已经找到了有关attaching documents to an Opportunity以及附加到custom objects的说明。我已经完成了这些步骤,并尝试为联系和客户记录定制说明,但是没有成功。
请让我知道我可以采取的步骤来完成此操作。非常感谢!
答案 0 :(得分:0)
如果您正在通过REST API与docusign通信,那么您应该收到base64响应。您会将响应正文传递到下面的“ execute”方法中,并将其插入到指定的联系人和相关客户中。导致这种情况的逻辑将需要查询特定的联系人和客户,但是此处的代码是从现有Salesforce ContentVersion中提取并从中创建新内容文档的示例。另外,您将需要解析传入的base64数据以确定文件信息(扩展名,标题),或者匹配响应主体中的扩展名。您的链接如下:
希望这会有所帮助!
public with sharing class uploadDocument {
//Build this to accept whatever you need to relate the document to
public Contact ourContact {get; set;}
//Init
public uploadDocument() {
ourContact = [SELECT Id, Name, AccountId FROM Contact LIMIT 1];
}
public static void execute(String base64EncodedDocument){
uploadDocument classObject = new uploadDocument();
String extension;
//Conditional logic will need to be added if you want to handle other file types
if(base64EncodedDocument.contains('pdf')){
extension = 'pdf';
}
//Prep the raw document for insert via the ContentVersion class
Blob base64DecodedDocument = EncodingUtil.base64Decode(base64EncodedDocument);
ContentVersion sfDocumentObject = new ContentVersion();
sfDocumentObject.VersionData = base64DecodedDocument;
sfDocumentObject.Title = 'Our Test Document';
sfDocumentObject.FirstPublishLocationId = classObject.ourContact.Id;
sfDocumentObject.PathOnClient = '//' + sfDocumentObject.Title + '.' + extension;
insert sfDocumentObject;
//Add additional references for the document
List<additionalIdLink> additionalLinks = new List<additionalIdLink>();
additionalIdLink accountLink = new additionalIdLink();
accountLink.linkedId = classObject.ourContact.AccountId;
accountLink.documentId = sfDocumentObject.Id;
additionalLinks.add(accountLink);
linkToAdditionalRecords(JSON.serialize(additionalLinks));
}
//Ignore, just for testing purposes
//I uploaded a document just to test it and everything worked just fine, preview includedS
private static String setupExample(){
ContentVersion ourTestDocument = [SELECT Id, FileExtension, FileType, Title, VersionData, VersionNumber FROM ContentVersion WHERE Title =: 'Getting Started With Salesforce' ORDER BY VersionNumber DESC][0];
String result = EncodingUtil.base64Encode(ourTestDocument.VersionData);
return result;
}
//Ignore, just for testing purposes
public static void executeExample(){
String base64DocumentReceived = setupExample();
execute(base64DocumentReceived);
}
//Quick Access class to pass the previous values to the Future method
public class additionalIdLink{
public String linkedId {get; set;}
public String documentId {get; set;}
}
//Allows you to link to as many additional records as you need. I haven't run into any use cases where you will be adding over 200 links at a time, but it will throw a query exception if you attempt that
@future
public static void linkToAdditionalRecords(String jsonLinks){
List<additionalIdLink> links = (List<additionalIdLink>)JSON.deserialize(jsonLinks, List<additionalIdLink>.class);
List<ContentDocumentLink> result = new List<ContentDocumentLink>();
for(additionalIdLink link : links){
ContentDocumentLink accountLink = new ContentDocumentLink();
String documentId = [SELECT Id, VersionNumber, ContentDocumentId FROM ContentVersion WHERE Id =: link.documentId LIMIT 1].ContentDocumentId;
accountLink.ContentDocumentId = documentId;
accountLink.LinkedEntityId = link.linkedId;
accountLink.ShareType = 'V';
accountLink.Visibility = 'AllUsers';
result.add(accountLink);
}
if(result.size() > 0){
insert result;
}
}
}