Skip navigation links

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

This package provides interfaces to manage object records.

See: Description

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

This package provides interfaces to manage object records.

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();
     
 
Skip navigation links

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