Package com.veeva.vault.sdk.api.file


package com.veeva.vault.sdk.api.file
This package provides interfaces to retrieve file references from any Vault you can access, either in the current Vault or a remote Vault. The following example uses ConnectionService, DocumentService, and FileReference to set the source file, get new version information, and create a new attachment and rendition.
 
 @DocumentActionInfo(label = "testCode")
 public class DeepCopyDocument implements DocumentAction {
     @Override
     public boolean isExecutable(DocumentActionContext documentActionContext) { return true; }

     @Override
     public void execute(DocumentActionContext documentActionContext) {

         ConnectionService connectionService = ServiceLocator.locate(ConnectionService.class);
         ConnectionContext connectionContext = connectionService.newConnectionContext("connectionApiName", ConnectionUser.CONNECTION_AUTHORIZED_USER);

         DocumentService documentService = ServiceLocator.locate(DocumentService.class);

         DocumentSourceFileReference documentSourceFileReference  = documentService.newDocumentSourceFileReference(connectionContext, "1_0_1");
         DocumentVersion documentVersion = documentService.newDocument();
         documentVersion.setValue("type__v", VaultCollections.asList("Claims"));
         documentVersion.setValue("subtype__v", VaultCollections.asList("Core Message Map"));
         documentVersion.setValue("lifecycle__v", VaultCollections.asList("Claims"));
         documentVersion.setValue("name__v", "file name");
         documentVersion.setSourceFile(documentSourceFileReference);
         documentVersion.suppressRendition();

         SaveDocumentVersionsResponse response = documentService.createDocuments(VaultCollections.asList(documentVersion));

         String newDocVersionId = response.getSuccesses().get(0).getDocumentVersionId();

         // Get new version info
         String[] parts = StringUtils.split(newDocVersionId, "_");
         String docId = parts[0];
         Integer major = Integer.valueOf(parts[1]);
         Integer newMinor = Integer.valueOf(parts[2]) + 1;

         DocumentSourceFileReference secondVersionSourceFileReference  = documentService.newDocumentSourceFileReference(connectionContext, "1_0_2");
         DocumentVersion newVersion = documentService.newVersion(docId);
         newVersion.setValue("type__v", VaultCollections.asList("Claims"));
         newVersion.setValue("subtype__v", VaultCollections.asList("Core Message Map"));
         newVersion.setValue("lifecycle__v", VaultCollections.asList("Claims"));
         newVersion.setValue("status__v", VaultCollections.asList("Draft"));
         newVersion.setValue("name__v", "1-17-2 v2");
         newVersion.setValue("major_version_number__v", BigDecimal.valueOf(major));
         newVersion.setValue("minor_version_number__v", BigDecimal.valueOf(newMinor));
         newVersion.setSourceFile(secondVersionSourceFileReference);

         documentService.migrateDocumentVersions(VaultCollections.asList(newVersion));

         // Create attachment
         DocumentAttachmentFileReference documentAttachmentFileReference = documentService.newDocumentAttachmentFileReference(connectionContext, "1");
         DocumentAttachment documentAttachment = documentService.newDocumentAttachment(documentAttachmentFileReference, docId);
         documentService.createAttachments(VaultCollections.asList(documentAttachment));

         // Create rendition
         DocumentRenditionFileReference documentRenditionFileReference = documentService.newDocumentRenditionFileReference(connectionContext, "1_0_1", "viewable_rendition__v");
         DocumentRendition rendition = documentService.newDocumentRendition(documentRenditionFileReference, "1_0_1", "viewable_rendition__v");
         documentService.createRenditions(VaultCollections.asList(rendition));
     }
 }

 The following example uses QueryService,
 FileHandleService, and RecordService
 to copy an attachment from one object record to another.

 
 @RecordTriggerInfo(object = "product__c", events = {RecordEvent.BEFORE_INSERT})
 public class CopyAttachmentRecordTrigger implements RecordTrigger {

     @Override
     public void execute(RecordTriggerContext recordTriggerContext) {

         // 1. Use QueryService to find a record with an attachment and get the file handle string.
         // This example queries for a 'source_product__c' record to find an attachment.
         // The 'attachment_field__c' on 'source_product__c' stores the file handle.
         QueryService queryService = ServiceLocator.locate(QueryService.class);
         String vql = "SELECT attachment_field__c FROM source_product__c ORDER BY modified_date__v DESC MAXROWS 1";

         QueryExecutionRequest queryRequest = queryService.newQueryExecutionRequestBuilder()
                 .withQuery(vql)
                 .build();

         List recordsToCreate = VaultCollections.newList();
         queryService.query(queryRequest)
             .onSuccess(queryResponse -> {
                 queryResponse.streamResults().findFirst().ifPresent(queryResult -> {
                     String fileHandleString = queryResult.getValue("attachment_field__c", ValueType.STRING);

                     if (fileHandleString != null) {
                         // 2. Use FileHandleService to get an AttachmentFieldFileReference from the file handle string.
                         FileHandleService fileHandleService = ServiceLocator.locate(FileHandleService.class);
                         ConnectionService connectionService = ServiceLocator.locate(ConnectionService.class);

                         // Get a ConnectionContext for the local Vault.
                         ConnectionContext connectionContext = connectionService.newLocalConnectionContext(RequestContextUserType.USER);

                         FileReferenceReadRequest readRequest = fileHandleService.newFileReferenceReadRequestBuilder()
                                 .withFileHandles(VaultCollections.asList(fileHandleString))
                                 .withConnectionContext(connectionContext)
                                 .build();

                         fileHandleService.readFileReferences(readRequest)
                             .onSuccess(fileReferenceReadResponse -> {
                                 fileReferenceReadResponse.streamFileReferences().findFirst().ifPresent(fileReference -> {
                                     if (fileReference instanceof AttachmentFieldFileReference) {
                                         AttachmentFieldFileReference attachmentRef = (AttachmentFieldFileReference) fileReference;

                                         // 3. Use RecordService to create a new record with the attachment.
                                         // This trigger is on 'product__c', so we will create a 'related_product__c' record.
                                         RecordService recordService = ServiceLocator.locate(RecordService.class);
                                         Record newRecord = recordService.newRecord("related_product__c");
                                         newRecord.setValue("name__v", "Product with copied attachment " + UUID.randomUUID());
                                         newRecord.setValue("attachment_field__c", attachmentRef); // Set the attachment
                                         recordsToCreate.add(newRecord);
                                     }
                                 });
                             })
                             .onError(operationError -> {
                                 // Handle errors from readFileReferences
                             }).execute();
                     }
                 });
             }).execute();

             if (!recordsToCreate.isEmpty()) {
                  RecordService recordService = ServiceLocator.locate(RecordService.class);
                  RecordBatchSaveRequest saveRequest = recordService.newRecordBatchSaveRequestBuilder()
                         .withRecords(recordsToCreate)
                         .build();

                  recordService.batchSaveRecords(saveRequest)
                         .onErrors(batchOperationErrors -> {
                             // Handle errors during record creation
                         }).execute();
            }
     }
 }