APIs & Integrations

aljones
Mitwirkender/Mitwirkende

Create "Name" of record based on Multiple Text Property Values

lösung

I want to autocreate the "Ticket Name" field based on the selection of the subsequent properties. Ideally I would like it to look something like this:

 

First Name + Last Name +" - " + Lead Source + date

John Smith - CSG Webinar 9/5/2021

 

It looks like you can only create calculations based on number values

and it doesn't look like I can copy and join multiple single line property values from different objects into one target property field.

 

I couldn't find a solution in any of the other discussions - so any advice would be welcome.

0 Upvotes
1 Akzeptierte Lösung
piersg
Lösung
Autorität

Create "Name" of record based on Multiple Text Property Values

lösung

There's this page, specifically the example of running a script when a form submits. If you have a hidden field in your form for the ticket name, you could do something like this on submit (I've put in guesses as to what the field names would be):

<!--[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: "[PORTAL ID]",
      formId: "[FORM ID]",
        onFormSubmit: function(form){
            var firstName = form.querySelector('input[name="firstname"]').value;
            var lastName = form.querySelector('input[name="lastname"]').value;
            var leadSource = form.querySelector('input[name="lead_source"]').value;
            var date = new Date();
            date = date.toLocaleDateString('en-GB'); // will give you date in dd/mm/yyyy format
            form.querySelector('input[name="ticket_name"]').value = `${firstName} ${lastName} - ${leadSource} ${date}`;
        }
      });
    </script>

 

There's one downside to this method. If you're redirecting to another page, e.g. a thank you page, that redirection might happen before the above code executes and the form is sent. It would be fine with a thank you message.

 

Another method, which will make sure data is changed and sent before any redirection happens if you're using a redirect, is this:

{% form
form_to_use="{{ module.form.form_id }}"
response_response_type="{{ module.form.response_type }}"
response_message="{{ module.form.message }}"
%}
{% if module.form.redirect_id %}
<script>
  window.addEventListener('message', event => {
    if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmit') {
    	let firstName = event.data.data.find(o => o.name === 'firstname');
    	let lastName = event.data.data.find(o => o.name === 'lastname');
    	let leadSource = event.data.data.find(o => o.name === 'lead_source');
		var date = new Date();
        date = date.toLocaleDateString('en-GB'); // will give you date in dd/mm/yyyy format
        form.querySelector('input[name="ticket_name"]').value = `${firstName.value} ${lastName.value} - ${leadSource.value} ${date}`;
      
    }
    if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmitted') {
        {% set redirect = content_by_id(module.form.redirect_id) %}
        window.location = '{{redirect.absolute_url}}'
    }
  });
</script>
{% endif %}

 

Lösung in ursprünglichem Beitrag anzeigen

6 Antworten
aljones
Mitwirkender/Mitwirkende

Create "Name" of record based on Multiple Text Property Values

lösung

thanks @piersg  - hopefully this works for us!

0 Upvotes
piersg
Lösung
Autorität

Create "Name" of record based on Multiple Text Property Values

lösung

There's this page, specifically the example of running a script when a form submits. If you have a hidden field in your form for the ticket name, you could do something like this on submit (I've put in guesses as to what the field names would be):

<!--[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: "[PORTAL ID]",
      formId: "[FORM ID]",
        onFormSubmit: function(form){
            var firstName = form.querySelector('input[name="firstname"]').value;
            var lastName = form.querySelector('input[name="lastname"]').value;
            var leadSource = form.querySelector('input[name="lead_source"]').value;
            var date = new Date();
            date = date.toLocaleDateString('en-GB'); // will give you date in dd/mm/yyyy format
            form.querySelector('input[name="ticket_name"]').value = `${firstName} ${lastName} - ${leadSource} ${date}`;
        }
      });
    </script>

 

There's one downside to this method. If you're redirecting to another page, e.g. a thank you page, that redirection might happen before the above code executes and the form is sent. It would be fine with a thank you message.

 

Another method, which will make sure data is changed and sent before any redirection happens if you're using a redirect, is this:

{% form
form_to_use="{{ module.form.form_id }}"
response_response_type="{{ module.form.response_type }}"
response_message="{{ module.form.message }}"
%}
{% if module.form.redirect_id %}
<script>
  window.addEventListener('message', event => {
    if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmit') {
    	let firstName = event.data.data.find(o => o.name === 'firstname');
    	let lastName = event.data.data.find(o => o.name === 'lastname');
    	let leadSource = event.data.data.find(o => o.name === 'lead_source');
		var date = new Date();
        date = date.toLocaleDateString('en-GB'); // will give you date in dd/mm/yyyy format
        form.querySelector('input[name="ticket_name"]').value = `${firstName.value} ${lastName.value} - ${leadSource.value} ${date}`;
      
    }
    if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmitted') {
        {% set redirect = content_by_id(module.form.redirect_id) %}
        window.location = '{{redirect.absolute_url}}'
    }
  });
</script>
{% endif %}

 

piersg
Autorität

Create "Name" of record based on Multiple Text Property Values

lösung

How is the ticket being submitted? If it's a form on a website page you could do this with some JS at the point of submission.

0 Upvotes
aljones
Mitwirkender/Mitwirkende

Create "Name" of record based on Multiple Text Property Values

lösung

@piersg  thank you - can you direct me to any documentation on this?

0 Upvotes
dennisedson
HubSpot-Produktteam
HubSpot-Produktteam

Create "Name" of record based on Multiple Text Property Values

lösung

@aljones , how are you creating the records?  Via a form?

@piersg might have some thoughts.


Join us on March 27th at 12 PM for the Digital Essentials Lab, an interactive session designed to redefine your digital strategy!
Engage with expert Jourdan Guyton to gain actionable insights, participate in live Q&A, and learn strategies to boost your business success.
Don't miss this opportunity to connect and grow—reserve your spot today!

0 Upvotes
aljones
Mitwirkender/Mitwirkende

Create "Name" of record based on Multiple Text Property Values

lösung

@dennisedson  - yes  - it will be via form.

0 Upvotes