Docs menu

Canvas apps

In a canvas app, bind Time Use’s Events to your events table, filtered to the range your app chooses, and save corrections with Patch in OnRecode. The control never writes to Dataverse itself in a canvas app.

Before you start

  • An admin has allowed code components in the environment (see Install).
  • The Ninth Forge Time Use solution is imported, version 0.8.28 or later. Earlier versions show no events in canvas apps, or can’t save corrections there.
  • In your app, go to Settings, General and set Data row limit to 2000. A canvas app hands the control at most that many rows, so filter Events to a range that stays under it (one day shift of a 12-machine fleet is about 900 rows).

Add the control

  1. Go to Insert, Get more components, Code, select Ninth Forge Time Use, Import, then insert it from Code components.
  2. Set its X and Y to 0, Width to Parent.Width and Height to Parent.Height.
  3. Add your events table under Data.

Choose the range

Your app owns the range: keep it in two variables, give them to the control, and load only those events. In App.OnStart (or wherever the user picks a shift):

Set(varFrom, DateAdd(Today(), 6, TimeUnit.Hours));
Set(varTo, DateAdd(Today(), 18, TimeUnit.Hours))

On the control, set Time range to Custom range start and end, Custom range start to varFrom and Custom range end to varTo.

Bind the events

Set Events (Items) to your table, filtered to the range:

AddColumns(
    Filter(
        'Machine Events',
        Start <= varTo,
        Start >= DateAdd(varFrom, -12, TimeUnit.Hours)
    ),
    MachineName, Machine.Name,
    StateCode, 'State code',
    EndAt, End,
    EventId, 'Machine Event'
)
  • The 12 hours before the range catch an event that started earlier and runs into it.
  • MachineName is there because a lookup column can’t be mapped to a code component: map the machine’s name instead.
  • StateCode and EndAt are there because a canvas app only fetches the columns a formula names. Name every column you map; columns you don’t name arrive empty.
  • EventId is each row’s Dataverse id (the table’s own id column, named after the table). A canvas app numbers the rows it hands a code component, so the control reads this column to tell OnRecode which record to save.

Then, in the properties pane, select Fields, Edit, Add field and add MachineName. On the Advanced tab, map each column by its name in quotes:

Property Value
eventAsset (Machine) "MachineName"
eventStart (Start) your start column, for example "nf_start"
eventEnd (End) your end column
eventStateCode (State code) your state-code column

Times show in the time zone of the user’s browser.

If the control says how many rows it couldn’t read, the message names the column each mapping points to. …, Copy diagnostics shows the first row as the control received it, and Rows held back by the host says whether the data row limit cut the range short.

Save corrections

Leave Save mode on Auto (or set it to Raise OnRecode). When a user re-codes an event or moves a boundary, the control raises OnRecode with the change in Self.lastRecode. Save it there:

With(
    { c: ParseJSON(Self.lastRecode) },
    With(
        { id: GUID(Text(c.id)), newCode: Text(c.state) },
        If(
            Text(c.kind) = "state",
            Patch('Machine Events', LookUp('Machine Events', 'Machine Event' = id), {
                State: LookUp('State Codes', Code = newCode),
                'State code': newCode,
                Source: 'Source (Machine Events)'.Corrected
            }),
            // A boundary: the next event's start, then this event's end (if it has one).
            With(
                { nextId: GUID(Text(c.nextId)), nextStart: DateTimeValue(Text(c.nextStart)) },
                Patch('Machine Events', LookUp('Machine Events', 'Machine Event' = nextId), { Start: nextStart })
            );
            If(
                !IsBlank(c.end),
                With(
                    { endAt: DateTimeValue(Text(c.end)) },
                    Patch('Machine Events', LookUp('Machine Events', 'Machine Event' = id), { End: endAt })
                )
            );
            Refresh('Machine Events')
        )
    )
)
  • Work out the ids and values in With first, as above. A LookUp whose condition reads the JSON directly isn’t sent to Dataverse: it only searches the rows already in the app, finds nothing past the data row limit, and Patch then quietly saves nothing.
  • Refresh after a boundary move: a canvas app re-reads the rows after the first Patch but not the second, so without it the control can’t see the whole move and puts it back.
  • The times in lastRecode are ISO instants in UTC, and DateTimeValue reads them correctly.

The control shows the change at once and waits up to 10 seconds for your data to show it. If it doesn’t, the change is put back with a message.

Outputs

  • selectedEventId and selection: the chosen block, for cross-filtering. OnBlockSelect fires. The id is the Dataverse id from EventId.
  • rangeStart and rangeEnd: the range shown, set as soon as the control loads.
  • mode, focus and lastRecode.

In a custom page

A custom page in a model-driven app is a canvas page, so everything on this page applies to it. In the app designer, choose Add page, Custom page, Create a new custom page, then add the control, bind Events and map the columns as above. Save and publish the page, then publish the app.

The editor in a canvas app

Set Display to Editor to let a supervisor review one shift: suggested codes for uncoded and no-data time, re-coding, moving boundaries, a change log and Approve shift. In a model-driven app the editor saves straight to Dataverse. In a canvas app it hands each change to your app, which saves it to the editor’s two tables. They come with the solution: Time Use Changes and Time Use Shift Approvals.

  1. Add Time Use Changes, Time Use Shift Approvals and your machines table under Data, next to your events table.

  2. Set Display to Editor and Site to your site’s name, for example "North Range". Approvals and the change log are kept per site and shift.

  3. The editor picks the shift (‹ › move between shifts), so let it set your range. In OnRangeChange:

    Set(varFrom, Self.rangeStart); Set(varTo, Self.rangeEnd)

    Events stays filtered on varFrom and varTo as in Bind the events.

  4. Give the editor the saved state. Set Shift approval status to the site’s approval rows and Change log (JSON) to its change rows. The control picks the rows for the shift on screen and sorts the log newest first:

    // Shift approval status
    JSON(ForAll(Filter('Time Use Shift Approvals', Site = "North Range"),
        { date: 'Shift date', shift: Shift, status: Text('Approval status') }))
    
    // Change log (JSON)
    JSON(ForAll(Filter('Time Use Changes', Site = "North Range"), {
        kind: Text(Kind), source: Text(Source), asset: Machine, reason: Reason, by: By, at: At,
        eventId: 'Event (id)', nextId: 'Next event (id, boundary moves)',
        original: { state: 'Original state code', start: 'Original start', end: 'Original end' },
        next: { state: 'New state code', start: 'New start', end: 'New end' },
        date: 'Shift date', shift: Shift }))

    For a site with a long history, filter on Shift date too (for example the last 14 days).

  5. Save each change in OnEditorChange. Self.editorChange holds one change: kind is recode, boundary, fill, approve or reopen; original and next hold state, start and end (ISO instants); source is Manual or Suggestion; shift is { site, date, shift }. This formula saves the event (or the approval), then a change-log row:

    With({ c: ParseJSON(Self.editorChange) },
    With({ kind: Text(c.kind), site: Text(c.shift.site), shiftDate: DateValue(Text(c.shift.date)),
           shiftName: Text(c.shift.shift),
           newEnd: If(IsBlank(c.next.end), Blank(), DateTimeValue(Text(c.next.end))) },
        Switch(kind,
            "recode", With({ id: GUID(Text(c.eventId)), newCode: Text(c.next.state) },
                Patch('Machine Events', LookUp('Machine Events', 'Machine Event' = id), {
                    State: LookUp('State Codes', Code = newCode), 'State code': newCode,
                    Source: 'Source (Machine Events)'.Corrected })),
            "boundary", With({ id: GUID(Text(c.eventId)), nextId: GUID(Text(c.nextId)) },
                Patch('Machine Events', LookUp('Machine Events', 'Machine Event' = nextId), { Start: newEnd });
                Patch('Machine Events', LookUp('Machine Events', 'Machine Event' = id), { End: newEnd })),
            "fill", With({ machine: Text(c.asset), newCode: Text(c.next.state),
                           newStart: DateTimeValue(Text(c.next.start)) },
                Patch('Machine Events', Defaults('Machine Events'), {
                    Name: machine & " " & newCode, Machine: LookUp(Machines, Name = machine),
                    Start: newStart, End: newEnd,
                    State: LookUp('State Codes', Code = newCode), 'State code': newCode,
                    Source: 'Source (Machine Events)'.Manual })),
            // approve or reopen: the shift's approval row
            Patch('Time Use Shift Approvals',
                Coalesce(LookUp('Time Use Shift Approvals',
                    Site = site && 'Shift date' = shiftDate && Shift = shiftName),
                    Defaults('Time Use Shift Approvals')), {
                Name: site & " " & Text(shiftDate, "yyyy-mm-dd") & " " & shiftName,
                Site: site, 'Shift date': shiftDate, Shift: shiftName,
                'Approval status': If(kind = "approve",
                    'Approval status (Time Use Shift Approvals)'.Approved,
                    'Approval status (Time Use Shift Approvals)'.Reopened),
                By: User().FullName, At: Now() })
        );
        Patch('Time Use Changes', Defaults('Time Use Changes'), {
            Name: Coalesce(Text(c.asset), site) & " " & kind,
            Kind: Switch(kind, "recode", 'Kind (Time Use Changes)'.'Re-code',
                "boundary", 'Kind (Time Use Changes)'.Boundary, "fill", 'Kind (Time Use Changes)'.'Gap fill',
                "approve", 'Kind (Time Use Changes)'.Approve, 'Kind (Time Use Changes)'.Reopen),
            'Event (id)': Text(c.eventId), 'Next event (id, boundary moves)': Text(c.nextId),
            Machine: Text(c.asset), Reason: Text(c.reason),
            'Original state code': Text(c.original.state),
            'Original start': If(IsBlank(c.original.start), Blank(), DateTimeValue(Text(c.original.start))),
            'Original end': If(IsBlank(c.original.end), Blank(), DateTimeValue(Text(c.original.end))),
            'New state code': Text(c.next.state),
            'New start': If(IsBlank(c.next.start), Blank(), DateTimeValue(Text(c.next.start))),
            'New end': newEnd,
            Source: If(Text(c.source) = "Suggestion", 'Source (Time Use Changes)'.Suggestion,
                'Source (Time Use Changes)'.Manual),
            By: User().FullName, At: Now(), Site: site, 'Shift date': shiftDate, Shift: shiftName });
        Refresh('Machine Events'); Refresh('Time Use Changes'); Refresh('Time Use Shift Approvals')))
    • Replace 'Machine Events', 'Machine Event', 'State Codes', Machines and the column names with yours, as in Save corrections.
    • A change after approval arrives as two changes, reopen and then the change itself, each in its own OnEditorChange.
    • Self.pendingChanges lists the event changes your data doesn’t show yet, oldest first, if you’d rather save them in a batch.

Everything else behaves the same in both kinds of app: suggestions are only ever offered, never applied on their own; approving locks the shift for reporting; any change after that reopens it and writes a log row.

Last updated