Dynamics 365 API v2: Adding sales lines to an existing sales order - web-services

I am trying to add sales lines to an existing sales order using the standard API included with dynamics 365 business central. However I cannot find the correct syntax on the api request to accomplish this.
I have tried posting to the companies({id})/salesOrderLines endpoint, and the companies({id})/salesOrder({id})/salesOrderLines endpoint but no matter how I try to work my request I always get the "Invalid Request Body" error.
Here is an example of the request I sent to the companies({id})/salesOrderLines endpoint:
Here is my request body:
[
{
"id": "e92c39cb-f552-4d4f-b680-ad7ded2949d0",
"documentId": "e80573b0-9c8b-ed11-bfba-001dd8b71ee3",
"lineType": "Item",
"lineObjectNumber": "H10-110013",
"quantity": 1
},
{
"id": "e92c39cb-f552-4d4f-b680-ad7ded2949d0",
"documentId": "e80573b0-9c8b-ed11-bfba-001dd8b71ee3",
"lineType": "Item",
"lineObjectNumber": "H10-112117",
"quantity": 1
}
]
Here is the response I get back:
"{"error":{"code":"BadRequest","message":"Invalid Request
Body CorrelationId: 241e540a-5af5-4516-83f2-fbc035f80389."}}"
I am able to post a sales order and its lines simultaneously using deep requests but with larger orders they hit the request limit and I need a way of splitting up the lines.

salesOrderLines endpoint expects an object and does not accept collections, so best you can do here is post each line in a separate request.
Request body would look like this:
{
"lineType": "Item",
"lineObjectNumber": "H10-110013",
"quantity": 1
}
id and documentId can be added to the request, but not really required.
Another option is to send the request on the $batch endpoint as described in the docs here: https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/webservices/use-odata-batch
POST http://bc21-dev:7048/bc/api/v2.0/$batch
{
"requests": [
{
"method": "POST",
"url": "http://bc21-dev:7048/bc/api/v2.0/salesOrders(dd3585b3-dd6c-ed11-81b4-6045bd8e5172)/salesOrderLines",
"headers": {
"Company": "CRONUS International Ltd.",
"Content-Type": "application/json"
},
"body": {
"lineType": "Item",
"lineObjectNumber": "1920-S",
"quantity": 2,
"unitPrice": 420.4
}
},
{
"method": "POST",
"url": "http://bc21-dev:7048/bc/api/v2.0/salesOrders(dd3585b3-dd6c-ed11-81b4-6045bd8e5172)/salesOrderLines",
"headers": {
"Company": "CRONUS International Ltd.",
"Content-Type": "application/json"
},
"body": {
"lineType": "Item",
"lineObjectNumber": "1952-W",
"quantity": 1,
"unitPrice": 183.12
}
}
]
}

Related

Facebook Graph API Album Endpoint is deprecated?

I am using the graph api (through android SDK and graph explorer) to get a page's posts, when I get to a post of a page that has multiple images in a post it becomes an album.
Example post response
"message": "Test Multi-Photos",
"full_picture": "",
"from": {
"name": "TestPage",
"id": ""
},
"attachments": {
"data": [
{
"media_type": "album",
"media": {
"image": {
"height": 405,
"src": "",
"width": 720
}
},
"type": "album",
"title": "Photos from TestPage's post",
"target": {
"id": "147710857059579",
"url": ""
}
}
]
},
"id": "_147710857059579"
}
When I try to use the target.id to get the album photos
I believe the url would look like this
https://graph.facebook.com/147710857059579
I get a response saying that the endpoint is deprecated
{
"error": {
"message": "(#12) singular statuses API is deprecated for versions v2.4 and higher",
"type": "OAuthException",
"code": 12,
"fbtrace_id": "AhhiY7ohbb5SoXdFNDZnHhU
"
}
}
Documentation does not say anything about it being deprecated so how do you get the album photos?
To get photos from an album post you have to add subattachments to the attachments fields in the query.
Example using the Android SDK
parameters.putString("fields", "message,full_picture,from,place,attachments{media_type,media,type,title,description,target,subattachments}")
parameters.putString("limit", "10")

Pre-request Postman script for Swagger petstore

Learning Postman, and got some trouble making DELETE call with POST call in pre-request script. Aim is to make only DELETE call without having to make POST call each time. Endpoint is swagger petstore. So, DELETE is for deleting pet by ID and POST (in pre-request) is for creating a pet with required ID.
When I make a POST call separately - it works okay. When POST call is in pre-request - pet is not created. What may be wrong?
Body of separated POST call:
{
"name": "Volodya",
"photoUrls": [
"www.parrots.org/photo1"
],
"id": 202207,
"category": {
"id": 12675,
"name": "Buzza"
},
"tags": [
{
"id": 5566,
"name": "A Scary Mummy"
}
],
"status": "yes"
}
image of POST call
And my pre-request code:
const createPet = {
url: 'https://petstore.swagger.io/v2/pet/202207',
method: 'POST',
header: {
'Content-Type': 'application/json',
'Accept': '*/*'
},
body: {
mode: 'raw',
raw: JSON.stringify({
"name": "Volodya",
"photoUrls": [
"www.parrots.org/photo1"
],
"id": 202207,
"category": {
"id": 12675,
"name": "Buzza"
},
"tags": [
{
"id": 5566,
"name": "A Scary Mummy"
}
],
"status": "yes"
})
}
};
pm.sendRequest(createPet);
Image of pre-request
Thanks for your help.
Found the answer =)
The problem was simple - mistake in endpoint. In original POST request endpoint was https://petstore.swagger.io/v2/pet. But in pre-request code endpoint was https://petstore.swagger.io/v2/pet/202207. So that was the reason.

List users as non admin with custom fields

As per the documentation, I should be able to get a list of users with a custom schema as long as the field in the schema has a value of ALL_DOMAIN_USERS in the readAccessType property. That is the exact set up I have in the admin console; Moreover, when I perform a get request to the schema get endpoint for the schema in question, I get confirmation that the schema fields are set to ALL_DOMAIN_USERS in the readAccessType property.
The problem is when I perform a users list request, I don't get the custom schema in the response. The request is the following:
GET /admin/directory/v1/users?customer=my_customer&projection=full&query=franc&viewType=domain_public
HTTP/1.1
Host: www.googleapis.com
Content-length: 0
Authorization: Bearer fakeTokena0AfH6SMD6jF2DwJbgiDZ
The response I get back is the following:
{
"nextPageToken": "tokenData",
"kind": "admin#directory#users",
"etag": "etagData",
"users": [
{
"externalIds": [
{
"type": "organization",
"value": "value"
}
],
"organizations": [
{
"department": "department",
"customType": "",
"name": "Name",
"title": "Title"
}
],
"kind": "admin#directory#user",
"name": {
"fullName": "Full Name",
"givenName": "Full",
"familyName": "Name"
},
"phones": [
{
"type": "work",
"value": "(999)999-9999"
}
],
"thumbnailPhotoUrl": "https://photolinkurl",
"primaryEmail": "user#domain.com",
"relations": [
{
"type": "manager",
"value": "user#domain.com"
}
],
"emails": [
{
"primary": true,
"address": "user#domain.com"
}
],
"etag": "etagData",
"thumbnailPhotoEtag": "photoEtagData",
"id": "xxxxxxxxxxxxxxxxxx",
"addresses": [
{
"locality": "Locality",
"region": "XX",
"formatted": "999 Some St Some State 99999",
"primary": true,
"streetAddress": "999 Some St",
"postalCode": "99999",
"type": "work"
}
]
}
]
}
However, if I perform the same request with a super admin user, I get an extra property in the response:
"customSchemas": {
"Dir": {
"fieldOne": false,
"fieldTwo": "value",
"fieldThree": value
}
}
My understanding is that I should get the custom schema with a non admin user as long as the custom schema fields are set to be visible by all domain users. This is not happening. I opened a support ticket with G Suite but the guy that provided "support", send me in this direction. I believe this is a bug or maybe I overlooked something.
I contacted G Suite support and in fact, this issue is a domain specific problem.
It took several weeks for the issue to be addressed by the support engineers at Google but it was finally resolved. The behaviour is the intended one now.

JSONAPI: Update relationships including attributes

I have a nested object in my SQLAlchemy table, produced with Marshmallow's nested schema feature. For example, an articles object GET response would include an author (a User type) object along with it.
I know that the JSONAPI spec already allows updating relationships. However, often I would like to update an article as well with its nested objects in one call (POST requests of articles that include a new author will automatically create the author). Is it possible to make a PATCH request that includes the resources of a relationship object that doesn't yet exist?
So instead of just this:
PATCH /articles/1 HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json
{
"data": {
"type": "articles",
"id": "1",
"relationships": {
"author": {
"data": { "type": "people", "id": "1" }
}
}
}
}
It'd be ideal to pass this in to create a new author if there didn't exist one (this is not my actual use case, but I have a similar real life need):
PATCH /articles/1 HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json
{
"data": {
"type": "articles",
"id": "1",
"relationships": {
"author": {
"data": { "type": "people", "id": "1", "attributes": {"name": "new author":, "articles_written": 1} }
}
}
}
}
Is this at all possible or have suggestions on what REST frameworks can support this, or would it be totally against the JSON API spec?
Updating multiple resources at once is not possible with JSON API spec v1.0. But there are several suggestions how to do that. Both official extensions which are not supported anymore aimed to support creating, updating or deleting multiple request at once. Also there is an open pull request which would introduce operations to upcoming JSON API spec v1.2.
For example a request updating two resources at once using suggested operations[1] would look like this:
PATCH /bulk HTTP/1.1
Host: example.org
Content-Type: application/vnd.api+json
{
"operations": [{
"op": "update",
"ref": {
"type": "articles",
"id": "1"
},
"data": {
"type": "articles",
"id": "1",
"attributes": {
"title": "Updated title of articles 1"
}
}
}, {
"op": "update",
"ref": {
"type": "people",
"id": "2"
},
"data": {
"type": "people",
"id": "2",
"attributes": {
"name": "updated name of author 2"
}
}
}]
}
This would update title attribute of article with id 1 and name attribute of person resource with id 2 in a single transaction.
An request to update a to-one relationship and updating the related resource in a singular transaction would look like this:
PATCH /bulk HTTP/1.1
Host: example.org
Content-Type: application/vnd.api+json
{
"operations": [{
"op": "update",
"ref": {
"type": "articles",
"id": "1",
"relationship": "author"
},
"data": {
"type": "people",
"id": "2"
}
}, {
"op": "update",
"ref": {
"type": "people",
"id": "2"
},
"data": {
"type": "people",
"id": "2",
"attributes": {
"name": "updated name of author 2"
}
}
}]
}
This is a request which creates a new person and associate it as author to an existing article with id 1:
PATCH /bulk HTTP/1.1
Host: example.org
Content-Type: application/vnd.api+json
{
"operations": [{
"op": "add",
"ref": {
"type": "people"
},
"data": {
"type": "people",
"lid": "a",
"attributes": {
"name": "name of new person"
}
}
}, {
"op": "update",
"ref": {
"type": "articles",
"id": "1",
"relationship": "author"
},
"data": {
"type": "people",
"lid": "a"
}
}]
}
Note that the order is important. Operations must be performed in order by server. So the resource has to be created before it could be associated to an existing one. To express the relationship we use a local id (lid).
Please note that operations should only be used if a request has to be performed transactionally. If each of the included operations could be executed atomically, singular requests should be used.
Accordingly to implementation list provided on jsonapi.org there are some libraries supporting Patch extension.
Update: Operations were not included in release candidate for JSON API v1.1. It's planned for v1.2 now. The author of the pull request, which is also one of the maintainers of the spec, said that "operations are now the highest priority". A release candidate for v1.2 including Operations may be shipped "RC within a few months of 1.1 final."
[1] Introducing operations to JSON API v1.2 is currently only suggested but not merged. There may be breaking changes. Read the related pull request before implementation.

get Post like count with Facebook AP

i am trying to retrieve a facebook POSTs information (ie: LIKE count) using the facebook API
The URL which used to work was in this format:
https://graph.facebook.com/?ids=[AUTHOR_ID]_[POST_ID]
(author and post ID omitted on purpose)
Now it returns a false data feed. Has the URL structure to retrieve POST information changed?
With October 2013 Breaking Changes, likes.count field is no longer available in the feed.
You need to make a call for each POST_ID with the summary field enabled.
https://graph.facebook.com/POST_ID/likes?summary=true&access_token=XXXXXXXXXXXX
There will be a "summary" element with a "total_count" field.
Nope, that still works for me - just tested with one of my posts:
"id": "<SNIP>",
//removed most fields
"actions": [
{
"name": "Comment",
"link": "https://www.facebook.com/X/posts/Y"
},
{
"name": "Like",
"link": "https://www.facebook.com/X/posts/Y"
}
],
"type": "photo",
"status_type": "added_photos",
"object_id": "SNIP",
"application": {
"name": "Facebook for Android",
"namespace": "fbandroid",
"id": "350685531728"
},
"created_time": "2012-10-12T06:52:10+0000",
"updated_time": "2012-10-12T07:48:34+0000",
"likes": {
"data": [
// four likers' details
],
"count": 28
},
//snip
Check you still have read_stream Permission from your user, that the post hasn't been deleted, etc