Log user and date upon row addition
/*
* Log user and date upon row addition
*
* The script checks who and when adds rows to the IJC table.
* Whenever a new row is added, username and time are stored in IJC fields.
* Attention: fields for username and date must be defined in the IJC table
* before running this script.
*
* Usage:
* 1. Add User and Date fields into the table
* 2. Edit the name of User and Date fields
* 3. Run button script
*
* @author Ondrej Svoboda <osvoboda@chemaxon.com>
*/
afterEdit= {event ->
   //Names of fields where username and date is stored
   def userFieldName="username"
   def dateFieldName="textDate"
   println "after edit operation"
    def widget=event.widget
  // find user and date fields
  def userField=widget.boundFields.find{it.name==userFieldName}
  def dateField=widget.boundFields.find{it.name==dateFieldName}
  // get the username and date
  def vs=widget.vertexState
  // get entity from the widget vertex state
  def ety=vs.getVertex().getEntity()
  // get id of the row you are adding
  selectedRowId=vs.getSelectedRowsIds()
  // go to the schema
  def rs=vs.resultSet
  def dataTree=rs.dataTree
  def schema=dataTree.schema
   // get user
   def user=DIFUtilities.findCapability(schema,IJCUserLoginService.class).getMe()
   // get date
   def today = new Date()
   // get entity data provider
   def edp = ety.schema.dataProvider.getEntityDataProvider(ety)
   // get access to the environment through lock
       def lock = edp.lockable.withLock('Updating'){ envRW ->
      // control whether date and user fields are filled. If so, do not update them
      def data=vs.getData(selectedRowId,DFEnvironmentRO.DEV_NULL)
      println data[selectedRowId[0]][userField.id]
      if((data[selectedRowId[0]][userField.id]==null) && (data[selectedRowId[0]][dateField.id]==null)) {
       // Defines empty map
               def vals = [:]
               vals[userField.id] = user.getUsername()
               // date field is defined as a string, the format can be arbitrary
               vals[dateField.id] = today.format("yyyy-MM-dd \'at\' HH:mm:ss")
               // Create the DFUpdateDescription and update the DFEntityDataProvider
                def ud = DFUpdateDescription.create(ety, selectedRowId, vals)
                def submitList = Collections.singletonList(ud)
                edp.update(submitList, DFUndoConfig.OFF, envRW)
               println "Updating field $userFieldName to value ${user.getUsername()} and field $dateFieldName to value $today"
               }
     }
// by returning false, the action proceeds as expected
return false
}