APIs & Integrations

JPs12
Participant | Diamond Partner
Participant | Diamond Partner

Updating line item value though google script editor based on data on google sheets

Hi!

 

Background:

We have developed a calculator in Google Sheets for our team to evaluate how much of work each of our initial offers to our clients would generate. We are populating this data from the Sheets to each Deal's Line Items. Problem is, that this is a manual task and it would be efficient to just update the values by using the API.


Information and the problem:

I have been using the API key with UrlFetchApp.fetch() using the url:

 
The problem is in options {} section of fetch(). I'm having trouble understanding the structure of the options when the method is PATCH and I'm using JS as the language. I have previously managed to work with the GET method to accomplish some minor tasks but PATCH is giving me a headache (not a pro developer obviously).

If you can help me to understand the initial structure of the options {} sections when using the API-key and help me to understand better how this part should be written for it to work properly I would appreciate it tremendously.
 
I'm trying to accomplish this: update the amount and the quantity of the Line Item.
 
Part of the code:

 

  var url = "https://api.hubapi.com/crm/v3/objects/line_items/"+ lineItemID 
  +"?"+ hapikey;

  var options = {
    "method": "PATCH",
    "headers": {
      "content-type": "application/json"
    },
    "body":{
      "properties": {
        "quantity": "2",
        "amount": "600.000"
      }
    },
    //"muteHttpExceptions":true
  }
  
  UrlFetchApp.fetch(url,options);

 

When the mute is off it gives this error message: "Exception: Request failed for https://api.hubapi.com  returned code 400. Truncated server response: {"status":"error","message":"Invalid JSON input: a request body was expected, but none found""

Most likely the reason is super simple but I just could not find anywhere an example of PATCH in JS that was not using oAuth but API-key.

0 Upvotes
3 Replies 3
zaklein
Contributor | Diamond Partner
Contributor | Diamond Partner

Updating line item value though google script editor based on data on google sheets

Hi @JPs12 

Looks like the solution is to send the JSON body as a string (via JSON.stringify() method), and to use the "payload" property name in place of where you've used "body" in your example. This should work within a Google Apps script function:

 

 

var url = "https://api.hubapi.com/crm/v3/objects/line_items/"+ LINE_ITEM_ID + "?hapikey=" + YOUR_HAPIKEY;

var data = {
  "properties": {
    "quantity": "2",
    "amount": "600.000"
  }
}

var options = {
  "method": "patch",
  "contentType": "application/json",
  "payload": JSON.stringify(data)
}

UrlFetchApp.fetch(url,options);

 

 

I came to this conclusion after reading this piece of Google Apps Script documentation on the "UrlFetchApp" class.

 

Hope this helps.

All the best,

Zach

malcolm1
Member | Gold Partner
Member | Gold Partner

Updating line item value though google script editor based on data on google sheets

Headers should probably include content-length of the body but I'd look at the docs for any other clues. 

dennisedson
HubSpot Product Team
HubSpot Product Team

Updating line item value though google script editor based on data on google sheets

Hi @JPs12

How is this coming along? 

@zaklein , @malcolm1 see anything out of sorts here?

0 Upvotes