Where are new, custom GlideDBFunctions created?

Jim Kerrigan
Kilo Expert

I see an example to create a custom function and use it in a script (see https://docs.servicenow.com/bundle/kingston-application-development/page/app-store/dev_portal/API_reference/GlideDBFunctionBuilderScoped/concept/GlideDBFunctionBuilderScopedAPI.html#ariaid-title10).

Can I define a custom function and reference it in a table's function field?  If so, how?

Answer


I posted this question thinking that custom functions could be devised and referenced in the
"Function definition" field of a "Function field".  I wanted to know where such functions, once
defined, could be stored.  I know believe that is not possible.  Rather, the GlideDBFunctionBuilder
method is for scripts to define functions for the GlideRecord "addFunction" method.  Example below.

// ------------------------------ ----------- ------------------------------------------- -----------
// Example GlideDBFunctionBuilder 16-mar-2018 Initial version                             Version 1.0
// ------------------------------ ----------- ------------------------------------------- -----------
//
// Exercise the new GlideRecord addFunction method.  Based on ServiceNow documentation
// for "dayofweek" under "GlideDBFunctionBuilder - Scoped, Global" for Kingston.
//
// Define the function.
   var functionBuilder   = new GlideDBFunctionBuilder();
   var dayOfWeekFunction = functionBuilder.dayofweek();
   dayOfWeekFunction     = functionBuilder.field ( 'opened_at' );
   dayOfWeekFunction     = functionBuilder.constant ( '2' );
   dayOfWeekFunction     = functionBuilder.build();
//
// Build an array of days.
   var days = [ 'Code_2', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ];
//
// Show results for ten incidents.
   var gr = new GlideRecord ( 'incident' );
   gr.addFunction ( dayOfWeekFunction );
   gr.setLimit ( 10 );
   gr.query();
   while( gr.next() ) {
          gs.log( gr.number + ' was opened at ' + gr.opened_at.getDisplayValue() +
                  ', a ' + days[gr.getValue(dayOfWeekFunction)], 'GlideDBFunctionBuilder' );
          }
// ------------------------------ ----------- ------------------------------------------- -----------

 

5 REPLIES 5

Chuck Tomasi
Tera Patron

What is the business requirement or use case you are trying to accomplish? Or is this purely R&D for your own curiosity?

Here is a business case (that i'm facing) : 

  • I have the "sys_created_on" field in my "task_time_worked" table
  • I have to make a pivot table to see each day how much time was worked 
  • I can't make it on sys_created_on
  • I have to create a new field that is calculated from"sys_created_on" to transform date/time into date
  • Then I can may a pivot on a day basis

 

The other option is to created a new field and make a business rule that populate the field from "sys_created_on"

 

Jim Kerrigan
Kilo Expert

Chuck ... Curiosity ... just trying to understand what it does since it is a new feature ... Jim

Yes, this is a new function. Check out TechNow ep 43 where we talk about some of the use of this.

 

TechNow Episode list