Validations in groovy for business object

We can write simple or complex validations at business objects level in groovy

  • In some cases we want to check if age > 18.
  • In some case we want to check for the object row the status is approved then no changes should happen.
  • In some cases we want to allow only the same user to edit the record.
  • In some case we want to validate using some external API ( to check availability of quantity or price ).

For all these we can use validations using groovy.

General idea is to create a object function using groovy to validate and use this object function in before insert trigger or before update trigger.

Let us say we have a Business Object Employee with fields (id, first name, last name, age) and we want to have validation that age should be at least 18 years.

Object function code.

if ( age < 18 ) {
  throw new oracle.jbo.ValidationException('Age should not be less than 18');
}

Let us say we have a Business Object ApprovalBO with fields (id, approval name, status {draft, created, pending approval, rejected, approved} ) and we want to restrict user to not edit any data once the record status is set to approved or rejected.

def origStatus = getOriginalAttributeValue('status')
if ( origStatus == 'Approved' || origStatus == 'Rejected' ) {
  throw new oracle.jbo.ValidationException('Changes not allowed for this record as status is ' + origStatus);
}

Let us say we have a Business Object Transaction with fields (id, transaction name, created by, updated by) and we want to have validation that only created by user can update the value of record.

def secCtx = adf.context.getSecurityContext()
def user = secCtx.getUserName()

if ( createdBy != user ) {
  throw new oracle.jbo.ValidationException('Only created user can update the record');
}

Let us say we have a Business Object Inventory with fields (id, inventory name, quantity, price) and we want to have validation against a external api. We can configure the external api using service connections. We can make rest call in groovy and validate against it.

def priceSvc = newService('priceService');
def responseObj = priceSvc.getQuantityAndPrice();
def quantityApi = responseObj.quantity;
def priceApi = responseObj.price;

if ( quantity > quantityApi ) {
  throw new oracle.jbo.ValidationException('Quantity entered is greater that avalible quantity from api (' + quantityApi + ')');
}

if ( price > priceApi ) {
  throw new oracle.jbo.ValidationException('Price entered is greater that avalible price from api (' + priceApi  + ')');
}

Reference – https://docs.oracle.com/en/cloud/paas/integration-cloud/visual-developer/trigger-rules-business-objects-1.html

https://docs.oracle.com/en/cloud/paas/app-builder-cloud/visual-builder-groovy/groovy-tips-and-techniques.html#groovy-tips-and-techniques