Loading data from a ledger

Loading data from a ledger

Why?

  • To read data previously saved into a ledger

Here is the calculate expression:

coalesce(instance('ledgerdb')/ledgerdb/ledger[@entity-id=case_id]/section[@section-id='balance']/entry[@id=current()/../@id], 0)

instance('commcaresession')/session/data/case_id

Let’s break this down:

  • coalesce([big scary ledger reference], 0)

  •  

    • just says use the leger reference, and if you don’t find a value, use 0.

    •  

      • This is in case it is the first time filling in a form that touches that value.

Looking at the ledger reference

  • instance('ledgerdb')/ledgerdb/ledger[@entity-id=case_id]/section[@section-id='balance']/entry[@id=current()/../@id]

  •  

    • case_id: put here the id of the case where you want to get your ledger from. For example instance('commcaresession')/session/data/case_id = the current case ID.

  • So instance('ledgerdb')/ledgerdb/ledger[@entity-id=instance('commcaresession')/session/data/case_id]

  •  

    • Says grab the ledger for this case

  • /section[@section-id='balance']

  •  

    • Says grab the section called “balance” (this corresponds to the “balance ID”). You could use here 'stock', 'consumption' ...

  • /entry[@id=current()/../@id]

  •  

    • Says grab the entry value with the ID of the current product

This is pretty scary expression. Feel free to ask for help with it, though it shouldn’t vary too much per form.