Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.

Populate current date into date field?

Avatar

Level 2

I've been working on trying to get a date field to populate the current date if the user enters ' / ' into the field.  So far I have been able to replace ' / ' with a value but I am not able to populate the current date.  I have tried several suggests that on who to populate current date but have not been able to get my code functioning. 

I have place the below code on the exit even of the date field which runs correctly but when I try to add in the date it breaks.  Any one have a suggestion?

if  (this.rawValue == "/")

{

this.rawValue = 1

}

Thanks

3 Replies

Avatar

Level 10

Hi,

I'm not sure why you choose such workflow to populate a date field, but here's my solution in FormCalc.

Put it into the change event.

if ($event.newText eq "/") then

$ = Num2Date(date(), "MM/DD/YYYY")

endif

Avatar

Level 2

Thanks, Radzmar

Unfotenlty that doesn't seem to work either. 

I know its a strange workflow but its meant to mimic software that I use every day.

Thanks again for the reply.

Avatar

Level 4

I have to agree with Radzmar that the workflow is quite odd. That character is a bit unique; however there are 2 ways you can get this to work. One is just to use the code snippet (below) on the Exit event of the text field where the "/" is located or you could use the Action Builder. The Action Builder is a bit easier to understand if your coding knowledge is a bit lacking like myself. Just go to the toolbar at the top, select tools then Action Builder. Each action has a condition and a result. In the condition section select the field which is going to contain the "/", make sure it is set to "is" and then in the box put /. From the result box, select "set the value of a field", select the date field that you wish to have today's date and then it make sure it says "to today's date". Then whenever someone puts "/" into the text field, the date field will populate today's date automatically.

if (this.resolveNode("$").rawValue == "/") {

  this.resolveNode("DateTimeField1").rawValue = util.printd("yyyy-mm-dd", new Date());

}