Skip to content

Create Related Records

kevinn-veeva edited this page Nov 16, 2018 · 5 revisions

Business Requirement

When a document enters a defined state, a related object record should be created and assigned to a particular user. The user should receive a notification informing them of the new record.

Action Logic

The CreateRecordsDocumentEntryAction class in the sample project contains code to address this requirement.

Key Concepts

  • This example is done via a custom Entry Action.
  • The document reference field on the object must be set as Specific Version; Latest Version is not supported by the Vault Java SDK:

  • The Document Version ID is retrieved from the document that the action was initialized from.
  • The RecordService is used to create the new vSDK Document Task object record and assign field values to it - such as the related Document ID, Assigned To, and Name fields.
  • When the record is successfully inserted, send a notification to the current user.
    • SDK Actions run in the system context, so standard tokens like ${docName} and ${Object.name__v} will resolve to null. You can define custom tokens, e.g., {$Custom.tokenName}, on the template and then set these in the action with NotificationTemplate.setTemplateName.

Run the action

To see the action in use, configure the debugger and set breakpoints or directly run the deployed code. When ready:

  1. Navigate to the Parent vSDK Document that you created
  2. Select Change State to "Entry Action via SDK" from the gear menu
  3. A vSDK Document Task record will be created and your user will be sent a notification.

Sample code is shown below:

@DocumentActionInfo(name = "vsdk_docaction_create_records__c", label="Run vSDK Create Records Entry Action",lifecycle="vsdk_document_lifecycle__c")
public class CreateRecordsDocumentEntryAction implements DocumentAction {
    
    public void execute(DocumentActionContext documentActionContext) {

        RequestContext requestContext = RequestContext.get();
        RecordService recordService = ServiceLocator.locate(RecordService.class);
        DocumentService docService = ServiceLocator.locate((DocumentService.class));
        NotificationService notificationService = ServiceLocator.locate(NotificationService.class);
    
        DocumentVersion docVersion = documentActionContext.getDocumentVersions().get(0);
        
        List<Record> recordList = VaultCollections.newList();
        List<DocumentVersion> docVersionList = VaultCollections.newList();
        
        String id = docVersion.getValue("id", ValueType.STRING) + "_" + 
                    docVersion.getValue("major_version_number__v", ValueType.NUMBER).toString() + "_" + 
                    docVersion.getValue("minor_version_number__v", ValueType.NUMBER).toString();
        

        //Creates new vSDK Document Task object records and sets the name__v, document__c, and assigned_to__c fields.
        //On a successful insert, the current user will be sent a notification using the "vSDK Object Notification Template"
        Record r = recordService.newRecord("vsdk_document_task__c");
        r.setValue("name__v", "vSDK Document Task " + Instant.now().toEpochMilli());
        r.setValue("document__c", id);
        r.setValue("assigned_to__c", requestContext.getCurrentUserId());
        recordList.add(r);
        
        recordService.batchSaveRecords(recordList)
        .onErrors(batchOperationErrors -> {
            batchOperationErrors.stream().findFirst().ifPresent(error -> {
                String errMsg = error.getError().getMessage();
                int errPosition = error.getInputPosition();
                String name = recordList.get(errPosition).getValue("name__v", ValueType.STRING);
                throw new RollbackException("OPERATION_NOT_ALLOWED", "Unable to create vSDK Document Task records: "
                        + name + " due to " + errMsg);
            });
        }).onSuccesses(successMessage -> {
            
            Set<String> notificationUsers = VaultCollections.newSet();
            successMessage.stream().forEach(positionalRecordId -> {
                
                docVersion.setValue("title__v", "vSDK Document Task: " + positionalRecordId.getRecordId());
                
                //NotificationParameters - sets the user who will be receiving the notification.
                NotificationParameters notificationParameters = notificationService.newNotificationParameters();
                notificationUsers.add(requestContext.getCurrentUserId());
                notificationParameters.setRecipientsByUserIds(notificationUsers);
                notificationParameters.setSenderId(requestContext.getCurrentUserId());
    
                //NotificationTemplate - grabs the Notification template that will be used and 
                //sets a custom token to link the new record Id to the notification.
                NotificationTemplate template = notificationService.newNotificationTemplate()
                .setTemplateName("vsdk_object_notification_template__c")
                .setTokenValue("objectRecordName", positionalRecordId.getRecordId());
    
                //Sends the Notification to the recipient once the document action has successfully completed.
                notificationService.send(notificationParameters, template);
            });
          })
        .execute();
        
        docVersionList.add(docVersion);
        docService.saveDocumentVersions(docVersionList);
    }

    public boolean isExecutable(DocumentActionContext documentActionContext) {
        return true;
    }
}

Clone this wiki locally