Javascript: Problem getting date out of data repository

Hi

I am not a programmer, but have tried to make some code to update the data repository with a deletion code, if the records are modified a certain amount of days ago. However, I cannot get it to work, and it seems like my problem is to get the “DateM” Modified date out of the repository and into my variable. In the repository the “DateM” is in a lighter gray, if that is a hint to why.

Hope someone can help guiding me in the right direction.

This is the code

/Repository variables/

var Repo = new ActiveXObject(‘RepositoryLib.WorkflowRepository’);

//var PrintJob = Repo.GetKeySets(“olfw_files”, ‘[“jobid”, “stateid”, “marked_delete”, “DateM”]’);

var PrintJob = Repo.GetKeySets(“olfw_files”, ‘*’);

/date now/

var dateNow = new Date();

var month = dateNow.getUTCMonth() + 1; //months from 1-12

var day = dateNow.getUTCDate();

var year = dateNow.getUTCFullYear();

newDate = year + “/” + month + “/” + day;

// only run if data repository group is not empty.

if (PrintJob.length != 0) {

    for (var x=0; x < PrintJob.length; x++) {

        /*modifyed date*/

        //var modifyedDate = new Date((PrintJob[x].DateM).replace('-', '/'));

        var modifyedDate = new Date((PrintJob[x].DateM));

        //var test1 = PrintJob[x].DateM; //test

        /*Date dif*/

        var TimeDif = Math.abs(newDate - modifyedDate);

        var DaysDif = Math.ceil(TimeDif / (1000 * 60 * 60 * 24));

        var JobID = PrintJob[x].jobid;

        Watch.log('NewDate: ' + newDate, 3);

        Watch.log('ModifyDate: ' + modifyedDate, 3);

        Watch.log('Dagif: ' + DaysDif, 3);

        Watch.log('Jobid: ' + JobID, 3);

        //watch.log('ModifyDate: ' + test1, 3); //test

        if (PrintJob[x].stateid == 30 && DaysDif >= 14) {

            Repo.SetValueByID("olfw_files", "marked_delete", "1", JobID);

        } else if (PrintJob[x].stateid == 40 && DaysDif >= 7) {

            Repo.SetValueByID("olfw_files", "marked_delete", "1", JobID);

        }

    }

}

Best regards
Thomas

Maybe my problem is the array where i try to get data out of the repository, because the log returns this all data from the repository.

13:56:57.893 [0009] NewDate: 2022/6/30
13:56:57.893 [0009] ModifyDate: NaN
13:56:57.893 [0009] Dagif: NaN
13:56:57.893 [0009] Jobid: undefined

Hi @ThomasAagaard,

Can you let us know please which information will be logged when you add the following JavaScript code to the for-loop “for (var x=0; x < PrintJob.length; x++) { /* ... */ }”:

Watch.Log(("PrintJob[x]: \"" + JSON.stringify(PrintJob[x]) + "\""), 3);
Watch.Log(("PrintJob[x].DateM: \"" + PrintJob[x].DateM + "\""), 3);
Watch.log(("PrintJob[x].jobid: \"" + PrintJob[x].jobid + "\""), 3);

The date stored in the repository is a string, so you need to parse it in order to convert it to an actual Date object.
Inside your loop, do something like this:

  var myDate = PrintJob[x].DateM.slice(0,10);
  var y = (myDate.slice(0,4)*1);
  var m = ((myDate.slice(5,7)*1)-1);
  var d = (myDate.slice(8,10)*1)
  var modifyedDate = new Date(y,m,d);

Hi Marten

Hope I’ve added it the right way, I also added an x<10 because I made log file at 1GB yesterday, hope I can limit test records that way.

for (var x=0; x < PrintJob.length && x< 100; x++) { /* … */ } {

This is what was returned
INFO : 07:36:31.455 [0010] Run embedded script…
INFO : 07:36:31.977 [0010] PrintJob: “”"“”
INFO : 07:36:31.977 [0010] PrintJob.DateM: “undefined”
INFO : 07:36:31.977 [0010] PrintJob.jobid: “undefined”
INFO : 07:36:31.978 [0010] Data file processed : job010DOK7OFDUAHEA75676D64.dat, size: 2 bytes
INFO : 07:36:31.978 [0010] Plugin Open Script completed successfully - 07:36:31 (elapsed time: 00:00:00.526)

Hi Phil

I get this error

ERROR: 07:45:38.948 [0010] W3602 : Error 0 on line 30, column 13: Microsoft JScript runtime error: ‘PrintJob[…].DateM’ is null or not an object
ERROR: 07:45:38.949 [0010] W3603 : Error running script.
ERROR: 07:45:38.949 [0010] Open Script: W1603 : Plugin failed - 07:45:38 (elapsed time: 00:00:00.522)

On this line

var myDate = PrintJob.DateM.slice(0,10);

Maybe it’s the code where I try to get the data out that is faulty

/Repository variables/

var Repo = new ActiveXObject(‘RepositoryLib.WorkflowRepository’);
//var PrintJob = Repo.GetKeySets(“olfw_files”, ‘[“jobid”, “stateid”, “marked_delete”, “DateM”]’);
var PrintJob = Repo.GetKeySets(“olfw_files”, ‘*’);

Yep, that’s definitely the problem, sorry I didn’t spot it earlier.

The GetKeySets method returns a string. You have to parse it into an object to be able to use it for your loop

var Repo = new ActiveXObject("RepositoryLib.WorkflowRepository");
var keysets = Repo.GetKeySets("olfw_files", '["jobid", "stateid", "marked_delete", "DateM"]');
var PrintJob = JSON.parse(keysets);

Here’s the complete script I used:

var Repo = new ActiveXObject("RepositoryLib.WorkflowRepository");
var PrintJob = JSON.parse(Repo.GetKeySets("olfw_files", "*"));
var newDate = new Date();

for (var x=0; x < PrintJob.length; x++) {
  var myDate = PrintJob[x].DateM.slice(0,10);
  var y = (myDate.slice(0,4)*1);
  var m = ((myDate.slice(5,7)*1)-1);
  var d = (myDate.slice(8,10)*1)
  var modifyedDate = new Date(y,m,d);
  
  var TimeDif = Math.abs(newDate - modifyedDate);
  var DaysDif = Math.ceil(TimeDif / (1000 * 60 * 60 * 24));
  
  Watch.log('NewDate: ' + newDate, 3);
  Watch.log('ModifyDate: ' + modifyedDate, 3);
  Watch.log('Dagif: ' + DaysDif, 3);
}
1 Like

Hi Phil

Thank you for the help, I got the date/array part to work now.

Best regards
Thomas

1 Like