X

The Visual Builder Cloud Service Blog

  • November 27, 2018

Advanced Business Object Validation with Groovy

Shay Shmeltzer
Director of Product Management - Oracle

One of the powerful aspects of  Visual Builder's business objects is the ability to leverage Groovy code to implement complex logic for them. You can leverage Groovy in triggers, validators, and object functions - all residing under the "Business Rules" tab in Visual Builder design time.

Groovy is quite a simple language, but when coding with the business objects in VB it is helpful to be aware of some unique methods that you can leverage to manipulate your business objects. We now have a book that provide a reference for Groovy usage in Visual Builder - check out the "Groovy Scripting Reference".

One section that is specifically useful is the one titled "Accessing the View Object for Programmatic Access to Business Objects" as it shows you how to work with the data in a business object. Here is an example of how this can be useful.

Example

Suppose that we have a business object called Emp with two fields in it - name and salary. We want to verify that there are no records that have the exact same values for both name and salary. To do this we can add an object level validator (we called it checkUnique):

 

In this validator we add a piece of groovy code that will try and fetch existing records that have the same values as the current record we are working on (adding or modifying the record). The code needs to return true if the changes/addition are ok, and false if we want to fail the validation.

Groovy Validation Logic

The code used in our example is:

def vo = newView('Emp');
vo.appendViewCriteria("name ='"+name+"' and salary = "+salary);
vo.executeQuery();
    def cnt = 0
    while (vo.hasNext()) {
        vo.next()
        cnt = cnt + 1
    }
    if (cnt > 1)
    {
        return false
    }
    else {
        return true
    }

In line 1 we create a new instance of a view for the Emp object.

In line 2 we add a criteria to the view, and in it we use the values from the current record for name and salary. Note that in the condition we use ' to surround character values.

In line 3 we execute a query with this condition.

In the rest of the code we are counting how many records are returned - and if it is more than 1 record we fail the validation.

That's it - quite simple but powerful.

P.S.

If you are planning to do groovy coding, you'll want to quickly review the tips I provided in this blog entry on debugging the code using the log window in Visual Builder.

 

 

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.