Skip to content

ExampleUDMTrigger

smehta-veeva edited this page Jun 9, 2021 · 1 revision

Key Concepts

To run this trigger for every VSDK User-Defined Model Object record create or update event, the events must be declared in the @RecordTriggerInfo annotation:

@RecordTriggerInfo(object = "vsdk_udm_example__c", events = {RecordEvent.BEFORE_INSERT, RecordEvent.BEFORE_UPDATE})

execute Method

In this method we will first fetch the first Record and get the Local and Remote Settings as ExampleSettings user-defined models and add the values to the results field of the first Record.

First, we will need to get the first record.

Record firstRecord = context.getRecordChanges().get(0).getNew();

Then we will get the Local Settings from the vSDK Settings record from the local vault as the ExampleSettings User-defined Model and add it to the Results field of the record. This will use the getLocalSettingsExample method.

ExampleSettings exampleLocalSettings = getLocalSettingExample();
StringBuilder results = new StringBuilder();
results.append("<B>Local Batch Size<B>: ");
results.append(exampleLocalSettings.getBatchSize().toPlainString());

Next, if the Remote Connection field of the record has a value, we will get the Remote Settings from the vSDK Settings record from the remote vault as the ExampleSettings User-defined Model and add it to the Results field of the record. This will use the getRemoteSettingsExample method.

String remoteConnectionId = firstRecord.getValue("remote_connection__c", ValueType.STRING);
if (remoteConnectionId != null) {
    String remoteConnectionName = getConnectionName(remoteConnectionId);

    ExampleSettings exampleRemoteSettings = getRemoteSettingExample(remoteConnectionName);
    results.append("<BR> ");
    results.append("<B>Remote Batch Size<B>: ");
    results.append(exampleRemoteSettings.getBatchSize().toPlainString());
}

getLocalSettingsExample method

This method uses the CustomSettingService User-defined Service to get the local settings in an Example Settings UDM.

CustomSettingService settingService = ServiceLocator.locate(CustomSettingService.class);
ExampleSettings jobSetting = settingService.getLocalSettings(ExampleSettings.class);

If the vSDK Setting record is not found in the local vault, then an empty ExampleSettings UDM is created using the UserDefinedModelService.

It is then set with default values and saved using the saveLocalSettings method of the CustomSettingService.

...
UserDefinedModelService modelService = ServiceLocator.locate(UserDefinedModelService.class);

jobSetting = modelService.newUserDefinedModel(ExampleSettings.class);
jobSetting.setBatchSize(new BigDecimal(DEFAULT_BATCH_SIZE));
jobSetting.setStatusTypes(VaultCollections.asList(DEFAULT_STATUS_TYPE));

settingService.saveLocalSettings(jobSetting, ExampleSettings.class);

...

getRemoteSettingsExample method

This method is similar to the getLocalSettings method but calls the getRemoteSettings method of the CustomSettingService to get the remote settings in an Example Settings UDM.

...
ExampleSettings jobSetting = settingService.getRemoteSettings(ExampleSettings.class, connectionName);
...

If the vSDK Setting record is not found in the remote vault, then an empty ExampleSettings UDM is created using the UserDefinedModelService.

It is then set with default values and saved using the saveRemoteSettings method of the CustomSettingService.

...
settingService.saveRemoteSettings(jobSetting, ExampleSettings.class, connectionName);
...
Clone this wiki locally