Auto populate city state based on postal code

I fixed this issue by creating a workflow with a custom code block.

I used the google geocode api to find the city based on the postalcode of an contact person

1. Create a trigger for postalcode and select the option “is known”.

Note: Don’t forget to select the postalcode is known in the re-register tab.

2. Add the block custom code to your workflow.

3. Add your google apikey to the secret field

4. Add postalcode to the properties to be included in the code.

at the end it should look like this:

5. Write the custom code:

This is the code i written to get the city by the postalcode:

const hubspot = require('@hubspot/api-client');
const axios = require('axios');

exports.main = async (event, callback) => {
 const zip = event.inputFields['zip'];
 var city = "";
 var url = [
 "https://maps.googleapis.com/maps/api/geocode/json?components=postal_code:",
 event.inputFields['zip'],
 "|country:NL&key=",
 process.env.GAPIKEY
 ];

 // Get city by postalcode using google api
 await axios
 .get(url.join(""))
 .then(res => {
 console.log(`statusCode: ${res.status}`)
 res.data['results'][0]['address_components'].forEach(function (item, index){
 if(item['types'][0] == "locality") {
 city = item['long_name'];
 }
 })
 })
 .catch(error => {
 console.error(error)
 })

 callback({
 outputFields: {
 city: city
 }
 });
}

Note: make sure you change the country in the url to your country

6. Define city the output fields so it can be used in the workflow

7. Add a new action to the workflow “copy property value”, chose the output from the custom code and the value of the contact person in this case city.

Save the workflow and you are done