Skip navigation links

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

This package provides interfaces to manage object records, and to retrieve metadata from object and fields.

See: Description

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

This package provides interfaces to manage object records, and to retrieve metadata from object and fields.

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();
     
 

ObjectMetadataService

This service is responsible for retrieve object metadata and field metadata. Requests are made by using the request object builders provided by the service, and then passing in the request objects to obtain the metadata objects. The different type of metadata object types include: After the metadata object is obtained, you can retrieve the desired metadata attributes, such as name or label. The example below retrieves the country object metadata, including the object label and active status.
 
         //Initialize services
         ObjectMetadataService objectMetadataService = ServiceLocator.locate(ObjectMetadataService.class);

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

         //Retrieve country's metadata
         ObjectMetadata countryMetadata = objectMetadataService.getObject(objectMetadataRequest);

         //Get the label and status of the country object
         String countryLabel = countryMetadata.getLabel();
         boolean isCountryActive = countryMetadata.isActive();
 
 
It is also possible to retrieve for the metadata of multiple fields. Multiple field metadata will be stored in a ObjectFieldCollectionResponse. When retrieving for multiple fields, a filter may also be applied to only retrieve for specific fields. The filter object, ObjectFieldFilter, contains the different type of filters that can be applied. The example code below will retrieve for all required fields on the country object. Afterwards, it will store all the names of the required country fields in a list.
 
         //Initialize services
         ObjectMetadataService objectMetadataService = ServiceLocator.locate(ObjectMetadataService.class);

         //Get the Builder for building the request object of the 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 into a list
         List<String> requiredFieldNames = new ArrayList();
         requiredCountryFields.streamFields().forEach(requiredField -> requiredFieldNames.add(requiredField.getName()));
 
 
Skip navigation links

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