Record Service & Document Service
Documents and object records are the most common types of data you’ll want to interact with. DocumentService and RecordService provide methods to create, update, and delete documents, document versions, and object records.
These services also allow creating, updating, and deleting attachments.
RecordService also allows setting record migration mode for a specific transaction. When migration mode is on, Vault bypasses entry criteria, entry actions, validation rules, event actions, and reference constraints and does not send notifications when creating or updating records. While record migration mode is on, you cannot change an object’s type in the same transaction.
The trigger in the example below executes after Vault creates a vsdk_service_basics__c object record and uses RecordService to create two related records.
@RecordTriggerInfo(object = "vsdk_service_basics__c", events = {RecordEvent.AFTER_INSERT})
public class vSDKRecordService implements RecordTrigger {
public void execute(RecordTriggerContext recordTriggerContext) {
RecordEvent recordEvent = recordTriggerContext.getRecordEvent();
RecordService recordService = ServiceLocator.locate(RecordService.class);
List<Record> recordList = VaultCollections.newList();
RecordBatchSaveRequest.Builder recordSaveRequestBuilder = recordService.newRecordBatchSaveRequestBuilder();
if (recordEvent.toString().equals("AFTER_INSERT")) {
for (RecordChange inputRecord : recordTriggerContext.getRecordChanges()) {
String name = inputRecord.getNew().getValue("name__v", ValueType.STRING);
String id = inputRecord.getNew().getValue("id", ValueType.STRING);
String relatedTo = inputRecord.getNew().getValue("related_to__c", ValueType.STRING);
// Break out of the trigger code if the new record has a related "vsdk_service_basics__c" record.
// This indicates that the records are "Copy of" records from "vSDKQueryService.java"
// and do not need processing.
if (relatedTo == "" || relatedTo == null) {
// Creates two related records by creating a new record via the RecordService.
// The name of records is set as "Related to: <name> x"
// The relation to the parent to then set with the "related_to__c" object reference field.
for (int i = 1; i <= 2; i++) {
Record r = recordService.newRecord("vsdk_service_basics__c");
r.setValue("name__v", "Related to: '" + name + "' " + i);
r.setValue("related_to__c", id);
recordList.add(r);
}
}
}
recordSaveRequestBuilder.withRecords(recordList);
RecordBatchSaveRequest saveRequest = recordSaveRequestBuilder.build();
// If there are records to insert, the batchSaveRecords takes a RecordBatchSaveRequest as input.
// This request should contain every new record that you are adding or updating.
if (recordList.size() > 0) {
recordService.batchSaveRecords(saveRequest)
.onErrors(batchOperationErrors -> {
// Iterate over the caught errors.
// The RecordBatchOperation.onErrors() returns a list of BatchOperationErrors.
// The list can then be traversed to retrieve a single BatchOperationError and
// then extract an **ErrorResult** with BatchOperationError.getError().
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 '" +
recordList.get(errPosition).getObjectName() + "' record: '" +
name + "' because of '" + errMsg + "'.");
});
})
.execute();
}
}
}
}When retrieving values for formula fields, Vault returns the value of the field after running the formula expression, not the expression itself.
Intelligent Record & Document Update
Section link for Intelligent Record & Document UpdateVault does not update object records or documents when a user or the Vault system saves without making any changes. This means that the Last Modified Date is not changed, record triggers do not execute, and Vault does not create an event in the object record or document audit history.