Prevent record insert in Apex

Prevent record save in Apex

You can create validations in the Apex trigger code to prevent the insertion of selected records?

Did you know that:

You can create validations in the Apex trigger code to prevent the insertion of selected records?

Don't do this
for (SObject record : Trigger.new) {
    if (!isValid(record)) {
        throw new DmlException();
    }
}

Prevent saving only records that failed validation:

Do this
for (SObject record : Trigger.new) {
    if (!isValid(record)) {
        record.addError('Validation failed!')
    }
}

More details: SObject Class reference

Text and code were extracted from the original slide. Plain-text version of the whole catalog