Collection assignment caveat

There is an edge case when assigning collections to singular variables that is not picked up by the Helium Dev Client, but will cause an issue during runtime on any environment. When attempting to assign a collection of an object using the DSL selectors to a variable of the same object type but is only declared as singular as follows:

unit TestUnit;

void someFunction(){
	Object singular_obj = Object:all();
}

an error similar to this will be thrown in the Glassfish/Portal logs:

7/02/2020, 11:41:54 AM - Runtime Error - [TestUnit:4] com.mezzanine.persistence.pgsql.PgSqlReadOnlyPagingCollectionInstance@6fc48ef7 could not be converted to a CustomObjectType{name=Object} value

This issue might not be picked up by the Helium Dev Client however, and can cause an issue during runtime.

To troubleshoot such an issue, go to the offending line as shown in the error message. Then troubleshoot as follows:

  1. Ensure that the model object being used actually exists in the project's model structure. There is another caveat with Helium where it sometimes might not pick up when using an invalid object name for something that doesn't exist in the project's model structure. See HE-6004 for more insight into this issue and for the progress on resolving this issue.
  2. Ensure that the object types match between the collection of what is returned and what the variable is assigned as, ie

    ...
    Object[] var_name = Object:all(); //Correct way
    ...
    Object[] var_name = DifferentObject:all(); //Incorrect way

    This will most likely be picked up by the Dev Client, but it is a good place to start.

  3. Ensure that the singular (instance)/plural (list) declaration of the variable matches the assignment to that variable, ie

    //Correct way
    Object var_instance = object_list.first(); //Singular declaration and singular assignment
    Object[] var_list = Object:all(); // Plural declaration and plural assignment
    
    //Incorrect way
    Object var_instance = Object:all(); //Singular declaration but plural assignment
    Object[] var_list = object_list.first(); //Plural declaration but singular assignment

    This will resolve the issue as described above and should address or fix it.

These issues might not cause errors and might even work as hoped or expected, but following these guidelines/steps should definitely be considered best practice to ensure any upcoming releases or fixes might not cause more damage than intended.