# Update contact property on form submit

**URL:** <https://community.hubspot.com/t/update-contact-property-on-form-submit/13004>\
**Category:** APIs & Integrations\
**Tags:** developer-tools, forms, contacts\
**Created:** [February 14, 2019, 1:31pm UTC](https://community.hubspot.com/t/update-contact-property-on-form-submit/13004 "2019-02-14T13:31:51Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/system/32/124_2.png) [@system](https://community.hubspot.com/u/system)\
**Post date:** [February 14, 2019, 1:31pm UTC](https://community.hubspot.com/t/update-contact-property-on-form-submit/13004/1 "2019-02-14T13:31:51Z")

</div>

Hi guys,

I have a custom property group called “Salesforce Information”. Within this group I’ve created a custom property called “web\_activity\_most\_recent\_\_c” and set it to hidden on a form.

Scenario: Everytime a form is submitted, we need this property to be updated with a value dependent on the page the form is submitted from. We currently have over 120 pages with corrsponsponding web\_activity\_most\_recent\_\_c values.

eg. if the visitor submits a form from [Ideagen Huddle](http://huddle.com/get-started/) then the value forweb\_activity\_most\_recent\_\_c should be “Downloaded\_ebook\_collaboration\_client-experience”, and a different value if the form is submited from another page, etc.

I can’t get it to work…

Any assistance will be much appreciated!

Herewith my edited form embed code:

```auto
<!--[if lte IE 8]>
<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2-legacy.js"></script>
<![endif]-->
<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2.js"></script>
<script>
hbspt.forms.create({
portalId: "my-hubspot-code",
formId: "e7075578-e362-4c94-a075-10fb9c74e8e0",
redirectUrl: "/sites/default/files/white-papers-files/huddle_-_accounting_and_professional_services_e-paper_2018.pdf",
onFormReady($form) {
$('input[name="web_activity_most_recent__c"]').val('Downloaded_ebook_collaboration_client-experience').change();
}
});
</script>

```

 ![Screenshot 2019-02-14 at 12.26.42.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/a/2/a269012897580af9479cf206d132745c1250a923.png)

 ![Screenshot 2019-02-14 at 12.27.16.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/2/d/2d4e9a35361bf80bd1bf66b7372ec7ab68f9ce93.png)

---

<div class="post-metadata">

**Author:** ![JasminLin](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jasminlin/32/99730_2.png) [@JasminLin](https://community.hubspot.com/u/JasminLin)\
**Post date:** [February 15, 2019, 8:33am UTC](https://community.hubspot.com/t/update-contact-property-on-form-submit/13004/2 "2019-02-15T08:33:15Z")

</div>

Hey @Anonymous , if you use this instead it should work:

```auto
onFormReady($form) {
$('input[name="web_activity_most_recent__c"]', $form).val('Downloaded_ebook_collaboration_client-experience').change();
}

```

---

<div class="post-metadata">

**Author:** ![system](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/system/32/124_2.png) [@system](https://community.hubspot.com/u/system)\
**Post date:** [February 15, 2019, 9:11am UTC](https://community.hubspot.com/t/update-contact-property-on-form-submit/13004/3 "2019-02-15T09:11:10Z")

</div>

Thanks but that doesn’t work either. It’s the $ that’s the issue. It works perfectly when replacing the $ with the actual word “JQuery”.

**Final working code as below:**

```auto
<!--[if lte IE 8]><script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2-legacy.js"></script><![endif]--><script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2.js"></script><script>
hbspt.forms.create({
  portalId: "replace-this-with-your-hubspot-id",
  formId: "replace-this-with-hubspot-form-id",
  onFormSubmit: function($form) {
    jQuery('input[name="replace-this-with-hubspot-internal-name-of-hidden-field"]').val('replace-this-with-your-required-value').change();
  }
});
</script>

```

Also ensure your Salesforce mapping within the Hubspot integration settings is set to “ **Use most recent value** ” to avoid Salesforce from overwriting recent values changes in Hubspot.

 ![Screenshot 2019-02-18 at 14.43.14.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/b/b/bb0102153283a9090ec0c53c659b8623df69b738.png)

---

<div class="post-metadata">

**Author:** ![dannio](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dannio/32/95715_2.png) [@dannio](https://community.hubspot.com/u/dannio)\
**Post date:** [March 27, 2020, 8:47am UTC](https://community.hubspot.com/t/update-contact-property-on-form-submit/13004/4 "2020-03-27T08:47:54Z")

</div>

Wouldn’t it be much safer to just use plain js to do this?

e.g:

```auto
<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2.js"></script>
<script>
 hbspt.forms.create({
 portalId: "xxxxxxx",
 formId: "xxxxxxx-xxxx-xxxx-xxxxxxxxxxxx",
 onFormReady: function($form){
 valueFrom = document.querySelector('#value-from').innerHTML;
 document.getElementsByName("value_to")[0].value=valueFrom;
 },
});
</script>

```

This way you wouldn’t be relying on jQuery at all, and it would, in general, be much quicker and more reliable. (No blocking, fires as soon as form loaded)

---

<div class="post-metadata">

**Author:** ![RLightfoot](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/rlightfoot/32/2317_2.png) [@RLightfoot](https://community.hubspot.com/u/RLightfoot)\
**Post date:** [March 25, 2021, 12:19am UTC](https://community.hubspot.com/t/update-contact-property-on-form-submit/13004/5 "2021-03-25T00:19:51Z")

</div>

@dannio -In your example, where would I place the “internal\_property\_name” of the field, and where would I place the “value” to go into that field?

Thank you

---

<div class="post-metadata">

**Author:** ![dannio](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dannio/32/95715_2.png) [@dannio](https://community.hubspot.com/u/dannio)\
**Post date:** [March 25, 2021, 1:40am UTC](https://community.hubspot.com/t/update-contact-property-on-form-submit/13004/6 "2021-03-25T01:40:00Z")

</div>

@RLightfoot the internal\_property\_name is the **value\_to** , and the value is the **valueFrom** :

`document.getElementsByName("internal_property_name")[0].value=valueFrom;`

---

<div class="post-metadata">

**Author:** ![RLightfoot](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/rlightfoot/32/2317_2.png) [@RLightfoot](https://community.hubspot.com/u/RLightfoot)\
**Post date:** [March 25, 2021, 2:58am UTC](https://community.hubspot.com/t/update-contact-property-on-form-submit/13004/7 "2021-03-25T02:58:43Z")

</div>

@dannio - sorry, novice here…  
is this the syntax?

\<!--[if lte IE 8]\>  
\<script charset=“utf-8” type=“text/javascript” src=“[//js.hsforms.net/forms/v2-legacy.js](https://js.hsforms.net/forms/v2-legacy.js)”\>\</script\>  
\<![endif]--\>  
\<script charset=“utf-8” type=“text/javascript” src=“[//js.hsforms.net/forms/v2.js](https://js.hsforms.net/forms/v2.js)”\>\</script\>  
\<script\>  
hbspt.forms.create({  
region: “xxx”,  
portalId: “xxxxxx”,  
formId: “xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”  
onFormReady: function($form){  
valueFrom = document.querySelector(‘#value-from’).innerHTML;  
document.getElementsByName(“favorite\_character”)[0].(‘Barney Rubble’)=valueFrom;  
}  
});  
\</script\>

Thank you.

---

<div class="post-metadata">

**Author:** ![dannio](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dannio/32/95715_2.png) [@dannio](https://community.hubspot.com/u/dannio)\
**Post date:** [March 25, 2021, 3:16am UTC](https://community.hubspot.com/t/update-contact-property-on-form-submit/13004/8 "2021-03-25T03:16:41Z")

</div>

As far as I can tell, **region** isn’t a param on the embedded code.

You can find out more of the form embedded code here:

[https://legacydocs.hubspot.com/docs/methods/forms/advanced\_form\_options](https://legacydocs.hubspot.com/docs/methods/forms/advanced_form_options)

The **valueFrom** variable in my example is only an example of where the data could come from. In this case, it’s an element with the ID **value-from** somewhere on the page.

The assignment should be something like:

`document.getElementsByName("favorite_character")[0].value='Barney Rubble';`

You can check if the value has been set after by opening up the console and typing:

`document.getElementsByName("favorite_character")[0].value `

---

<div class="post-metadata">

**Author:** ![RLightfoot](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/rlightfoot/32/2317_2.png) [@RLightfoot](https://community.hubspot.com/u/RLightfoot)\
**Post date:** [March 25, 2021, 3:42am UTC](https://community.hubspot.com/t/update-contact-property-on-form-submit/13004/9 "2021-03-25T03:42:49Z")

</div>

@dannio - Thank you.

---

<div class="post-metadata">

**Author:** ![dannio](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dannio/32/95715_2.png) [@dannio](https://community.hubspot.com/u/dannio)\
**Post date:** [March 29, 2021, 2:26am UTC](https://community.hubspot.com/t/update-contact-property-on-form-submit/13004/10 "2021-03-29T02:26:45Z")

</div>

No worries. Another way to make sure it gets the correct element with js is to use the **form** value that’s defined by the callback like so:

`form.elements.favorite_character.value = *insert value*`

---

<div class="post-metadata">

**Author:** ![bradmin](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/bradmin/32/56633_2.png) [@bradmin](https://community.hubspot.com/u/bradmin)\
**Post date:** [February 15, 2019, 1:06pm UTC](https://community.hubspot.com/t/update-contact-property-on-form-submit/13004/11 "2019-02-15T13:06:07Z")

</div>

Hi, @Anonymous. While the approach outlined would work, the properties in the Salesforce Information section are used in mappings. Populating the properties ending in “\_\_c” - custom fields from Salesforce - won’t push values to Salesforce from HubSpot. You want to update the HubSpot property mapped to that Salesforce Information property instead in your form.

---

<div class="post-metadata">

**Author:** ![system](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/system/32/124_2.png) [@system](https://community.hubspot.com/u/system)\
**Post date:** [February 18, 2019, 7:28am UTC](https://community.hubspot.com/t/update-contact-property-on-form-submit/13004/12 "2019-02-18T07:28:08Z")

</div>

Thanks for this @bradmin , what do you suggest I do?

I’ve gone ahead and updated the Update Rule in the Salesforce mapping section within Hubspot from Use Salesforce value to Use updated value - as per Hubspot documentation. Everything seems to be working fine? Is there something I’m missing?

 ![Screenshot 2019-02-18 at 14.43.14.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/b/b/bb0102153283a9090ec0c53c659b8623df69b738.png)

---

<div class="post-metadata">

**Author:** ![bradmin](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/bradmin/32/56633_2.png) [@bradmin](https://community.hubspot.com/u/bradmin)\
**Post date:** [February 19, 2019, 1:14pm UTC](https://community.hubspot.com/t/update-contact-property-on-form-submit/13004/13 "2019-02-19T13:14:26Z")

</div>

@Anonymous, it looks like the API name of the HubSpot-side property matches the Salesforce property. That would work if your custom HubSpot property has that same API name, but that may not be sustainable.

Mappings allow you to populate the Salesforce side of the mapping, by setting the value in the HubSpot-side property part of the mapping. You’d have CustomHubspotProperty on the left side of the mapping, and Custom\_Salesforce\_Field\_\_c on the right. After that’s established, put CustomHubSpotProperty as your field on the form, and subsequent submissions will pass the desired value to Salesforce in Custom\_Salesforce\_Field\_\_c.

The ambiguous part is that the Salesforce field needs to exist among your HubSpot properties, so it can be included in the mapping. However, if you map the Salesforce field to the Salesforce field, nothing will effectively get synced to Salesforce from HubSpot.

---

<div class="post-metadata">

**Author:** ![system](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/system/32/124_2.png) [@system](https://community.hubspot.com/u/system)\
**Post date:** [February 19, 2019, 1:19pm UTC](https://community.hubspot.com/t/update-contact-property-on-form-submit/13004/14 "2019-02-19T13:19:43Z")

</div>

@bradmin thanks for the response, however everything seems to be working and matches up perfectly. Basically we had this working through Marketo for ages, so it’s the exact same implementation.

The field’s value updates whether amended from Salesforce or Hubspot.

Cheers

---

<div class="post-metadata">

**Author:** ![bradmin](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/bradmin/32/56633_2.png) [@bradmin](https://community.hubspot.com/u/bradmin)\
**Post date:** [February 19, 2019, 1:21pm UTC](https://community.hubspot.com/t/update-contact-property-on-form-submit/13004/15 "2019-02-19T13:21:27Z")

</div>

Okay, if it’s working for you, you’re all set. That’s a particular config which does have the potential to interfere with results, but if you haven’t experienced any issues, disregard what I wrote.

---

<div class="post-metadata">

**Author:** ![system](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/system/32/124_2.png) [@system](https://community.hubspot.com/u/system)\
**Post date:** [February 19, 2019, 2:09pm UTC](https://community.hubspot.com/t/update-contact-property-on-form-submit/13004/16 "2019-02-19T14:09:26Z")

</div>

Awesome thanks @bradmin I’ll definitely keep an eye out.

Thanks again, cheers
