Skip to main content

Hello,

I am trying to use ETM to pull out the Acquired Date of an item, then set a custom Date field value based on this date. The custom field will be set to 49 months after the Acquire date.

Pulling out the value of the system field and doing the calculation is no issue, however when I try to set the value of the custom field, I am getting this error:

 

In this case, the “acquiredDate” variable is being set earlier in the mapper from outbound.self.acquiredDate.

 

If I convert the value to a string and use it to set the value on a single-line string field, it works fine (see below).

 

 

Any help would be appreciated.

 

Thanks,

 

Duncan

 

Hey Duncan,

 

Try this.

var acq = new Date(variables.acquiredDate);
var ever = new Date(acq.setMonth(acq.getMonth() + 49));

ever;

In my case I’m using the first data mapper in the channel to set my variables.  I’m also pulling the initial date from another custom field (Effective From) not the Acquired Date field.

When debugging in ETM it will still show as this.

 

However, in assystweb it shows correctly.

 


Thanks. I did try the new Date() function a couple of different ways with no luck, but this way worked.


I’ll explain why the original expression didn’t work & why @momboc’s solution worked.

acq = variables.acquiredDate;

This creates a new reference (acq) to the acquiredDate variable. Modifications of acq also affect acquiredDate & vice-versa.


ever = acq.setMonth(acq.getMonth() + 49);

 adds 49 months to acq/acquiredDate and sets ever to the timestamp (milli seconds since 1st Jan 1970). See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setMonth  - specifically see “Return Value”.

ever;

Sets the custom fields to the number of millis since 1/1/1970)

 

Using new Date()  with the result of setMonth() solves the problem by creating a new Date object for the custom field value.

 

A simpler solution would be 

variables.acquiredDate.setMonth(variables.acquiredDate.getMonth() + 49);

variables.acquiredDate;

this makes it clear that setMonth() is modifying the acquiredDate and not just a copy.,

Alternatively:

var acq= new Date(variables.acquiredDate);

acq.setMonth(acq.getMonth() + 49);

acq;

Creates a copy of acquiredDate before modification. In this case acquiredDate isn’t changed. This could be important if subsequent mappings reference acquiredDate, and expect the original value - not the one 4 years later.

 


Reply