# Help on Timing on Initial Window Populate

**URL:** <https://forum.inductiveautomation.com/t/help-on-timing-on-initial-window-populate/47798>\
**Category:** Ignition\
**Created:** [June 15, 2021, 4:18pm UTC](https://forum.inductiveautomation.com/t/help-on-timing-on-initial-window-populate/47798 "2021-06-15T16:18:10Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![pmo](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@pmo](https://forum.inductiveautomation.com/u/pmo)\
**Post date:** [June 15, 2021, 4:18pm UTC](https://forum.inductiveautomation.com/t/help-on-timing-on-initial-window-populate/47798/1 "2021-06-15T16:18:10Z")

</div>

I am trying to populate a form with SQL data when a from opens.

- On the internalFrameActivated Event I run a call to a Root Container Script called Populate

> event.source.getComponentForPath('Root Container').Populate(system.gui.getParentWindow(event).getComponentForPath('Root Container').Vessel)

- In the Populate Script, I run a SQL Query that is based on a Custom Property for the window that is tied to a PLC Tag.
- I have tried to reference the Custom Property directly and by reference the Property when the data is linked to a label.
- The issue that I am having is that SQL Script is running before the Custom Property is updated, so I am getting data from the previous instance of the form and not the current instance.

So my question is how do I force the SQL Script to run when the Custom Property is updated on Window open?

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [June 15, 2021, 4:22pm UTC](https://forum.inductiveautomation.com/t/help-on-timing-on-initial-window-populate/47798/2 "2021-06-15T16:22:32Z")

</div>

I think you’re overcomplicating things a bit, but then I don’t have a good understanding of what you’re trying to accomplish.

What is the result of this SQL Script? Why does it need to be called from the internalFrameActivated event?

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [June 15, 2021, 4:26pm UTC](https://forum.inductiveautomation.com/t/help-on-timing-on-initial-window-populate/47798/3 "2021-06-15T16:26:03Z")

</div>

I would have a custom property on the root container `In_FormData` or similar naming wise that is selecting all your form data.

Then under each component, a custom property that I usually call `initialValue`, and it’s an expression of the form `try({Root Container.In_FormData}[0, 'someColumn'], 'defaultValue')`. Then I bind the appropriate component property to initial value.

You could try writing a script to populate each component but you will probably want a naming convention on your components to know 1) which components need to be populated and 2) what property of said component needs the populating.

I see you mentioned you are getting the old form data. What is the windows caching policy? Try setting it to `Never`.

---

<div class="post-metadata">

**Author:** ![pmo](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@pmo](https://forum.inductiveautomation.com/u/pmo)\
**Post date:** [June 15, 2021, 4:29pm UTC](https://forum.inductiveautomation.com/t/help-on-timing-on-initial-window-populate/47798/4 "2021-06-15T16:29:28Z")

</div>

I have a Window that I want to populate Fields and Labels with values from the database based on the value of a Tag from a PLC that is set a Custom Property for the Window.

In this case, I have an Order Number in the PLC that is in a Vessel Structure, so every Vessel Detail should produce unique values from the Database.

I am using the internalFrameActivated Event because that is the only way that I have found to trigger something on Window Open. My Python/Ignition Scripting is still weak and heavily influenced by years of only having VBA for my scripting.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [June 15, 2021, 4:32pm UTC](https://forum.inductiveautomation.com/t/help-on-timing-on-initial-window-populate/47798/5 "2021-06-15T16:32:23Z")

</div>

You should be able to bind each Field to a query which uses the custom property, the bindings will update once when the window opens. No need for using the internalFrameActivated event in this case.

> [@pmo](#):
>
> heavily influenced by years of only having VBA for my scripting.

🤮 I feel your pain

---

<div class="post-metadata">

**Author:** ![pmo](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@pmo](https://forum.inductiveautomation.com/u/pmo)\
**Post date:** [June 15, 2021, 4:40pm UTC](https://forum.inductiveautomation.com/t/help-on-timing-on-initial-window-populate/47798/6 "2021-06-15T16:40:58Z")

</div>

Thanks for pointing me in a new direction!!!

I will miss my hammer but this screwdriver does seem useful.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [June 15, 2021, 9:56pm UTC](https://forum.inductiveautomation.com/t/help-on-timing-on-initial-window-populate/47798/7 "2021-06-15T21:56:27Z")

</div>

I’ll chime in here and say that it’s far more efficient the original way you were doing this, than to be calling the “same” query 50 times (or however many fields you have). Travis Cox did a presentation on improving performance, and this was one thing he specifically mentioned. Personally, I would continue down the first route, but you don’t want to use that Window event, you would put your script inside the propertyChange event and look for a change in your passed in property. And as @bkarabinchak.psi already mentioned, make sure to name your components usefully, for example with the sql field name within it, so that you can easily write to these components within a loop.  
E.g.

```auto
ds = system.db.runPrepQuery('SELECT ....')
pds = system.dataset.toPyDataSet(ds)
fieldNames = list(ds.getColumnNames())
for field in fieldNames:
    event.source.getComponent("UserInput_{}".format(field)).text = pds[0][field]

```

\*\* `'{}'.format(field)` will only work for Jython 2.7 (Ignition v8 and above). Use `'%s' % field` for v7  
\*\*\* Not tested

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [June 16, 2021, 12:06pm UTC](https://forum.inductiveautomation.com/t/help-on-timing-on-initial-window-populate/47798/8 "2021-06-16T12:06:50Z")

</div>

My understanding was that the fields where different but all used the same information in the where clause.

I’m wondering if this is the best approach or if populating a dataset and then using expression bindings to lookup the values wouldn’t be better?

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [June 16, 2021, 12:10pm UTC](https://forum.inductiveautomation.com/t/help-on-timing-on-initial-window-populate/47798/9 "2021-06-16T12:10:10Z")

</div>

The values are just meant to be initial values from my understanding, so a binding isn’t appropriate in this case (imo). A bit like setting the “enter text here” value of an input field

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 16, 2021, 12:12pm UTC](https://forum.inductiveautomation.com/t/help-on-timing-on-initial-window-populate/47798/10 "2021-06-16T12:12:48Z")

</div>

I would use a single dataset property in the root container with a non-polling named query binding to obtain the relevant row from the DB. Then each field would use an expression binding to pick out the correct column value from the dataset. Wrap each of those in a `try()` so any initial nulls are ignored.

No scripting required. Bindings will all fire shortly after window load when the dataset is populated from the query binding (which itself fires with the passed-in tag parameter). The bindings won’t fire again, leaving the fields alone for user editing.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [June 16, 2021, 12:14pm UTC](https://forum.inductiveautomation.com/t/help-on-timing-on-initial-window-populate/47798/11 "2021-06-16T12:14:20Z")

</div>

Ok Phil, you win this time (and every other time, let’s be honest)

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [June 16, 2021, 1:17pm UTC](https://forum.inductiveautomation.com/t/help-on-timing-on-initial-window-populate/47798/12 "2021-06-16T13:17:48Z")

</div>

Hey @pturmel copied my answer! (should I feel honored? or perhaps I learned it from him previously?)

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 16, 2021, 1:53pm UTC](https://forum.inductiveautomation.com/t/help-on-timing-on-initial-window-populate/47798/13 "2021-06-16T13:53:32Z")

</div>

> [@bkarabinchak.psi](#):
>
> copied my answer!

Heh. Missed it. Didn't re-read the whole topic. Your more complete recommendation has the advantage that you can compare each entry field's current value property to the corresponding initial value property to derive an "edited" status, which can then drive styling to show what has changed. I've been doing that where appropriate for ages, so you _might_ have learned it from me.
