# Using "dataValue('path')" inside a custom function

**URL:** <https://community.openfn.org/t/using-datavalue-path-inside-a-custom-function/200>\
**Category:** Job Writing\
**Created:** [April 7, 2022, 6:49am UTC](https://community.openfn.org/t/using-datavalue-path-inside-a-custom-function/200 "2022-04-07T06:49:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylordowns2000](https://community.openfn.org/user_avatar/community.openfn.org/taylordowns2000/32/165_2.png) [@taylordowns2000](https://community.openfn.org/u/taylordowns2000)\
**Post date:** [April 7, 2022, 6:49am UTC](https://community.openfn.org/t/using-datavalue-path-inside-a-custom-function/200/1 "2022-04-07T06:49:48Z")

</div>

@Levi , I’m sorry that I missed [this pull request](https://github.com/sid-indonesia/OpenFn-antenatal-care/pull/2) (please keep @amber CCed on this forum if you have questions!) but I’m glad you merged it.

Check out how [`sourceValue`](https://github.com/OpenFn/language-common/blob/main/src/Adaptor.js#L58-L73) and [`dataValue`](https://github.com/OpenFn/language-common/blob/main/src/Adaptor.js#L108-L121) work in language-common. There are some interesting differences between the two (_namely_ that `dataValue` is a shortcut to `$.data.yourPath` (since that’s such a common place to select values) and `sourceValue` is just `$.yourPath`.

## Big important thing

But the big important thing is that these are both _functions_! OpenFn does do a little bit of magic 🪄 to make all these asynchronous bits (like HTTP requests) behave like a big, predictable, synchronous pipeline. One bit of magic is that each operation in a job will be called _after_ the previous operation completes. Another bit of magic is that each function used inside an operation **(except for a custom function that takes state, like an `fn(input => output)` block)** will be called _when that operation runs_ thanks to [**expandReferences**](https://github.com/OpenFn/language-common/blob/9495c8d48f8487a81ac4f588ce7219a0bfc5921f/src/Adaptor.js#L298-L325), rather than right at the beginning of the job… this is super important because it means that if you write

```js
create('patient', { name: dataValue('full_name') })
create('visit', { patientId: dataValue('response.id') })

```

that second operation will be evaluating `state.data.response.id` _after_ the first operation is finished. In other words, `state.data.full_name` is from the initial state, but then `state.data.response.id` is from the state _when that second operation gets run_. In my example, I’m assuming that the first operation gets a response from some FHIR server with a new patient ID that we can use in later operations.

Now, imagine that you wrote:

```js
create('patient', { name: state.data.full_name })
create('visit', { patientId: state.data.response.id })

```

Since neither `state.data.full_name` _nor_ `state.data.response.id` are _ **functions** _, they’ll be evaluated right at the start of the job’s execution… so that `patientId` we want to use in the second operation will be blank! 🤦

Note that `dataValue('response.id')` will work in there, and _so will_ `state => state.data.response.id` since both are _FUNCTIONS_ which can be called with `state`.

Finally… getting back to your pull request: if you write:

```js
fn(state => {
  console.log(
    dataValue('path')
  )
})

```

You’ll see that you’re logging a function… not _what that function returns_ when called with state! As you point out, when you’re inside that custom function (either inside your own `state => { whatever }` function or inside a `fn(state => { whatever })` operation, if you want to see the result of calling dataValue with state, you need to do it yourself… we don’t “mess” with your code inside those custom Javascript playgrounds:

```js
fn(state => {
  console.log(
    dataValue('path')(state) // I call dataValue('path') with state myself!
  )
})

```

A final thought… since you’re already inside a custom function that takes state when you create those FHIR resources in your `fn(state => blah)` block, you might prefer accessing `state.data.path` directly… since it’s already inside a custom function, you don’t have to worry about it being evaluated right at the start of the job… it will wait to be called until the previous operation has finished. You might then write:

```js
// Build "Patient" resource
fn(state => {
  // take the body of the triggering message and call it "input", for example:
  const input = state.data 
  // then use `input['x']` everywhere else...

  const patient = {
    fullUrl: 'urn:uuid:0fc374a1-a226-4552-9683-55dd510e67c9', // will be referenced in many `Observation` resources
    request: {
      method: 'PUT',
      url: `Patient?identifier=https://fhir.kemkes.go.id/id/nik|${input['patient_ID/patient_identifier_NIK'].replace(/ /g, "_")}`
    },
    resource: {
      resourceType: 'Patient',
      identifier: [
        {
          use: 'usual',
          system: 'https://fhir.kemkes.go.id/id/nik',
          value: input['patient_ID/patient_identifier_NIK'].replace(/ /g, "_"),
        },
      ],
      name: [
        {
          use: 'official',
          text: input['patient_ID/patient_name'],
          text: input['patient_ID/patient_name'],
        },
      ],
  })...

```

I hope this provides some helpful context.

Do you think you’re ready to start working on the FHIR adaptor? I’d love to take these custom functions that you’ve written in your job and abstract them into helpers provided by language-fhir so that all of this structure is provided out of the box to others building FHIR resources and requests!

---

<div class="post-metadata">

**Author:** ![Levi](https://community.openfn.org/letter_avatar_proxy/v4/letter/l/ce73a5/32.png) [@Levi](https://community.openfn.org/u/Levi)\
**Post date:** [April 8, 2022, 7:55am UTC](https://community.openfn.org/t/using-datavalue-path-inside-a-custom-function/200/2 "2022-04-08T07:55:12Z")

</div>

@taylordowns2000 Thank you so much for the very clear explanation! 😸

> [@taylordowns2000](#):
>
> Do you think you’re ready to start working on the FHIR adaptor? I’d love to take these custom functions that you’ve written in your job and abstract them into helpers provided by language-fhir so that all of this structure is provided out of the box to others building FHIR resources and requests!

Sure. I’d help with all I got so that every “not yet FHIR-compliant” data in the world can be transformed into FHIR-compliant data and achieve interoperability with low cost, thus improving the quality of care given to whoever in need.
