TypeError on onRender Event

Hi,

I have a onRender function for a table view form, because I would like to display all records with the column flag_int (integer) value 1 in a specific fgcolor. The code for this is:

/** @type {JSRecord<db:/database/table>} */
	var record = event.getRecord();
	if(record.flag_int == 1){
		event.getRenderable().fgcolor = '#ff0000';
	} else {
		event.getRenderable().fgcolor = '#000000';
	}

In the Debug Client I can see that this is working, but I always get the error:

TypeError: Cannot read property "flag_int" from (C:\Users\Thomas\servoy_workspace\project_....

Does anybody know what’s wrong?

Regards,

Hi Thomas,

I think the error says it all:

TypeError: Cannot read property “flag_int” from (C:\Users\Thomas\servoy_workspace\project_…

It seems your JSRenderEvent doesn’t give a JSRecord back.
Apart from why that is you should check if you are actually get something back like so:

/** @type {JSRecord<db:/database/table>} */
   var record = event.getRecord();
   if(record && record.flag_int == 1){
      event.getRenderable().fgcolor = '#ff0000';
   } else {
      event.getRenderable().fgcolor = '#000000';
   }

Hope this helps.

Hi Robert,

it seems to be not working on Table view (locked) forms. The event.getRecord() gives null back.
Btw, this is what the tooltip of the JSRenderEvent.getRecord(): JSRecord in the developer says…
Do I set the form property to List view, the onReder event is working as expected.

Is there a way to get this working also on a Table view form?

EDIT
I tried your solution Robert (record && record.flag_int == 1) and this is working on my Table view form!

Thank you!

Regards,

Hi Thomas,

I am glad it’s working (as in not giving errors) for you. But you say that with a table view the JSRenderEvent doesn’t return a JSRecord object but with a listview it does. That sounds to me like a bug and you should report it as such.
What Servoy version is this in ?

Scratch that. I just read what the tooltip said in Servoy 6.1 and it indeed says it will be null when you use it in a table view.

I guess you could make it a feature request.

Hi Robert,

I’m on Servoy 6.0.7 and I will make a feature request for this.

And indeed the onRender function is now working without errors. Curious or, because of the null return?

Regards

tgs:
And indeed the onRender function is now working without errors. Curious or, because of the null return?

It doesn’t error because now you check if there is a value in your record variable.

if ( record ) {
    // I am not null, zero (0), false, undefined or an empty string ('').
} else {
    // I have a value (other than 0 or false or '')
}

So using if (record && record.flag_int == 1 ) will make the condition fail before it tries to access record.flag_int and therefor won’t generate an error.

Hope this explains things.

Yes Robert,

I understand this, but I thought in Table view the onRender event return always null? Why is it working (coloring the fgcolor of the flagged records) because the event must return anything and not null?

Regards,

Hi Thomas,

It’s not the event that is null, it’s the result of event.getRecord(). According to the docs this will return null when used in a table view.

event.getRecord() does not always return null, in a table-view, but CAN return null.
I talked to Johan about this a while ago, and this is because the onRender is much triggered, (also over elements, that do not have recordObject attached)
and his advice was also, to ALWAYS check the recordObject first.

Hi Harjo,

thank you for the clearing words! It would be nice to have this in function tooltip.

Regards,

So does this mean that the ability I had to highlight certain records in the old form bgcolour property has now been lost.

I wanted to show certain “cancelled” records with a red background. The only way i can do this is by referring to a record field and it seams the onRender function cannot do this like the old bgcolour property used to do?

David

I needed the same functionality as well using a “return” calculation / virtual column and was getting the same “Type error: Cannot read…” error. After searching the forum and coming across this post i have it working now. Curious as to why, i stepped through it in the debugger and the reason (one of, anyway) it is necessary to test for a record object is that the onRender event first fires for the form object itself and therefore the event.getrecord() is null (and Index = -1 ). So as reported above testing for the record is necessary. After that the onRender is fired for the table view rows/records and the results are as expected.

The Point being to answer David’s question: No, you don’t lose the functionality from bgcolour property. Just done in a slightly different way.