-
Notifications
You must be signed in to change notification settings - Fork 3
RecordService
The Vault Java SDK RecordService allows developers to manage - create, read, update, and delete - object records from within custom vault extensions.
For full details on the interfaces and methods used, please review the Javadocs.
The vSDKRecordService trigger demonstrates this functionality by creating two new related records if the inserted record doesn't already have any records related to it. It uses the inserted record's ID to create the related records after it is inserted - i.e., an AFTER_INSERT operation.
- Create two related
vsdk_service_basics__crecords with the inserted record's ID. - If the
related_to__cfield is empty, this is an original record and the related records should be created. - If is not empty, don't create any new records. This indicates that one of the new related records is passing through the trigger.
- The related records will be named
Related to: '<name__v>' 1andRelated to: '<name__v>' 2 - Save the records to vault.
The below code creates two new Record objects that store object record information for vault to ingest.
This is accomplished with a RecordService.newRecord(object_name) call. Once a Record has been initialized, you can set values on the new Record with Record.setValue(field, ValueType).
In this example, we set the name__v of the new records to Related to: '<name__v>' 1 and Related to: '<name__v>' 2. The related_to__c field on both is then set to the inserted record's ID. This creates a relationship between the new records and the original inserted record.
These two new Records are then added to a List that is used as input for the save operation.
RecordService recordService = ServiceLocator.locate(RecordService.class);
List<Record> recordList = VaultCollections.newList();
//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);
}After the Record objects have been initialized with field values, the new records are ready to be saved into the vault database.
The RecordService.batchSaveRecord(List<Record>) performs this save as a batch (bulk) operation. In general, you should parse all new records of the same object into a single list of records (up to 500) and save them with a single batchSaveRecord call.
The batch save method returns a BatchOperation object that requires you either ignore errors, catch errors, or rollback on errors. The below example demonstrates how to catch errors.
//If there are records to insert, the batchSaveRecords takes a List<Record> as input.
//This list should contain every new record that you are adding or updating.
if (recordList.size() > 0) {
recordService.batchSaveRecords(recordList)
.onErrors(batchOperationErrors -> {
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();
}The BatchOperation.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().
If the batchSaveRecords operation is successful, the new record will be saved and visible in vault. If it encounters an error, the transaction will fail and be rolled back (all records) with a RollBackException. The error will then be displayed in the vault UI and in the debug logs.