I am trying to add Javascript to a Marketo LP using a Marketo form that adds a date/time stamp to the submission. I have created a new field in Marketo named Last Form Submission Date (lastFormSubmissionDate) and am adding the field to the form. Then on the Marketo page I am adding this code to the Custom Head:
<script>
MktoForms2.whenReady(function(mktoForm){
mktoForm.onSubmit(function(mktoForm){
mktoForm.addHiddenFields({
lastFormSubmissionDate : new Date().toISOString()
});
});
});
</script>
It is not populating the field though, so wondering what I am doing wrong.
Hey @kenmckown - I think you might be making this more complicated than it needs to be!
Why don't you use a system token in a change data value?
From Experience League:
The {{system.dateTime}} token will render the current date and time at runtime like so: 2013-08-08 16:36:13
Works in:
You can use a Change Data Value flowstep to change the data value of your 'Last Form Submission Date' to {{system.DateTime}}!
This was actually my first thought, but we wanted to make sure it was written at the time of submission so we can use it to route in Dynamic Chat, so it needs to be real time as opposed to a program, which could have a little delay.
Does that make sense?
@kenmckownThere shouldn't be a delay unless you include a wait step, but I see what you mean with Dynamic Chat. Do you mind sharing your use case in full? That would probably help more.
Sure, we will be using dynamic chat to book a meeting, which uses a conversational form that asks questions. We want to use that date stamp to route whether to send it directly to a sales rep (during business hours) or to a queue for after hours. Basically using that date and time stamp to know if it can be worked right away.
You might as well put any date field as hidden field on your form and populate it with the system.token. Like this:
I tried that, but since it is a string, it shows up like this:
Assume I need to make it a time/date field type?
The specific reason your code doesn’t work is it has a dependency on the global MktoForms2
object and you’re loading it in the <head>
, while the Forms 2.0 library loads somewhere in the body.
Move the code just before the closing </body>
tag and it’ll work.
As for doing this on the form vs. in a Smart Campaign, I like doing it on the form because it will really capture the form submission time.
Would I have to add that to the LP Template? There is not a way to edit the code of a LP itself correct?
It is still pulling back the value like this: