Load Custom Values Script for select fields with Categories

Any issue about Cyclos 4 scripting and Webservices

Moderators: alexandre, hugo, rmvanarkel

Post Reply
erich.manser
Posts: 24
Joined: Tue Nov 08, 2022 10:40 am

Load Custom Values Script for select fields with Categories

Post by erich.manser »

Hi Everybody,

I've got a user record that uses a shared record field of type single selection. This field has several possible values assigned to categories. This works fine so far!

Then I've created a custom operation of Scope User that should return these user records. In this custom operation I created a form field that should have the same values as the normal filter for this shared record field would have. So since I can't just add the Shared record field (which by the way I would like to see in one of the future relases), I decided to create a script, that would copy the possible values from the original shared record field.

This works fine when returning DynamicCustomValueVO's as in documentation for the possible values. But there is no possibility to set the categories. So I changed to CustomFieldPossibleValueVO and CustomFieldPossibleValueCategoryVO. In the form I get the values as if I would use DynamicCustomValueVO's, again missing the categories and in the wrong order. How Do I have to change this code:

Code: Select all

import org.cyclos.impl.users.RecordCustomFieldServiceLocal
import org.cyclos.model.system.fields.CustomFieldPossibleValueCategoryVO
import org.cyclos.model.system.fields.CustomFieldPossibleValueVO

def fs = (recordCustomFieldService as RecordCustomFieldServiceLocal)

def result = []
def internalNameParam = scriptParameters?.INTERNALNAME?.toString()?.trim()
if (!internalNameParam) {
  throw new RuntimeException("Parameter INTERNALNAME is required for this script")
}

fs.listAllShared().each {fieldDef ->
  if ( fieldDef.internalName == internalNameParam) {
    def possibleValues = fieldDef?.possibleValues
    if (!possibleValues) return

    Map<Long, CustomFieldPossibleValueCategoryVO> categoriesById = [:]

    possibleValues.each { pv ->
      def id = (pv?.id != null ? pv.id.toString() : (pv?.internalName ?: pv?.value?.toString()))
      def label = (pv?.value ?: id)
      def groupLabel = pv?.category?.name
      def groupId = pv?.category?.id

      if (id && label) {
        def value = new CustomFieldPossibleValueVO(id.toLong())
        value.setValue(label)
        if (groupLabel && groupId != null) {
          def cat = categoriesById[groupId as Long]
          if (cat == null) {
            cat = new CustomFieldPossibleValueCategoryVO(
              id: groupId,
              name: groupLabel
            )
            categoriesById[groupId as Long] = cat
          }
          value.setCategory(cat)
        }
        result << value
      }

    }
  }
}

return result
A different approach with a shorter code and the same result:

Code: Select all

import org.cyclos.entities.users.RecordCustomField
import org.cyclos.impl.users.RecordCustomFieldServiceLocal
import org.cyclos.impl.utils.persistence.EntityManagerHandler
import org.cyclos.model.users.recordfields.RecordCustomFieldDTO
import org.cyclos.model.users.recordfields.RecordCustomFieldData

RecordCustomFieldServiceLocal recordCustomFieldService = binding.recordCustomFieldService
EntityManagerHandler entityManagerHandler = binding.entityManagerHandler

def result = []
def internalNameParam = scriptParameters?.INTERNALNAME?.toString()?.trim()
if (!internalNameParam) {
  throw new RuntimeException("Parameter INTERNALNAME is required for this script")
}

def sharedField = entityManagerHandler.find(RecordCustomField, internalNameParam)
RecordCustomFieldData data = recordCustomFieldService.getData(sharedField.id)

return data.getPossibleValues()
Any ideas?
Post Reply