Dalet Flex documentation has moved!
This page is no longer actively maintained. For the latest documentation, please visit us at our new support portal: https://support.dalet.com

Defensive script writing

When scripts are writen they should be written defensively. This means that any situation that is unexpected should be coded for and appropriate actioun taken.

For example the following script assumes that a UDO named myname exists:

def execute() {

    def udoService = flexSdkClient.userDefinedObjectService

    def udoObjectQuery = UserDefinedObjectApiQuery.builder()
            .name('myname')
            .build()
    // Should always return a UDO
    def udos = udoService.getUserDefinedObjects('reels', udoObjectQuery)

    // get the first UDO
    def udo = udos.getObjects().find {true}

    def assetPl = NewAssetPlaceholder.builder()
            .type('media-asset')
            .name('demo asset')
            .build()
    def assetService = flexSdkClient.assetService
    def asset = assetService.createAsset(assetPl)
    
    // add the asset as a child to the UDO
    udoService.addChildObject('reals', udo.id, asset.id)

}

The problem with this is that if no UDO named myname exists the job will fail with a NullPointerException. A better solution is to throw an exception with a relevant message:

def execute() {

    def udoService = flexSdkClient.userDefinedObjectService

    def udoObjectQuery = UserDefinedObjectApiQuery.builder()
            .name('myname')
            .build()
    // Should always return a UDO
    def udos = udoService.getUserDefinedObjects('reels', udoObjectQuery)

    if (udos.totalCount === 0) {
        throw new Exception("UDO named 'mynane' does not exist")
    }
    
    // get the first UDO
    def udo = udos.getObjects().find {true}

    def assetPl = NewAssetPlaceholder.builder()
            .type('media-asset')
            .name('demo asset')
            .build()
    def assetService = flexSdkClient.assetService
    def asset = assetService.createAsset(assetPl)
    
    // add the asset as a child to the UDO
    udoService.addChildObject('reals', udo.id, asset.id)

}

Defensive programming is especially important when assuming the existence of certain data. For example, if a script performs an asset search and expects a single result, exceptions should be thrown if zero or multiple assets are found.