HelloWorld Trigger - smehtaca/vsdk-helloworld GitHub Wiki

Summary

This trigger's code is found in the javasdk/src/main/java/com.veeva.vault.custom/triggers/HelloWorld.java file. It is the first in the trigger order and is run before inserting a record. This trigger changes the record's name to have the "Hello, " prefix if it is a new record. If a record that has the same name as an existing record is being created, then it will be prefix with "Copy of ".

In-depth code walkthrough

This trigger's main business logic starts from line #21 onwards.

Annotation

The class annotation (@RecordTriggerInfo) indicates that this class is a record trigger. This annotation specifics the name of the object that the trigger will run on as being that of vsdk_hello_world__c. Additionally, it specifics that the trigger running on the BEFORE_INSERT event. Lastly it defines that this will be the first trigger run on this object.

Execute Method

To implement the RecordTrigger interface, the trigger class must have an execute method.

This trigger first creates an instance of the Query Service which will later be used to determine if a record with the same name already exists.

QueryService queryService = ServiceLocator.locate(QueryService.class);

Then this trigger creates a set of names for each record that is being created. Here we use the Record Context's getRecordChanges function to fetch all of the names of the records being created at once to add them to our set.

// Retrieve Names from all Hello World input records
Set<String> helloObjects = VaultCollections.newSet();

// Get the names for all the records being created
recordTriggerContext.getRecordChanges().stream().forEach(recordChange -> {
    String name = recordChange.getNew().getValue("name__v", ValueType.STRING);
    if(name != null || !name.isEmpty()) {
        helloObjects.add("'Hello, " + name + "'");
    }
});

Next, we convert the set to a comma separated string to be used for querying.

String recordsToQuery = String.join (",", helloObjects);
String queryRecord = "select id, name__v from vsdk_hello_world__c where name__v contains(" + recordsToQuery + ")" ;
QueryResponse queryResponse = queryService.query(queryRecord);

Next, we build a map to store the results of our query in an easy to access manner.

// Build a Map of Record Name (key) and Hello World Records (value) from the query result
Map<String, QueryResult> nameRecordMap = VaultCollections.newMap();
queryResponse.streamResults().forEach(queryResult -> {
    String name = queryResult.getValue("name__v", ValueType.STRING);
    if (!nameRecordMap.containsKey(name)) {
        nameRecordMap.put(name, queryResult);
    }
});

Next, we use a for each loop to loop over our recordChanges and check if the record is in our map.

for (RecordChange inputRecord : recordTriggerContext.getRecordChanges()) {
    String recordName = inputRecord.getNew().getValue("name__v", ValueType.STRING);
    String helloName = "Hello, " + recordName;
    QueryResult existingRecord = nameRecordMap.get(helloName);

Finally, if the record has been found in the map, we set the current record's name to have the "Copy of" Prefix. Otherwise, we set it to have the "Hello, " prefix.

if(existingRecord != null) {
                existingRecordId = existingRecord.getValue("id", ValueType.STRING);
            }

            if(existingRecordId != null) {
                // Create this record as a related record with name to be of type "Copy of 'Hello, name__v'" if record already exists
                inputRecord.getNew().setValue("name__v", "Copy of 'Hello, " + recordName + "'");
                inputRecord.getNew().setValue("related_to__c", existingRecordId);
            } else {
                // Record with same name does not exist, save
                inputRecord.getNew().setValue("name__v", helloName);
            }
⚠️ **GitHub.com Fallback** ⚠️