OL Learn

Set date in action step not working

Hi,
Trying to reassign value to a detial date field in an action step after mapping.
I am mapping a field that occationnally is blank. I can default it to some value as follow.

Then in the action I loop through the detail records to set the defaulted date field to the value of the previous detail record but the assignement is not working I tried multiple type of assignement and nothing is working.

d = new Date('1900/01/01');
d2 = new Date('1900/01/02');
PrevDate = new Date();
cd = new Date();

for (i in record.tables['detail']) {

	cd = record.tables.detail[i].fields.Request_Date;
	logger.info(i);
	logger.info('PrevDate=' + PrevDate);
	logger.info('ReqDate=' + cd);
	logger.info('d=' + d);
	
	if (cd.getFullYear() === d.getFullYear() && cd.getMonth() === d.getMonth() && cd.getDate() === d.getDate()	) {
	        logger.info('d2=' + d2);	
//	        d2.setFullYear(PrevDate.getFullYear(), PrevDate.getMonth(), PrevDate.getDate());
	        d2 = PrevDate;
	        logger.info('d2=' + d2);
			record.tables.detail[i].fields.Request_Date = d2;
			logger.info('in 1900');
			logger.info(record.tables.detail[i].fields.Request_Date);
	} else {
			PrevDate = record.tables.detail[i].fields.Request_Date;
			logger.info(PrevDate);
	} 	
} 

log output for last step
image

Hi @PascalGervais,

Can you also please share a screenshot of the Date Format settings you are using? These settings can be found in the Step Properties (panel) of your Extraction Step or – in case the option Automatic is selected – via: Window > Preferences… > (Preferences window) > DataMapper > Default Format

Here you go

Also tried with setFullYear and does not work.
Have not problems with string fields.

d = new Date(‘1900/01/01’);
d2 = new Date(‘1900/01/02’);
PrevDate = new Date();
cd = new Date();

for (i in record.tables[‘detail’]) {

cd = record.tables.detail[i].fields.Request_Date;
det = record.tables.detail[i]
logger.info(i);
logger.info('PrevDate=' + PrevDate);
logger.info('ReqDate=' + det.fields.Request_Date);
logger.info('d=' + d);

if (cd.getFullYear() === d.getFullYear() && cd.getMonth() === d.getMonth() && cd.getDate() === d.getDate()	) {
        logger.info('d2=' + d2);	

// d2.setFullYear(PrevDate.getFullYear(), PrevDate.getMonth(), PrevDate.getDate());
d2 = PrevDate;
logger.info(‘d2=’ + d2);
// record.tables.detail[i].fields.Request_Date = PrevDate.getFullYear() + ‘-’ + (“0” + (PrevDate.getMonth() + 1)).slice(-2) + ‘-’ + PrevDate.getDate();
record.tables.detail[i].fields.Request_Date.setFullYear(PrevDate.getFullYear(), PrevDate.getMonth(), PrevDate.getDate());
record.tables.detail[i].fields.SKU = 'Testing’
logger.info(‘in 1900’);
logger.info(record.tables.detail[i].fields.Request_Date);
} else {
PrevDate = record.tables.detail[i].fields.Request_Date;
logger.info(PrevDate);
}
}

  • Create a Record property named lastDate, of type Date, and initialize it to null;
  • In your Repeat loop, start with a condition to see if a date is present.
  • In the True branch:
    • Extract your Date field as usual. Set the Date/Time format to Automatic.
    • Add an Action step that sets the lastDate property to the same extracted value.
  • In the False branch:
    • Add an Extract step for the Date field, set it to JavaScript with the following code:

      sourceRecord.properties.lastDate || new Date();
      

This will use the last date you extracted as the date for any line item that doesn’t have a date. In case the very first item does not have a date, then the date will be set to today’s date.

The attached sample config demonstrates how to do this.
LastDate.OL-datamapper (4.7 KB)

Thanks, I understand the principale and will try.
Unfortunatly the config file attached is giving me invalid config. I have version 2021.1.

It Works
Thanks for the help.