QuickBooks API POST using Google Apps Script - post

Does anyone have a working example of doing a POST to the QuickBooks API using Google Apps Script?
I'm trying to create an estimate using the QuickBooks API, however although the request body below works in the API explorer, from within Apps Script I get:
Error: Fetch failed, code: 400, message: {"Fault":{"Error":[{"Message":"Request has invalid or unsupported property","Detail":"Property Name:Unrecognized token 'Line': was expecting ('true', 'false' or 'null')\n specified is unsupported or invalid","code":"2010"}],"type":"ValidationFault"},"time":"2016-08-02T09:51:28.917-07:00"} (line 195, file "Tests")
But I can't see why the API expects a boolean rather than the "Line" key.
This is how I define it as a POST payload in the code:
var payload = {
"Line": [
{
"Id": "3",
"LineNum": 1,
"Amount": 10,
"DetailType": "SalesItemLineDetail",
"SalesItemLineDetail": {
"ItemRef": {
"value": "2",
"name": "Hours"
},
"UnitPrice": 10,
"Qty": 2
}
},
{
"Amount": 10,
"DetailType": "SubTotalLineDetail",
"SubTotalLineDetail": {}
}
],
"TxnTaxDetail": {
"TotalTax": 0
},
"CustomerRef": {
"value": "1",
"name": "Mr Blobby"
},
"CustomerMemo": {
"value": "Thank you for your business and have a great day!"
},
"TotalAmt": 31.5,
"ApplyTaxAfterDiscount": false,
"PrintStatus": "NeedToPrint",
"EmailStatus": "NotSet",
}
var companyId = PropertiesService
.getUserProperties()
.getProperty('QuickBooks.companyId')
var url = 'https://quickbooks.api.intuit.com/v3/company/' + companyId + '/estimate'
var options = {
headers: {
'Accept': 'application/json'
},
contentType: 'application/json',
method: 'post',
payload: payload,
muteHttpExceptions: true,
}
var service = OAuth1_.getService();
var response = service.fetch(url, options)

You need to stringify the whole payload before passing it to the OAuth.fetch() call.
So
var payload = JSON.stringify({
"Line": [
{
"Id": "3",
.
.
})

Related

Get contacts by IDs in batch in Microsoft Graph

I have a list of IDs which I want to pull the corresponding contacts from Microsoft Graph. I tried filtering with https://graph.microsoft.com/v1.0/me/contacts?$filter=Id eq 'my-id-here' or Id eq 'other-id-here' but it says
ErrorInvalidProperty The property 'Id' does not support filtering.
I know than I can query by calling https://graph.microsoft.com/v1.0/me/contacts/my-id-here but I want to request multiple at once to minimize round trips.
You can use batch processing to execute multiple requests. For each request you need to provide a request id, a http method and a request url. Sample request:
URL: https://graph.microsoft.com/v1.0/$batch
Http Method: POST
Request Body:
{
"requests": [
{
"id": "1",
"method": "GET",
"url": "/me/contacts/firstId"
},
{
"id": "2",
"method": "GET",
"url": "/me/contacts/secondId"
}
]
}
Graph will send a response such as:
{
"responses": [
{
"id": "1",
"status": 200,
"headers": {
...
},
"body": {
...
}
},
{
"id": "2",
"status": 200,
"headers": {
...
},
"body": {
...
}
}
]
}
Make sure, that you check for the id of the request, since requests may not be returned in order, in which you sent them to Graph. The Microsoft documentation on batch processing provides more information: here.

Is there a way to add headingId to paragraphs using Google Docs Api?

Problem
If you edit a Google Doc and add TITLE's, HEADING_1's, HEADING_2's etc. and then perform a documents.get request, the JSON response contains headingId's.
Whereas if you use a batchUpdate request with a insertTextRequest and updateParagraphStyleRequest (including, namedStyleType = HEADING_1) and then perform a documents.get request, the JSON response does not contain, headingId's.
I've tried sending the insertTextRequest in a separate batchUpdate request from the updateParagraphStyleRequest. I've also tried sending both together with the insertTextRequest first and last in the requests array as part of the batchUpdate request.
documents.batchUpdate - Request Body:
{
"requests": [
{
"updateParagraphStyle": {
"paragraphStyle": {
"namedStyleType": "HEADING_1"
},
"range": {
"segmentId": "",
"startIndex": 1,
"endIndex": 1
},
"fields": "namedStyleType"
}
},
{
"insertText": {
"location": {
"index": 1,
"segmentId": ""
},
"text": "\nMyText\n"
}
}
]
}
documents.get - Sample Output after the above batchUpdate request:
{
"startIndex": 2,
"endIndex": 9,
"paragraph": {
"elements": [
{
"startIndex": 2,
"endIndex": 9,
"textRun": {
"content": "MyText\n",
"textStyle": {}
}
}
],
"paragraphStyle": {
"namedStyleType": "HEADING_1",
"direction": "LEFT_TO_RIGHT"
}
}
},
documents.get - Sample Output after manually editing the document:
{
"startIndex": 2,
"endIndex": 10,
"paragraph": {
"elements": [
{
"startIndex": 2,
"endIndex": 10,
"textRun": {
"content": "My Text\n",
"textStyle": {}
}
}
],
"paragraphStyle": {
"headingId": "h.3e6t9mhosaga",
"namedStyleType": "HEADING_1",
"direction": "LEFT_TO_RIGHT"
}
}
},
In the documents.get request sample output after manually editing the document there is "headingId": "h.3e6t9mhosaga",.
Why isn't this headingId present after the batchUpdate request?

Microsoft graph batch inserting new task and updating details

Current steps for creating task are
POST /planner/tasks
GET /planner/tasks/{id from post call}/details
PATCH /planner/tasks/{id from post call}/details
If-Match: {etag from get call}
but I want to batch three steps in single call using https://developer.microsoft.com/en-us/graph/docs/concepts/json_batching
And according to odata v4 references http://docs.oasis-open.org/odata/odata-json-format/v4.01/csprd02/odata-json-format-v4.01-csprd02.html#sec_ReferencingNewEntities we can refer entities in same batch call using ${id of other request}
{
"requests": [
{
"id": "task",
"url": "/planner/tasks",
"body": {
"title": "asff",
"appliedCategories": {
"category5": true
},
"planId": "mSV7ODf3g0iTJrUtsNcvHZYAB-ZW",
"bucketId": "WFN6kxMykE-4xxqLUh1uS5YALCWq",
"assignments": {
"4393baf8-8a52-4164-bf93-b1cba5130329": {
"#odata.type": "#microsoft.graph.plannerAssignment",
"orderHint": " !"
}
},
"dueDateTime": "2018-04-23T18:30:00.000Z"
},
"method": "POST",
"headers": {
"Content-Type": "application/json"
}
},
{
"id": "getDetail",
"method": "GET",
"dependsOn": [
"task"
],
"url": "/planner/tasks/$task/details"
},
{
"id": "patchDetail",
"dependsOn": [
"getDetail"
],
"url": "/planner/tasks/$task/details",
"method": "PATCH",
"headers": {
"Content-Type": "application/json",
"if-match": "$getDetail"
},
"body": {
"description": "gwrthbetrhnety"
}
}
}
]
}
but Get details call is failing with error
{
"id": "getDetail",
"status": 400,
"body": {
"error": {
"code": "BadRequest",
"message": "The request URI is not valid. Since the segment 'tasks' refers to a collection, this must be the last segment in the request URI or it must be followed by an function or action that can be bound to it otherwise all intermediate segments must refer to a single resource.",
"innerError": {
"request-id": "a46ce528-993f-4cff-865e-98b2b98d5f23",
"date": "2018-04-17T10:38:29"
}
}
}
}
What I'm doing wrong here
I believe what is missing is the URI (should be POST https://graph.microsoft.com/v1.0/$batch instead of POST /planner/tasks) and "id" field has to be numeric. Check out MS documentation:
https://learn.microsoft.com/en-us/graph/json-batching?context=graph%2Fapi%2F1.0&view=graph-rest-1.0
Here is an example of combining POST and GET in a batch call

Webhook input not available in scripting step?

I have an internal app that uses a webhook listener and some scripting to manipulate the input data. I'm posting this to it:
curl -X POST -d '{
"assignment_id": 12345,
"updated_custom_fields": [{
"name": "RNVIDAYEBB",
"value": "updated!"
},
{
"name": "QUFTXSIBYA",
"value": "and me too"
}
],
"custom_fields": [{
"id": 981,
"name": "RDEXDPVKRD",
"fields": [
{
"id": 4096,
"name": "RNVIDAYEBB",
"default": "EDJEAJICYW",
"required": true,
"value": "Blah"
},
{
"id": 4097,
"name": "QUFTXSIBYA",
"default": "",
"required": true,
"value": ""
}]
}]
}' "https://hooks.zapier.com/hooks/catch/......"
My script is as follows:
update_custom_fields_by_name_pre_write: function(bundle) {
var updatedFields = _.map(bundle.request.data.custom_fields, function(group) {
return _.map(group.fields, function(field) {
return _.extend(field, _.findWhere(bundle.request.data.updated_custom_fields, { name: field.name} ));
});
});
bundle.request.data = updatedFields;
return bundle.request;
}
I know that the merging logic is good, but it appears that the custom_fields and updated_custom_fields arrays are not present in the bundle.request.data object. Anyone know how to get access to them in the script?
It seems like you should be using update_custom_fields_by_name_catch_hook to capture the incoming static webhook data (instead of _pre_write). If you use that, you can capture the data within bundle.cleaned_request.custom_fields and bundle.cleaned_request.updated_custom_fields.

PouchDB not able to insert multiple json files

I am trying to store 3 different JSON Object's using PouchDB,
devices.json:
[
{"id":"001","name":"iPhone","brand":"Apple","tags":["Apple","IOS"],"os":"IOS","Description":"...."},
{"id":"002","name":"galaxy","brand":"Samsung","tags":["Samsung","android"],"os":"android","Description":"...."},
{"id":"003","name":"nexus","brand":"Google","tags":["Google","android"],"Description":"...."},
{"id":"004","name":"galaxy
s4","brand":"Samsung","tags":["Samsung","android"],"os":"android","Description":"...."}
]
brand.json:
[
{ "id": "001", "name": "Apple" },
{ "id": "002", "name": "Samsung" },
{ "id": "003", "name": "Google" } ]
And tags.json as
[
{ "id": "001", "name": "Apple" },
{ "id": "002", "name": "Samsung" },
{ "id": "003", "name": "Google" },
{ "id": "004", "name": "IOS" },
{ "id": "005", "name": "android" } ]
Here when i am inserting using db.bulkDocs() devices.json it's creating 7 tables in one database. Now am trying the same function to insert brands.json & tags.json. But its not able to insert to database.
My javascript code is
var db = new PouchDB('dev');
var toinsert = ["devices","brands","tags"];
toinsert.forEach(function(json){
insert(json);
})
function insert(json){
var param2 = {
url: json + '.json',
dataType: 'json',
cache: false,
type: 'GET'
};
$.ajax(param2)
.done(function(data){
db.bulkDocs(data).then(function (result) {
console.log('Row inserted Successfully');
}).catch(function (err) {
console.log('Unable to insert into DB. Error: ' + err.name + ' - ' + err.message);
});
});
}
Here when i am checking indexedDB i am getting only devices.json data, Others are missing. Here I am not sure weather PouchDB can insert multiple json files or not? If it's Possible using PouchDB, Please helm me to get it.
It seems that the second and third objects are not a valid json object. This could be the issue, check http://jsonlint.com/ to validate your json objects.

Resources