← Back to team overview

dhis2-users team mailing list archive

Re: Script to save datavalues


Hey Joao,

Sections 1.10.1 and 1.10.2 contain the manual on how to send data values either, single or multiple values.

It contains some example JSON structures on how to build the payload to save those values. From what i understand you want to save the previous values to the current period.
That sounds like you only want to change the date and the rest stays the same?

In that case you could modify the structure that you got and POST it back to the api.

A simple sample script that would be the basics of what you would need could look something like the one below.

Hope that helps.

jQuery(function () {

  //The data you wish to save (example taken from the documentation)
  var data = {
    "dataValues": [
      { "dataElement": "f7n9E0hX8qk", "period": "201401", "orgUnit": "DiszpKrYNg8", "value": "12" },
      { "dataElement": "f7n9E0hX8qk", "period": "201401", "orgUnit": "FNnj3jKGS7i", "value": "14" },
      { "dataElement": "f7n9E0hX8qk", "period": "201402", "orgUnit": "DiszpKrYNg8", "value": "16" },
      { "dataElement": "f7n9E0hX8qk", "period": "201402", "orgUnit": "Jkhdsf8sdf4", "value": "18" }

  //Excecute the ajax request to the api
    //[1] Set preheadCache to false because it is a small import. (See documentation Table 1.16. Import parameters)
    //[2] The url should be pointing to the api, if your instance does not run at the root of your server
    //    you need to correct this
    url: '/dhis/api/dataValueSets?preheatCache=false',
    type: 'POST',
    data: JSON.stringify(data),
    processData: false,
    contentType: "application/json; charset=UTF-8",
      .then(function (response) {
        //[3] The server will return a different state than SUCCESS when the import failed
        if (response.status !== 'SUCCESS') {
          console.log('Import failed');

        //[4] Records can be ignored and the response will still be SUCCESS so we need to check that
        //    the length of the imported items is equal to the values we sent.
        if (response.importCount.imported === data.dataValues.length) {
          console.log('Imported all values');
        } else {
          console.log('Handle partial import here')

      .fail(function () {
        console.log('Request failed');


Kind regards,

Mark Polak
+47 970 36 752

On 06 May 2015, at 12:17, Joao Mazuze <Joao_Mazuze@xxxxxxxxxxxx> wrote:

> Hi,
> Can someone help me on how to save datavalues using a script, that is because I usually pull some datavalues from previous period to the data entry form, now I need to save this data in the current period, dhis2 is not saving them automatically. How do I perform it using a script?
> Kind regards,
> Joao
> This message may contain privileged and confidential information intended solely for the addressee. Please do not read, disseminate or copy it unless you are the intended recipient. If this message has been received in error, we kindly ask that you notify the sender immediately by return email and delete all copies of the message from your system. _______________________________________________
> Mailing list: https://launchpad.net/~dhis2-users
> Post to     : dhis2-users@xxxxxxxxxxxxxxxxxxx
> Unsubscribe : https://launchpad.net/~dhis2-users
> More help   : https://help.launchpad.net/ListHelp