⚙ Operations Hub

ah8
投稿者

Custom Code - Make a form submission create a ticket and associate that ticket with a deal

解決

Hi,

I wasn't actually sure where to put this. I am new to HubSpot. I am trying to use workflows to make a form submission create a ticket and associate that ticket with a deal. Advice on getting started would be greatly appreciated. I think it may take a custom code action, but what objects to use, etc, I don't really know.

 

Thank you

2件の承認済みベストアンサー
CMcKay
解決策
トップ投稿者

Custom Code - Make a form submission create a ticket and associate that ticket with a deal

解決

Hey @ah8 ,

 

This is an interesting idea, so a couple of questions for you: 

 

1. What type of form is this? What does it do?

2. How do you want to associate the Deal? Does the form as for a deal ID?

 

The reason I ask these things is because if the form is a ticket form it will already create a ticket for you and bonus if you have a field on the ticket form that's the deal object record id property you can pretty easily create that in a new ticket workflow and just associate the deal with the ticket in a super simple custom code (which I can give you with a loom). 

Let me know.

元の投稿で解決策を見る

ah8
解決策
投稿者

Custom Code - Make a form submission create a ticket and associate that ticket with a deal

解決

Thank you. I realized that the issue I was facing was that I couldn't do the put request through hubspotClient. I had to use Axios. It works now. I just wish I could pull the secret through hubspotClient without messing up Axios. That's still something for me to work on...

元の投稿で解決策を見る

12件の返信
AMammarella
投稿者 | Diamond Partner
投稿者 | Diamond Partner

Custom Code - Make a form submission create a ticket and associate that ticket with a deal

解決

Hello 🙋🏼‍♂️

 

If you have ever struggled with manually adding line items to deal, say hello to the Add Line Item to Deal app we have released!

 

Key Features:

 

- Dynamically include a line item in an already existing deal;

- Capability to insert the following fields: SKU of the product to add, product’s quantity, line item price, currency list (USD, EUR, GBP, etc.).



Install the App: Head to the HubSpot Marketplace, install the Add Line Item to Deal app, and get started for free.



Say goodbye to manually adding line items to deals. 

Try the Add Line Item to Deal today and elevate your HubSpot workflow game!


1589805894166.jpeg

Alessio Mammarella

HubSpot Specialist

Exelab

hs diamondexelab logo
 
 
 
333-6580788
alessio.mammarella@exelab.com
exelab.com

0 いいね!
ArdenM
投稿者

Custom Code - Make a form submission create a ticket and associate that ticket with a deal

解決

To echo CMcKay, there are a lot of moving pieces when it comes to giving advice for building out workflows that are going to be managing an association in your system.

 

The simplest version of this would be that you have either the Deal Id on the form as a mandatory field (unlikely, given that users shouldn't have their own Deal Id for any reason) or some kind of other unique identifier for the deal. In this case, you could make a workflow that triggers whenever a new ticket is created, and associate the ticket to the deal directly using the Deal Id, or by searching for the Deal using the API and then associating them.

 

Alas, that's almost never the case.

 

Assuming that your tickets are already being associated to contacts, more likely, you're going to want to create a ticket property that counts the number of associated contacts (calculated property). Then you create a ticket workflow that is triggered when the number of associated contacts is more than one.

 

Using the Associations API, grab all associated contacts. If there's a way to differentiate which is the most relevant contact, loop through them for that, otherwise I'd take the oldest one by default. Then use the Associations API to get all of that contact's associated deals, and filter through the deals with whatever criteria you're using to determine the most relevant deal. Finally, use the Associations API to associate the ticket with the deal.

This is a highly intensive process, requiring minimum 3 API calls per run, and potentially multiple Custom Code Actions to execute.

As @CMcKay said, if you can be more specific about exactly the process you want here (how are you determining which deal is the right one) we can likely give more detail! Also, if your tickets aren't being automatically associated to contacts, then you'd need to do another step in searching for the contact via the email address on the ticket.

Hope this is at least somewhat helpful!

CMcKay
解決策
トップ投稿者

Custom Code - Make a form submission create a ticket and associate that ticket with a deal

解決

Hey @ah8 ,

 

This is an interesting idea, so a couple of questions for you: 

 

1. What type of form is this? What does it do?

2. How do you want to associate the Deal? Does the form as for a deal ID?

 

The reason I ask these things is because if the form is a ticket form it will already create a ticket for you and bonus if you have a field on the ticket form that's the deal object record id property you can pretty easily create that in a new ticket workflow and just associate the deal with the ticket in a super simple custom code (which I can give you with a loom). 

Let me know.

ah8
投稿者

Custom Code - Make a form submission create a ticket and associate that ticket with a deal

解決

I tried the following code:

 

const hubspot = require('@hubspot/api-client');
const hubspotClient = new hubspot.Client({ accessToken: "access token" });

exports.main = async (event, callback) => {


  const BatchInputPublicAssociationMultiPost = { inputs: [{"_from":{"id":"hs_ticket_id"},"to":{"id":"hs_object_id"},"types":[{"associationCategory":"HUBSPOT_DEFINED","associationTypeId":28}]}] };
  const fromObjectType = "ticket";
  const toObjectType = "deal";

  try {
    const apiResponse = await hubspotClient.crm.associations.batchApi.create(fromObjectType, toObjectType, BatchInputPublicAssociationMultiPost);
    console.log(JSON.stringify(apiResponse, null, 2));
    callback(null, apiResponse);
  } catch (e) {
    e.message === 'HTTP request failed'
      ? console.error(JSON.stringify(e.response, null, 2))
      : console.error(e)
  }
};

 

 

I received this error:

WARNING: The logs for this function have exceeded the 4KB limit.
...
ccurred.
Body: {"status":"error","message":"Invalid input JSON on line 1, column 65. Some required fields were not set: [type]","correlationId":"ee5746e3-451a-4f71-8a0b-597fabe647d8","category":"VALIDATION_ERROR"}
Headers: {"access-control-allow-credentials":"false","alt-svc":"h3=\":443\"; ma=86400","cf-cache-status":"DYNAMIC","cf-ray":"7da4d3faeb118260-IAD","connection":"close","content-length":"198","content-type":"application/json;charset=utf-8","date":"Tue, 20 Jun 2023 14:51:07 GMT","nel":"{\"success_fraction\":0.01,\"report_to\":\"cf-nel\",\"max_age\":604800}","report-to":"{\"endpoints\":[{\"url\":\"https:\\/\\/a.nel.cloudflare.com\\/report\\/v3?s=%2Bs0rt2rOl%2BQFKoBgCxB%2Fji6o6VVcd0TIMZ2sgkYtegT6Gg8C73DTWT2puW6iAehMWvOvSmwSUzhu4CqRen82Ilc%2BgEX%2B8ShF48a9zMGYoeVVZIyfGt4zL43VBmyxjpRv\"}],\"group\":\"cf-nel\",\"max_age\":604800}","server":"cloudflare","strict-transport-security":"max-age=31536000; includeSubDomains; preload","vary":"origin, Accept-Encoding","x-envoy-upstream-service-time":"16","x-evy-trace-listener":"listener_https","x-evy-trace-route-configuration":"listener_https/all","x-evy-trace-route-service-name":"envoyset-translator","x-evy-trace-served-by-pod":"iad02/hubapi-td/envoy-proxy-598c95b5b7-vk5c8","x-evy-trace-virtual-host":"all","x-hubspot-correlation-id":"ee5746e3-451a-4f71-8a0b-597fabe647d8","x-hubspot-ratelimit-daily":"500000","x-hubspot-ratelimit-daily-remaining":"499998","x-hubspot-ratelimit-interval-milliseconds":"10000","x-hubspot-ratelimit-max":"150","x-hubspot-ratelimit-remaining":"149","x-hubspot-ratelimit-secondly":"15","x-hubspot-ratelimit-secondly-remaining":"14","x-request-id":"3698e3b1-7651-46d3-9d8f-e664e6440333"}
    at BatchApiResponseProcessor.<anonymous> (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/associations/apis/BatchApi.js:172:23)
    at Generator.next (<anonymous>)
    at fulfilled (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/associations/apis/BatchApi.js:5:58)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  code: 400,
  body: {
    status: 'error',
    message: 'Invalid input JSON on line 1, column 65. Some required fields were not set: [type]',
    correlationId: 'ee5746e3-451a-4f71-8a0b-597fabe647d8',
    category: 'VALIDATION_ERROR'
  },
  headers: {
    'access-control-allow-credentials': 'false',
    'alt-svc': 'h3=":443"; ma=86400',
    'cf-cache-status': 'DYNAMIC',
    'cf-ray': '7da4d3faeb118260-IAD',
    connection: 'close',
    'content-length': '198',
    'content-type': 'application/json;charset=utf-8',
    date: 'Tue, 20 Jun 2023 14:51:07 GMT',
    nel: '{"success_fraction":0.01,"report_to":"cf-nel","max_age":604800}',
    'report-to': '{"endpoints":[{"url":"https:\\/\\/a.nel.cloudflare.com\\/report\\/v3?s=%2Bs0rt2rOl%2BQFKoBgCxB%2Fji6o6VVcd0TIMZ2sgkYtegT6Gg8C73DTWT2puW6iAehMWvOvSmwSUzhu4CqRen82Ilc%2BgEX%2B8ShF48a9zMGYoeVVZIyfGt4zL43VBmyxjpRv"}],"group":"cf-nel","max_age":604800}',
    server: 'cloudflare',
    'strict-transport-security': 'max-age=31536000; includeSubDomains; preload',
    vary: 'origin, Accept-Encoding',
    'x-envoy-upstream-service-time': '16',
    'x-evy-trace-listener': 'listener_https',
    'x-evy-trace-route-configuration': 'listener_https/all',
    'x-evy-trace-route-service-name': 'envoyset-translator',
    'x-evy-trace-served-by-pod': 'iad02/hubapi-td/envoy-proxy-598c95b5b7-vk5c8',
    'x-evy-trace-virtual-host': 'all',
    'x-hubspot-correlation-id': 'ee5746e3-451a-4f71-8a0b-597fabe647d8',
    'x-hubspot-ratelimit-daily': '500000',
    'x-hubspot-ratelimit-daily-remaining': '499998',
    'x-hubspot-ratelimit-interval-milliseconds': '10000',
    'x-hubspot-ratelimit-max': '150',
    'x-hubspot-ratelimit-remaining': '149',
    'x-hubspot-ratelimit-secondly': '15',
    'x-hubspot-ratelimit-secondly-remaining': '14',
    'x-request-id': '3698e3b1-7651-46d3-9d8f-e664e6440333'
  }
}

 

So, I'm not quite sure what I'm doing wrong. The code would be extremely helpful when you do have the time.

Thank you.

0 いいね!
ArdenM
投稿者

Custom Code - Make a form submission create a ticket and associate that ticket with a deal

解決

Not sure if this is just an artifact of the way you've shared the code, but it doesn't look like you're actually passing your id properties into the custom code action. You'd need to define them as inputs, and then use this code to make them available:

const hs_object_id = event.inputFields['hs_object_id'];
const hs_ticket_id = event.inputFields['hs_ticket_id'];

 Once you've done that at the start of your function, you'd want to change the strings you're currently passing so that they're actually variables.

const BatchInputPublicAssociationMultiPost = { inputs: [{"_from":{"id":"hs_ticket_id"},"to":{"id":"hs_object_id"},"types":[{"associationCategory":"HUBSPOT_DEFINED","associationTypeId":28}]}] };

const BatchInputPublicAssociationMultiPost = { inputs: [{"_from":{"id":hs_ticket_id},"to":{"id":hs_object_id},"types":[{"associationCategory":"HUBSPOT_DEFINED","associationTypeId":28}]}] };

 

I'm also not sure what your input variables would actually be, as it's not clear which variable you're storing your dealId in.

0 いいね!
ah8
投稿者

Custom Code - Make a form submission create a ticket and associate that ticket with a deal

解決

Thank you.
I was using actual values to test for it so I stuck in variable names to ask about it. It reads more like this now:

 

 

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



exports.main = (event, callback) => {
  return callback(processEvent(event));
};


async function processEvent(event) {
  const hubspotClient = new hubspot.Client({
    accessToken: process.env.private_app,
    numberOfApiCallRetries: 3,
  });
  
  const objectID = event.inputFields['hs_object_id'];
  const ticketID = event.inputFields['hs_ticket_id'];

  const BatchInputPublicDefaultAssociationMultiPost = { inputs: [{"_from":{"id":ticketID},"to":{"id":objectID}}] };
  const fromObjectType = "tickets";
  const toObjectType = "deals";

    try {
      const apiResponse = await hubspotClient.crm.associations.batch.read.batchApi.createDefault(fromObjectType, toObjectType, BatchInputPublicDefaultAssociationMultiPost);
      console.log(JSON.stringify(apiResponse, null, 2));
      /*callback(null, apiResponse);*/
    } catch (e) {
      e.message === 'HTTP request failed'
        ? console.error(JSON.stringify(e.response, null, 2))
        : console.error(e)
    }
}

 

 

I believe that the issue I am having stems from the path I am dealing with...
My error says:

ERROR	TypeError: Cannot read properties of undefined (reading 'read')
    at processEvent (/var/task/file.js:25:70)
    at Object.exports.main (/var/task/file.js:7:19)
    at Runtime.exports.hubspot_handler [as handler] (/var/task/hubspotHandler.js:6:21)
    at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1086:29)

I think it is an issue here:
 "hubspotClient.crm.associations.batch.read.batchApi.createDefault"
However, I am not sure how to fix this... 

0 いいね!
ArdenM
投稿者

Custom Code - Make a form submission create a ticket and associate that ticket with a deal

解決

Can you try it again, but change "tickets" and "deals" back to "ticket" and "deal"? You had it like that originally, and I think that's correct. At very least you should get a different error.

0 いいね!
ah8
解決策
投稿者

Custom Code - Make a form submission create a ticket and associate that ticket with a deal

解決

Thank you. I realized that the issue I was facing was that I couldn't do the put request through hubspotClient. I had to use Axios. It works now. I just wish I could pull the secret through hubspotClient without messing up Axios. That's still something for me to work on...

ah8
投稿者

Custom Code - Make a form submission create a ticket and associate that ticket with a deal

解決

@CMcKay  Yes, that is exactly it. The dealID will be a hidden object in the form. Each form is sent with a custom link via email so that we ensure it will be the correct one. I am just working on the custom code action now. I would greatly appreciate the code in the loom you offered.

0 いいね!
ah8
投稿者

Custom Code - Make a form submission create a ticket and associate that ticket with a deal

解決

This form creates a ticket for our customers. We will need a custom ticket property to add to it when the ticket is generated. Ultimately, though, the purpose is to associate the ticket with the deal that is tracking the user.

0 いいね!
PamCotton
コミュニティーマネージャー
コミュニティーマネージャー

Custom Code - Make a form submission create a ticket and associate that ticket with a deal

解決

Hello @ah8 happy Friday!

I want to invite our top experts to this conversation to share their thoughts @CMcKay @FNeri any recommendations to @ah8?

 

Thank you,

Pam

Você sabia que a Comunidade está disponível em outros idiomas?
Participe de conversas regionais, alterando suas configurações de idioma !


Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings !




CMcKay
トップ投稿者

Custom Code - Make a form submission create a ticket and associate that ticket with a deal

解決

Thanks for the invite Pam!

0 いいね!