Assigning object field via entity reference

I am trying to assign an object to an object field using entity reference in v17.2. It seems like it should be as simple as:

<code 4D>
$entity:=ds.Entity.new()
$entity.Name:=“Some Name”
$entity.CreatedOn:=Current date
$entity.ObjectField:=New object()
// $entity.ObjectField still equals null

</code 4D>

However, $entity.ObjectField is still null. I’ve tried many variations, like creating an object variable and assigning that, using OB SET, both empty and populated objects, even JSON Stringify($tempObj). There are no errors for any direct assignments. If I try assigning a value to a key in the object field, I get an error that an object was expected.

<code 4D>
$entity.ObjectField.key:=“value”
// error: object was expected

</code 4D>

Any ideas?

Hi

I beleive your approach is not the good one.
When you use “New object” command, you get a null object, without anything inside. So, for me, it is norma that you get Null as return value.

In you example, I would do:

<code 4D>
c_object($object)
$object:=new object
$object.key:=“value”

$entity:=ds.Entity.new()
$entity.Name:=“Some Name”
$entity.CreatedOn:=Current date
$entity.ObjectField:= $object // I use also OB COPY to duplicate the reference when needed
entity.save()

</code 4D>

Code not tested but should work fine.

Patrick

: James CRATE

<code 4D>
$entity.ObjectField:=New object()

</code 4D>

Hi Jim,

Omit the parentheses when there are no arguments for “New object”.

Also, check the field type in the structure editor. Is “[Entity]ObjectField” actually typed as “Object”?

If you change the field’s type to “Object”, remember to restart 4D so ORDA knows the change.

Best regards,
Jeremy

See documentation:

  1. https://doc.4d.com/4Dv17/4D/17.2/New-object.301-4387366.en.html New object>
  2. https://doc.4d.com/4Dv17/4D/17.2/Overview.200-4403191.en.html#Paragraph_3783599ORDA prerequisites> (6th-bullet item beginning “• Any modifications…”)

Aha. That field was text, it was just going to be a usage log, and then I decided keeping a collection might be neater than continuously appending text. I had switched it to an object type, but did not restart the database. Once I did that it worked as expected.

: James CRATE

I had switched it to an object type, but did not restart the database.
Maybe an occasion to request a warning from 4D.