Skip navigation links

Package com.veeva.vault.sdk.api.queue

This package provides interfaces to send and receive Spark messages.

See: Description

Package com.veeva.vault.sdk.api.queue Description

This package provides interfaces to send and receive Spark messages.

To use Spark messages, you must first set up a Connection and Queue in Vault Admin for each vault that needs to send or receive a message. Connections can be set up for the local vault, with another vault, or with an external application. Learn more about Spark Messaging in the Developer Portal.

Example: Basic Usage

The following example creates a CrossLink and starts a workflow with information from a Spark message.
  @MessageProcessorInfo()
  public class ProcessStudy implements MessageProcessor {
          public void execute(MessageContext context) {
              String docId = context.getMessage().getAttribute("docId",MessageAttributeValueType.STRING);
              String docName = context.getMessage().getAttribute("docName",MessageAttributeValueType.STRING);
              String remoteVaultId = context.getRemoteVaultId();

              HttpService httpService = ServiceLocator.locate(HttpService.class);

              // When a message is received, create a CrossLink to the document in the source vault.
              HttpRequest httpRequest = httpService.newLocalHttpRequest()
                  .setMethod(HttpMethod.POST)
                  .setBodyParam("source_document_id__v",docId)
                  .setBodyParam("source_vault_id__v", remoteVaultId)
                  .setBodyParam("source_binding_rule__v", "Latest version")
                  .setBodyParam("name__v",docName)
                  .setBodyParam("type__v", "TypeX")
                  .setBodyParam("lifecycle__v", "General Lifecycle")
                  .appendPath("/api/v18.3/objects/documents");

              httpService.send(httpRequest,HttpResponseBodyValueType.JSONDATA)
                  .onSuccess(response -> {
                      // After CrossLink is created, start the Approval workflow
                      JsonData body = response.getResponse();
                      String crossLinkDocId = body.getJsonObject().getValue("id", JsonValueType.NUMBER).toString();
                      HttpRequest startWorkflowRequest = httpService.newLocalHttpRequest()
                          .setMethod(HttpMethod.PUT)
                          .setBodyParam("Approver", RequestContext.get().getCurrentUserId())
                          .setBodyParam("dueDate", LocalDate.now().plusDays(30).toString())
                          .appendPath("/api/v18.3/objects/documents/" + crossLinkDocId + "/versions/0/1/lifecycle_actions/startApproval");
                      httpService.send(startWorkflowRequest,HttpResponseBodyValueType.STRING)
                          .onError(starkWorkflowResponse -> {
                          int startWorkflowResponseCode = starkWorkflowResponse.getHttpResponse().getHttpStatusCode();
                          ServiceLocator.locate(LogService.class).info("RESPONSE: " + responseCode);
                      })
                          .execute();
                 })
                 .onError(response -> {
                      int responseCode = response.getHttpResponse().getHttpStatusCode();
                      ServiceLocator.locate(LogService.class).info("RESPONSE: " + responseCode);
                 })
                 .execute();

          }
     }
 
 

Example: Set Error

The following example creates a new Product record, sets an error record, and throws a RollbackException when the operation fails.
   @MessageProcessorInfo()
     public class ProcessProduct implements MessageProcessor {

      {@literal @}Override
      public void execute(MessageContext context) {
         RecordService recordService = ServiceLocator.locate(RecordService.class);
         Record record = recordService.newRecord("product__v");
         record.setValue("name__v", "productName");
         List<BatchOperationError> errors = new ArrayList<>();
         recordService.batchSaveRecords(VaultCollections.asList(record)).
             onErrors(le -> errors.addAll(le)).
             execute();

         if (! errors.isEmpty()) {
             // Create a new error record and set to the ErrorProvider
             Record errRecord = recordService.newRecord("exception_message__sys");
             // Below are 5 required fields
             errRecord.setValue("name__v", "productCreationError");
             errRecord.setValue("error_message__sys", "Product batch operation error");
             errRecord.setValue("integration__sys", "V2T000000001002");
             errRecord.setValue("integration_point__sys", "V2U000000001001");
             errRecord.setValue("error_type__sys", Collections.singletonList("item_processing_error__sys"));
             messageContext.getErrorProvider().setError(errRecord);

             // Rollback the entire transaction
             throw new RollbackException("TRACKED_AND_PROPAGATED_ERRORS", "Product batch operation error");
         }
      }
    }
  
  
Skip navigation links

Copyright © Veeva Systems 2017–2021. All rights reserved.