SOLVED

Re: Radom Generated Unique ID Upon Form Submit

Go to solution
Michelle_LaVer1
Level 2

Random Generated Unique ID Upon Form Submit

Hello,

 

We are looking to generate a random ID value when a Marketo form is submitted. We would make it a hidden form field that we'd block from being overwritten if there is an existing value. Has anyone done this and if so how?

 

Thanks,

Michelle

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Radom Generated Unique ID Upon Form Submit

What are you using this for, may I ask?

 

In any case, you can generate a UUID.  Here the Marketo field being populated is called FormSubmissionID.  You would swap that with the SOAP name of the field in your instance:

 

(function(){
    /**
     * @preserve contains SO @broofa's UUID polyfill (https://stackoverflow.com/questions/105034/)
     * @author https://stackoverflow.com/users/109538/broofa
     * @license CC-BY-SA
     *
     */
    function uuidv4() {
        var crypto = window.crypto || window.msCrypto;
        return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
            (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
        );
    }
   
    MktoForms2.whenReady(function(mktoForm){
      mktoForm.addHiddenFields({
         FormSubmissionID : uuidv4()
      });
    });
})();

 

 

Also, this is adding the UUID when the form is displayed, rather than just before it's submitted. Is there a specific reason you want it added just before submission (since it's globally unique anyway)?

View solution in original post

3 REPLIES 3
SanfordWhiteman
Level 10 - Community Moderator

Re: Radom Generated Unique ID Upon Form Submit

What are you using this for, may I ask?

 

In any case, you can generate a UUID.  Here the Marketo field being populated is called FormSubmissionID.  You would swap that with the SOAP name of the field in your instance:

 

(function(){
    /**
     * @preserve contains SO @broofa's UUID polyfill (https://stackoverflow.com/questions/105034/)
     * @author https://stackoverflow.com/users/109538/broofa
     * @license CC-BY-SA
     *
     */
    function uuidv4() {
        var crypto = window.crypto || window.msCrypto;
        return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
            (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
        );
    }
   
    MktoForms2.whenReady(function(mktoForm){
      mktoForm.addHiddenFields({
         FormSubmissionID : uuidv4()
      });
    });
})();

 

 

Also, this is adding the UUID when the form is displayed, rather than just before it's submitted. Is there a specific reason you want it added just before submission (since it's globally unique anyway)?

Michelle_LaVer1
Level 2

Re: Radom Generated Unique ID Upon Form Submit

Thanks for the solution. Looking to pass a unique ID value when the form is submitted that we can pass to engineering when a new trial account is requested via our form submit.

 

Thanks,

Michelle

SanfordWhiteman
Level 10 - Community Moderator

Re: Radom Generated Unique ID Upon Form Submit

OK, then there's no reason to not generate the ID when the form is ready. So the code above is fine.

 

I do wonder how you're planning to interlock the form submission ID with the other fields in the form, though. Technically the only place to see a discrete form payload (all fields submitted at the same time) is in the Activity Log.