X

The Visual Builder Cloud Service Blog

Service Methods for Business Objects in Visual Builder

Shay Shmeltzer
Director of Product Management - Oracle

In a previous blog I showed you how to create an object function for a business object to allow you to expose specific functionality on a specific row. This blog is a little variation on this capability showing you how to create service methods in your data layer that are not related to a specific row. 

This is based on a use case a customer had where they would get data for a record in a table, if the row already exists in the table they need to update it. If it doesn't exist they need to create a new row with the data. The REST API provided by the Visual Builder business objects supports this use case as long as you have the id of the record you want to update. This is done with the upsert option for a POST call - see the information about this combined insert/update operation here.

But the data that the customer got didn't have the id of the record they would need to update. Instead they had the information about another field in the object. To resolve this we can create a function in the BO layer that will search the set of records for a specific value in a specific field, and then update that record if the value is found, or insert a record if it doesn't exist.

The tricky part is that object functions work on a specific record in a BO (the id of that record is part of the path to that REST endpoint). In our case, we don't have a row to work on. Instead, we are going to create a dummy business object, with a single row in it. Then we'll create the function on this business object, always invoking it with the id of the row that is in there. This technique could be useful for any generic function you need in you BO layer that doesn't directly relate to a specific row.

The function itself is written in Groovy, the programming language used by the Visual Builder business objects layer. In the video you'll see how we use various methods available on BOs in groovy (such as methods to query, add filter condition, and insert new records). You can learn more about these and other methods in our new Groovy for Visual Builder Reference Book.

In the video below you'll see how to add such a function and how to call it from the VB user interface. Note that these functions are also callable from other tools through simple REST calls.

 

For those interested in the specific code used in the sample it is:

def vo = newView('Department')
vo.appendViewCriteria("department = '"+dname+"'")
vo.executeQuery()
if (!vo.hasNext()){
  r = vo.createRow()
  r.maxSalary=salary;
  r.department=dname;
  vo.insertRow(r)
}
while (vo.hasNext()) {
  def r = vo.next();
  r.maxSalary = salary;
}

 

Line 1 gets us a pointer to our business object

Line 2 adds a "where" clause to the query associated with our instance

Line 3 execute the query with this where clause

in the If section starting at line 4 we take the case where no records where found, create a new row, populate the data in the fields in that row, and then insert it into the business object.

In the while loop we handle the case where a record (or more) were found, and update that row with the new value.

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.