# Job to automatically upsert Tracked Entity Instances in DHIS2 returning undefined values

**URL:** <https://community.openfn.org/t/job-to-automatically-upsert-tracked-entity-instances-in-dhis2-returning-undefined-values/87>\
**Category:** Job Writing\
**Created:** [March 19, 2021, 8:14am UTC](https://community.openfn.org/t/job-to-automatically-upsert-tracked-entity-instances-in-dhis2-returning-undefined-values/87 "2021-03-19T08:14:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aleksa-krolls](https://community.openfn.org/user_avatar/community.openfn.org/aleksa-krolls/32/156_2.png) [@aleksa-krolls](https://community.openfn.org/u/aleksa-krolls)\
**Post date:** [March 19, 2021, 8:14am UTC](https://community.openfn.org/t/job-to-automatically-upsert-tracked-entity-instances-in-dhis2-returning-undefined-values/87/1 "2021-03-19T08:14:46Z")

</div>

I’m trying to upload CSV data to DHIS2 to update Tracked Entity Instances. I’m using the `upsertTEI(...)` helper function from the OpenFn [DHIS2 adaptor](https://github.com/OpenFn/language-dhis2) to upsert TEIs via a unique identifier (check if existing, update/insert accordingly).

```js
each(
  dataPath("csvData[*]"),
  upsertTEI(
    'SgQW3vpnhuL', //unique custom attribute in dhis2
    {
      trackedEntityType: 'bsDL4dvl2ni',
      orgUnit: dataValue('OrgUnit'),
      attributes: [
        {
          attribute: 'SgQW3vpnhuL',
          value: dataValue('SgQW3vpnhuL'),
        },
        {
          attribute: 'MxQPuS9G7hh',
          value: dataValue('MxQPuS9G7hh'),
        },
      ],
    },
    { strict: false }
  ))

```

When I run this job, all values return undefined.

```auto
Checking if a record exists that matches this filter: e[32mattribute{ id: SgQW3vpnhuL, value: undefined }e[0m ...
URLSearchParams { 'ou' => 'undefined', 'filter' => 'SgQW3vpnhuL:EQ:undefined' }
undefined
undefined
Usinge[33m lateste[0m version of DHIS2 api.

```

@chaiwa any ideas? Am I maybe missing something in my job expression? I

---

<div class="post-metadata">

**Author:** ![chaiwa](https://community.openfn.org/user_avatar/community.openfn.org/chaiwa/32/33_2.png) [@chaiwa](https://community.openfn.org/u/chaiwa)\
**Post date:** [March 19, 2021, 8:22am UTC](https://community.openfn.org/t/job-to-automatically-upsert-tracked-entity-instances-in-dhis2-returning-undefined-values/87/2 "2021-03-19T08:22:14Z")

</div>

@aleksa-krolls It seems `dataValue` is not resolving correctly. Would you also share the sample state?

---

<div class="post-metadata">

**Author:** ![aleksa-krolls](https://community.openfn.org/user_avatar/community.openfn.org/aleksa-krolls/32/156_2.png) [@aleksa-krolls](https://community.openfn.org/u/aleksa-krolls)\
**Post date:** [March 19, 2021, 8:25am UTC](https://community.openfn.org/t/job-to-automatically-upsert-tracked-entity-instances-in-dhis2-returning-undefined-values/87/3 "2021-03-19T08:25:16Z")

</div>

@chaiwa sure! Here’s the Message body for a CSV row I’m trying to upload to update a TEI’s `Geographical Scope` (custom attribute)…

```auto
{
  "csvData": [
   {
      "MxQPuS9G7hh": "Regional",
      "OrgUnit": "xcs1ZRVThAV",
      "SgQW3vpnhuL": "005899TGO"
    },
    {
      "MxQPuS9G7hh": "National",
      "OrgUnit": "ijEoEQbfYXO",
      "SgQW3vpnhuL": "005532TST"
    }
 ]
}

```

---

<div class="post-metadata">

**Author:** ![chaiwa](https://community.openfn.org/user_avatar/community.openfn.org/chaiwa/32/33_2.png) [@chaiwa](https://community.openfn.org/u/chaiwa)\
**Post date:** [March 19, 2021, 8:36am UTC](https://community.openfn.org/t/job-to-automatically-upsert-tracked-entity-instances-in-dhis2-returning-undefined-values/87/4 "2021-03-19T08:36:09Z")

</div>

> [@aleksa-krolls](#):
>
> `csvData`

@aleksa-krolls What happens if you set **dataValue** as `dataValue(csvData[state.index]['OrgUnit'])`?

---

<div class="post-metadata">

**Author:** ![chaiwa](https://community.openfn.org/user_avatar/community.openfn.org/chaiwa/32/33_2.png) [@chaiwa](https://community.openfn.org/u/chaiwa)\
**Post date:** [March 19, 2021, 8:48am UTC](https://community.openfn.org/t/job-to-automatically-upsert-tracked-entity-instances-in-dhis2-returning-undefined-values/87/5 "2021-03-19T08:48:40Z")

</div>

@aleksa-krolls Or simply `csvData[state.index]['OrgUnit']`, considering how `each` works.

---

<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:** [March 25, 2021, 6:41am UTC](https://community.openfn.org/t/job-to-automatically-upsert-tracked-entity-instances-in-dhis2-returning-undefined-values/87/6 "2021-03-25T06:41:24Z")

</div>

Hey @aleksa-krolls , @chaiwa , I know that language-dhis2#v2.0.4 has now been released to [openfn.org](http://openfn.org); does this solve the issue?

---

<div class="post-metadata">

**Author:** ![aleksa-krolls](https://community.openfn.org/user_avatar/community.openfn.org/aleksa-krolls/32/156_2.png) [@aleksa-krolls](https://community.openfn.org/u/aleksa-krolls)\
**Post date:** [March 29, 2021, 5:50pm UTC](https://community.openfn.org/t/job-to-automatically-upsert-tracked-entity-instances-in-dhis2-returning-undefined-values/87/7 "2021-03-29T17:50:46Z")

</div>

@taylordowns2000 @chaiwa I’m not sure. There is nothing showing in the OpenFn logs, but the run is successful. How can I see the DHIS2 import summary? See example run:  
[https://www.openfn.org/projects/p5qam9/runs/r7xd4kn6](https://www.openfn.org/projects/p5qam9/runs/r7xd4kn6)

---

<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:** [March 30, 2021, 10:05am UTC](https://community.openfn.org/t/job-to-automatically-upsert-tracked-entity-instances-in-dhis2-returning-undefined-values/87/8 "2021-03-30T10:05:13Z")

</div>

It looks like language-dhis2 does _not_ error out on empty arrays, which can be confusing but is important for longer, chained job expression. In this case, since the array provided for `upsertTEI` was empty and no other operations were performed, the run finished in something like 300ms! Given array items in there, you’ll see the expected logs.
