SOLVED

Re: Field Calculations

Go to solution
Bonni_Graham_Go
Level 2

Re: Field Calculations

I have a similar issue, but I'm not quite sure how to adapt this code to fit (or, honestly, where to put it, since I'm not using Marketo landing pages - our forms feed straight into our website, which is done in Wordpress using a Marketo Forms plugin). What I need to do is add the numerical values of 5 specific fields to populate a separate field. I do not need to display the results on the form; that value will drive dynamic text in an email based on segmentation rules (those I know how to set up).

 

I'm 90% sure I understand how to adjust the code itself, but I'm not sure where to PLACE the script to trigger the action. I strongly suspect it's in the script area of my Wordpress page, but am not sure. I'm also not entirely sure the script as it is now posts back to Marketo if the script resides on a page that is not a Marketo landing page. Help?

SanfordWhiteman
Level 10 - Community Moderator

Re: Field Calculations

Scripts that drive extended forms behaviors need to go after the script that loads forms2.min.js (since they depend on that script, the main Forms 2.0 JS library, to have already loaded and created its global object MktoForms2).

 

So if you know forms2.min.js is being loaded at the same place in the HTML that the <form> element is — which is usually, but not always, the case — you can add the extended forms JS right before the closing </body> and that will implicitly be after the required script.

Bonni_Graham_Go
Level 2

Re: Field Calculations

Perfect - I suspected, but was not sure that was the case. You rock, sir.

Bonni_Graham_Go
Level 2

Re: Field Calculations

So, I didn't get my total. I am sure I did something stupid. Here's my revised script:

var mktoInstance = '//app-sjo.marketo.com',
munchkinId = '313-JIS-706',
formId = 6846;
// reuse existing fields in demo!
var fieldProxyForWhatDevices = 'QuizWhatDevices',
fieldProxyForDeviceHardening = 'QuizDeviceHardening';
fieldProxyForResponsePlan = 'QuizResponsePlan';
fieldProxyForThreats = 'QuizThreats';
fieldProxyForImpact = 'QuizImpact';
fieldProxyForScore = 'QuizTotalScore';
MktoForms2.loadForm(mktoInstance, munchkinId, formId,
function (form) {
form.onSubmit(function (form) {
var devices = +form.getValues()[fieldProxyForWhatDevices],
hardening = +form.getValues()[fieldProxyForDeviceHardening],
response = +form.getValues()[fieldProxyForResponsePlan],
threats = +form.getValues()[fieldProxyForThreats],
impact = +form.getValues()[fieldProxyForImpact],
finalValues = {};
// calc the actual final value and post that to Marketo
finalValues[fieldProxyForScore] = devices + hardening + response + threats + impact;
form.setValues(finalValues);
form.submittable(false);
});
});

 

I added this script to the end of the page where the form that collects the values resides. I have verified that forms2.min.js loads prior to my script, and that my script appears before the </body> tag. I got the scores for each answer on the quiz, but I didn't get a total.  Am I missing something?

SanfordWhiteman
Level 10 - Community Moderator

Re: Field Calculations

What's your URL? Hard to know if you're embedding this correctly.

Bonni_Graham_Go
Level 2

Re: Field Calculations

My site URL is https://www.scantron.com. There will be a specific quiz URL, but the page in question is not yet published (although I published briefly for testing). It's embedded through a Wordpress Plugin called MarketoForms. Here's the staged version so you can look at the page source: https://www.scantron.com/a1f5b73cabd1a13198ea3d6c5ba254fa/ 

SanfordWhiteman
Level 10 - Community Moderator

Re: Field Calculations

Well, for starters, you didn't put the code in a <script>! It's just text on the page.

Bonni_Graham_Go
Level 2

Re: Field Calculations

It's in the script box in WP, which I *thought* blocked that out automatically with the script command, but clearly does not. OK, let me try adding that to the script box. 

Bonni_Graham_Go
Level 2

Re: Field Calculations

So I added that, but I'm still not getting my total score. Same link I provided earlier will show the updated code.

 

SanfordWhiteman
Level 10 - Community Moderator

Re: Field Calculations

Fatal syntax error, always make sure to check the F12 Console first.

 

2020-09-28 22_13_19-Cybersecurity Quiz _ Scantron.png

You want semicolons where you have commas in this section.