Managing Undefined Results

I thought I was getting the hang of ORDA but now not so sure. I’m getting error when I’m trying to access an undefined property.

So my case I have two tables litters and dogs. A dog can be a mother or father so I have relations for both. All works very well when I have a mother and a father. However for a new record where the foreign key is not assigned I’m getting errors using the relation attribute.

		$litters:=ds.Litter.query("CompanyID =:1";comp.UUID) // get litters
		
		For each ($element;$litters)
			
			APPEND TO ARRAY(LitterMother_at;$element.MotherToLitter.Name)
			APPEND TO ARRAY(LitterFather_at;$element.FatherToLitter.Name)
			
		End for each 

So in this case I have created and saved a litter record but when I loop through the entities MotherToLitter.Name returns undefined. This seems confusing because the attribute is not undefined just dont have an entity loaded.

I need to use array as this is for a web page. What’s best practice in this situation ?

Thanks
Paul

To solve you have to assign to a local var.

		For each ($element;$litters)
			
			$mothername:=$element.MotherToLitter.Name
			$fathername:=$element.FatherToLitter.Name
			
			APPEND TO ARRAY(LitterMother_at;$mothername)
			APPEND TO ARRAY(LitterFather_at;$fathername)
			
		End for each 

Paul

Very off the subject, but would it not be better to have all the dogs in one table and derive the litter data through parents and date of birth. I use this technique and have for a long time and have not found it disadvantageous for simple pedigree building. I am interested to know if there is a special reason for that structure. Do tell me to mind my own business if appropriate!

On topic thank you for posting – I am struggling with getting ORDA to replace some code and such tips are really useful.

Steve

May I suggest something else, which is based on the third example of the documentation

$litters:=ds.Litter.query("CompanyID =:1";comp.UUID)
 $filter:=New collection
 $filter.push("MotherToLitter.Name")
 $filter.push("FatherToLitter.Name")
 
 $littersCollection:= litters.toCollection($filter)

Patrick

1 Like

Collections are versatile, and I would use it in many places, but in this case, it looks like a #4DLOOP tag is used to generate semi-dynamic content, so I suppose an array is the right choice.

First, sorry if i misunderstood or not fully understand your question/example.
But for me it looks (only after short study your example, not fully study)
like a typical usecase of: COLLECTION TO ARRAY (entitySelection.extract();$myArray)

Maybe you got with this a problem in compiled-mode:

  // maybe this throw an type error when it is in compiled-mode
  // and a element .Name is Null and you want to assign this value to a text-variable
C_TEXT($mothername;$fathername)
$mothername:=$element.MotherToLitter.Name
$fathername:=$element.FatherToLitter.Name

Typical usecase of: COLLECTION TO ARRAY (entitySelection.extract();$myArray)

ARRAY TEXT(LitterMother_at;0)
ARRAY TEXT(LitterFather_at;0)
COLLECTION TO ARRAY($litters.extract("MotherToLitter.Name";ck keep null);LitterMother_at)
COLLECTION TO ARRAY($litters.extract("FatherToLitter.Name";ck keep null);LitterFather_at)

Thanks Patrick and Lutz, Collection to array is way to go can eliminate the for each loop.

Off topic.

Hello Steve, I have a separate litter table to normalise otherwise there would be a number of empty fields that wouldn’t be used in the dog table. For example Litters includes details of mating which doesn’t necessarily result in a litter. It would be simpler and one table and ORDA has enabled me to drop some tables.
Paul

1 Like

Thanks for replying. There are so many ways of doing things: we record matings as a health event in the two dogs records. Trying to learn ORDA (particularly to improve VPN/WAN performance makes me feel like I am starting out all over again), but is proving a focused distraction during a very busy and crazy time.