APIs & Integrations

rechavar
Participant

append info instead of overwriting

SOLVE

Hi everybody, hope everythings going well!

 

This is my situacion, Im sending lead info from my webpage to hubspot using python. the thing is, some properties doesnt need to be overwrited.

E.g. someone is looking product  in my website, so he sends a request and i receive it and send it to hubspot, then he goes to other option that he likes it too and sends a request with the second option and its the same as the first request, what i hoping to see in hubspot its 1 contact with a field that says tha he likes option 1 and 2.

 

Theres one las ting to get in mind, not all properties gonna be appended, in the above case, i need that email, phone, cityt, etc be overwritted in each request

 

Is there some doc that i can look for, or theres somethin im missing that is overwriting each time that a contact sends a request?

 

thanks in advance, have a great week

0 Upvotes
1 Accepted solution
Teun
Solution
Key Advisor | Diamond Partner
Key Advisor | Diamond Partner

append info instead of overwriting

SOLVE

@rechavar ,

 

I wrote this real quick, but it retrieves the properties of the contact and checks if it includes the new value. If not, it adds the value and updates the contact. If you use an API key instead of a private app token, remove the 'authorization' header and add the API key to the URL.

let needToUpdate = false

const getCurrentContact = async (contactId, token) => {
  return axios({
    method: 'get',
    url: `https://api.hubapi.com/crm/v3/objects/contacts/${contactId}?properties=project_names&properties=suppliers&properties=prices`,
    headers: {
      'Authorization': `Bearer ${token}`,
      'Content-Type': 'application/json'
    }
  }).then((response) => {
    if (response.data.properties) {
      return response.data.properties
    } else {
      return false
    }
  }).catch((error) => {
    console.log(`Error while retrieving contact: ${error.response.data.message}`)
    return false
  })
}

const updateContact = async (contactProperties, contactId, token) => {
  const data = contactProperties

  return axios({
		method: 'patch',
		url: `https://api.hubapi.com/crm/v3/objects/contacts/${contactId}`,
		headers: {
			'Authorization': `Bearer ${token}`,
      'Content-Type': 'application/json'
		},
    data: JSON.stringify(data)
	}).then((response) => {
    return response
  }).catch((error) => {
    console.log(`Error while updating contact: ${error.response.data.message}`)
    return false
  })
}

const currentContactProperties = await getCurrentContact(contactId, token)

if (!currentContactProperties.project_names.includes(myNewProjectValue)) {
  currentContactProperties.project_names.push(myNewProjectValue)
  needToUpdate = true
}

if (!currentContactProperties.suppliers.includes(myNewSupplierValue)) {
  currentContactProperties.suppliers.push(myNewSupplierValue)
  needToUpdate = true
}

if (!currentContactProperties.prices.includes(myNewPricesValue)) {
  currentContactProperties.prices.push(myNewPricesValue)
  needToUpdate = true
}

if (needToUpdate) {
  const updatedContact = await updateContact(currentContactProperties, contactId, token)
  console.log(updatedContact)
}






Did my answer solve your issue? Help the community by marking it as the solution.

View solution in original post

8 Replies 8
JMark2
Member

append info instead of overwriting

SOLVE

@Teun wrote:

@rechavarprequel,

 

I wrote this real quick, but it retrieves the properties of the contact and checks if it includes the new value. If not, it adds the value and updates the contact. If you use an API key instead of a private app token, remove the 'authorization' header and add the API key to the URL.

 

 

let needToUpdate = false

const getCurrentContact = async (contactId, token) => {
  return axios({
    method: 'get',
    url: `https://api.hubapi.com/crm/v3/objects/contacts/${contactId}?properties=project_names&properties=suppliers&properties=prices`,
    headers: {
      'Authorization': `Bearer ${token}`,
      'Content-Type': 'application/json'
    }
  }).then((response) => {
    if (response.data.properties) {
      return response.data.properties
    } else {
      return false
    }
  }).catch((error) => {
    console.log(`Error while retrieving contact: ${error.response.data.message}`)
    return false
  })
}

const updateContact = async (contactProperties, contactId, token) => {
  const data = contactProperties

  return axios({
		method: 'patch',
		url: `https://api.hubapi.com/crm/v3/objects/contacts/${contactId}`,
		headers: {
			'Authorization': `Bearer ${token}`,
      'Content-Type': 'application/json'
		},
    data: JSON.stringify(data)
	}).then((response) => {
    return response
  }).catch((error) => {
    console.log(`Error while updating contact: ${error.response.data.message}`)
    return false
  })
}

const currentContactProperties = <a href="https://apkkernal.com/prequel-mod-apk/">prequel</a> getCurrentContact(contactId, token)

if (!currentContactProperties.project_names.includes(myNewProjectValue)) {
  currentContactProperties.project_names.push(myNewProjectValue)
  needToUpdate = true
}

if (!currentContactProperties.suppliers.includes(myNewSupplierValue)) {
  currentContactProperties.suppliers.push(myNewSupplierValue)
  needToUpdate = true
}

if (!currentContactProperties.prices.includes(myNewPricesValue)) {
  currentContactProperties.prices.push(myNewPricesValue)
  needToUpdate = true
}

if (needToUpdate) {
  const updatedContact = await updateContact(currentContactProperties, contactId, token)
  console.log(updatedContact)
}

 

 

Thankyou so much it helped me also. Finally i ran it without getting any errors.

0 Upvotes
Teun
Key Advisor | Diamond Partner
Key Advisor | Diamond Partner

append info instead of overwriting

SOLVE

Hi @dennisedson ,

 

Like Dennis said, you might want to use a checkbox or even a multiselect. If you go for a multiselect, you need a function to check if the option is present, and if not, add it.
You could also go for a single-line text, but in that case, you would also need to first retrieve the contact and the property you want to append a value to and add the value instead. 

I am not great with Python, but could share some NodeJS examples.



Did my answer solve your issue? Help the community by marking it as the solution.
rechavar
Participant

append info instead of overwriting

SOLVE

Im needing the checkbox option, well I guess. Using some king of json sintax, this is what i need at the end of the day:

name: jhon
last_name: perez
city: virgia beach
state: VA
project_names: ["12x30 barn", "18x22 metal garage", "12 x 25 carport"]

Suppliers: ["Alans factory", "catapult steel buildings", "UMB"]
prices: ["2000", "49000", 50000"]

 

would be great if you can share with me the NodeJs example, thanks my man, appreciated it!!!

0 Upvotes
Teun
Solution
Key Advisor | Diamond Partner
Key Advisor | Diamond Partner

append info instead of overwriting

SOLVE

@rechavar ,

 

I wrote this real quick, but it retrieves the properties of the contact and checks if it includes the new value. If not, it adds the value and updates the contact. If you use an API key instead of a private app token, remove the 'authorization' header and add the API key to the URL.

let needToUpdate = false

const getCurrentContact = async (contactId, token) => {
  return axios({
    method: 'get',
    url: `https://api.hubapi.com/crm/v3/objects/contacts/${contactId}?properties=project_names&properties=suppliers&properties=prices`,
    headers: {
      'Authorization': `Bearer ${token}`,
      'Content-Type': 'application/json'
    }
  }).then((response) => {
    if (response.data.properties) {
      return response.data.properties
    } else {
      return false
    }
  }).catch((error) => {
    console.log(`Error while retrieving contact: ${error.response.data.message}`)
    return false
  })
}

const updateContact = async (contactProperties, contactId, token) => {
  const data = contactProperties

  return axios({
		method: 'patch',
		url: `https://api.hubapi.com/crm/v3/objects/contacts/${contactId}`,
		headers: {
			'Authorization': `Bearer ${token}`,
      'Content-Type': 'application/json'
		},
    data: JSON.stringify(data)
	}).then((response) => {
    return response
  }).catch((error) => {
    console.log(`Error while updating contact: ${error.response.data.message}`)
    return false
  })
}

const currentContactProperties = await getCurrentContact(contactId, token)

if (!currentContactProperties.project_names.includes(myNewProjectValue)) {
  currentContactProperties.project_names.push(myNewProjectValue)
  needToUpdate = true
}

if (!currentContactProperties.suppliers.includes(myNewSupplierValue)) {
  currentContactProperties.suppliers.push(myNewSupplierValue)
  needToUpdate = true
}

if (!currentContactProperties.prices.includes(myNewPricesValue)) {
  currentContactProperties.prices.push(myNewPricesValue)
  needToUpdate = true
}

if (needToUpdate) {
  const updatedContact = await updateContact(currentContactProperties, contactId, token)
  console.log(updatedContact)
}






Did my answer solve your issue? Help the community by marking it as the solution.
rechavar
Participant

append info instead of overwriting

SOLVE

Thank you so much my man, really appreciated it!!! this everything im needing!!!

Teun
Key Advisor | Diamond Partner
Key Advisor | Diamond Partner

append info instead of overwriting

SOLVE

Awesome! Happy I could help.



Did my answer solve your issue? Help the community by marking it as the solution.
dennisedson
Community Manager
Community Manager

append info instead of overwriting

SOLVE

@rechavar , I believe the property field type that you would want to use is a checkbox. 

See here for more info on properties

You can patch that property, but when you patch it, you will need to include all checked options in the new request.  If you only include the newest, the older one will be removed.

@taran42 any suggestions from your end?

Thanks,

Dennis




HubSpot Community Developer ShowMake sure to subscribe to our YouTube channel
where you can find the HubSpot Community Developer Show
0 Upvotes
rechavar
Participant

append info instead of overwriting

SOLVE

Ohhh thanks, ill try it and if it workz im gonna accept it as Solution, but, just to be clear, at the end of the day im gonna see in hubspot something like this?(where you see lists "[]" its mean that the info in that field needs to be append):

 

name: jhon
last_name: perez
city: virgia beach
state: VA
project_names: ["12x30 barn", "18x22 metal garage", "12 x 25 carport"]

Suppliers: ["Alans factory", "catapult steel buildings", "UMB"]
prices: ["2000", "49000", 50000"]

 

thank you so much for your answer!! appreciated it