Skip navigation links

Package com.veeva.vault.sdk.api.data

This package provides interfaces to manage Vault objects and create record triggers.

See: Description

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

This package provides interfaces to manage Vault objects and create record triggers.

Record Trigger

When you insert, update, or delete an object record, the record passes into a record trigger class. Inside this class you can use custom logic to manipulate the current record or other related records.

A record trigger is a Java class that implements the RecordTrigger interface and has the RecordTriggerInfo annotation as shown below:

 
   @RecordTriggerInfo(object = "product__v", events = RecordEvent.BEFORE_INSERT)
   public class ProductFieldDefaults implements RecordTrigger {

       public void execute(RecordTriggerContext recordTriggerContext) {
           for (RecordChange inputRecord : recordTriggerContext.getRecordChanges()) {
               // Default Expected Date a week ahead
               inputRecord.getNew().setValue("expected_date__c", LocalDate.now().plusWeeks(1));
           }
       }
   }
 
 
The sample code above demonstrates how to manipulate current records by setting the expected_date__c field to a week ahead. The annotation declares that the record trigger executes before a new product__v record is inserted.

In addition to working with the current record, custom logic in a trigger can also use RecordServce to manage other records, such as creating related child records. In the example below, we have an AFTER trigger on the Product object that creates a Product Brand record, which is a child of Product. We then update the parent field to the Product's ID.

     
   RecordService recordService = ServiceLocator.locate(RecordService.class);
   Record record = recordService.newRecord("product_brand__c");
   record.setValue("name__v", "Cholecap");
   record.setValue("product__c", productId);
   List<Record> records = VaultCollections.asList(record);

   recordService.batchSaveRecords(records)
    .onErrors(batchOperationErrors ->{
        batchOperationErrors.stream().findFirst().ifPresent(error -> {
                String errMsg = error.getError().getMessage();
                int errPosition = error.getInputPosition();
                String name = records.get(errPosition).getValue("name__v", ValueType.STRING);
                throw new RollbackException("OPERATION_NOT_ALLOWED", "Unable to create: " + name +
                        "because of " + errMsg);
                });
        })
    .execute();
     
 

Retrieving Vault Object Metadata

To retrieve object and object-field metadata, first create a request object using one of the builders in ObjectMetadataService. Then pass in the request objects to retrieve either the ObjectMetadata or the ObjectField metadata. Once the the metadata object is obtained, retrieve the desired metadata attributes, such as name or label.

The following example retrieves metadata of the country__v object, including the label and active status:

     
     // Initialize service
     ObjectMetadataService objectMetadataService = ServiceLocator.locate(ObjectMetadataService.class);

     // Build the request object for retrieving the country__v object metadata
     ObjectMetadataRequest objectMetadataRequest = objectMetadataService.newObjectRequestBuilder()
         .withObjectName("country__v")
         .build();

     // Retrieve country__v object metadata
     ObjectMetadata countryMetadata = objectMetadataService.getObject(objectMetadataRequest);

     // Retrieve the label and status of the country__v object
     String countryLabel = countryMetadata.getLabel();
     boolean isCountryActive = countryMetadata.isActive();
     
 

The following example uses an optional ObjectFieldFilter to retrieve all required fields of the country__v object and then stores the names in a List:

     
     // Initialize service
     ObjectMetadataService objectMetadataService = ServiceLocator.locate(ObjectMetadataService.class);

     // Get the builder for building the request object of ObjectFieldCollectionResponse
     ObjectFieldCollectionRequest.Builder fieldCollectionRequestBuilder = objectMetadataService.newFieldCollectionRequestBuilder();

     // Build the filter to only get required fields
     ObjectFieldFilter onlyRequiredFieldsFilter = fieldCollectionRequestBuilder.newFieldFilterBuilder().withRequired(true).build();

     ObjectFieldCollectionRequest objectFieldCollectionRequest = fieldCollectionRequestBuilder
         .withObjectName("country__v")
         .withFieldFilter(onlyRequiredFieldsFilter)
         .build();

     ObjectFieldCollectionResponse requiredCountryFields = objectMetadataService.getFields(objectFieldCollectionRequest);

     // Store all the names of the required fields in a List
     List<String> requiredFieldNames = VaultCollections.newList();
     requiredCountryFields.streamFields().forEach(requiredField -> requiredFieldNames.add(requiredField.getName()));
     
 
If a specific subset of fields is required, use one or more of the filters in ObjectFieldFilter.

The following example uses the withUnique() and withRequired() filters to retrieves all unique, non-required fields:

     
     ObjectFieldFilter onlyUniqueAndNotRequired = fieldCollectionRequestBuilder.newFieldFilterBuilder().withUnique(true).withRequired(false).build();
     
 
Skip navigation links

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