Skip to main content

Notifications

Announcements

No record found.

Business Central forum
Answered

Modifying Ship-to Code on Sales Orders

Posted on by 13
Hi,
 
I have been trying to find a way to POST to a sales order with the standard API to change the Ship-to code.
 
With the standard /salesOrder endpoint there is no field to allow Ship-to Codes to be changed from the generated default.
 
I have exposed the endpoint for /APIV2 - Sales Shipments/ via the Web Services in Dynamics, and it actually has the Ship-to Code field present in the response. However, you can't POST to the endpoint to change the Ship-to Code because it doesn't support insert, even with If-Match set. 
 
I have attempted to make an AL page extension just extending the Sales Order Shipment page to make Editable = true. Didn't work though, and after looking at the official documentation looks like you cannot change a non-editable page to editable with custom extensions. Tried setting InsertAllowed = true as well but it doesn't work for page extensions.
 
Is there any way to change the Ship-to Code, preferably with just using a different standard API endpoint? If not, how would I go about making an extension with AL. I am not very experienced with AL so a bit of break down of how to do it would be great :)
 
 
Categories:
  • Verified answer
    jessb101 Profile Picture
    jessb101 13 on at
    Modifying Ship-to Code on Sales Orders
    Hello,
     
    I have figured out how to change the Ship-to Codes like I needed. Thank you!
     
    I went with the route of using a custom API like Saurav suggested.
     
    After making the custom API, I figured out the issue I ran into with the standard API was that the standard API endpoint for the sales shipments did not return the id for the sales shipment record, therefore I could not POST/PATCH an updated Ship-to Code to a record.
     
    In my custom API, I changed the response to include that id so the Ship-to code could be updated.
     
    Here is what I did:
     
    Using the follow code below I created a custom API based off of the Sales Order Shipment table.
     
    I would HIGHLY suggest you use VS Code with the AL extension developed by Microsoft and the AZ AL Tools extension as it make developing with AL much easier with autofill.
     
    To save someone out there some frustration - some of the tables have different reference names than their SourceTable names. In the UI, the table I used is referred to as 'Sales Order Shipping' or 'APIV2 - Sales Shipments' from the Web Service, but both are referencing the same SourceTable of 'Sales Header' as you can see below.
     
    If you ever need to find the SourceTable name of a different table, in the UI go to your desired table, then click on the '?' at the top of the page, then 'Help and Support', scroll down on the page it opens up and click on 'Inspect pages and data'. A panel will then pop up on the right side and say what SourceTable it is along with the table number.
     
    namespace Microsoft.API.V2;
    
    using Microsoft.Integration.Entity;
    using Microsoft.Sales.Document;
    using Microsoft.Sales.Customer;
    using Microsoft.Finance.Currency;
    using Microsoft.Foundation.PaymentTerms;
    using Microsoft.Foundation.Shipping;
    using Microsoft.Integration.Graph;
    using Microsoft.Sales.History;
    using Microsoft.Sales.Posting;
    using Microsoft.Utilities;
    using Microsoft.API;
    page 50102 "API Sales Order Test"
    {
        PageType = API;
    
        APIVersion = 'v1.0';
        APIPublisher = 'COMPANY';
        APIGroup = 'COMPANYGROUP';
    
        EntityCaption = 'SalesOrderTest2';
        EntitySetCaption = 'SalesOrdersTest2';
        EntityName = 'salesOrderTest2';
        EntitySetName = 'salesOrdersTest2';
    
        ODataKeyFields = SystemId;
        SourceTable = "Sales Header";
    
        Extensible = false;
        DelayedInsert = true;
    
        layout
        {
            area(content)
            {
                repeater(Group)
                {
                    field(id; Rec.SystemId)
                    {
                        Caption = 'Id';
                        Editable = false;
                    }
    
                    field(number; Rec."No.")
                    {
                        Caption = 'No.';
                        
                    }
    
                    field(Ship_to_Code; Rec."Ship-to Code")
                    {
                        Caption = 'Ship-to Code';
                    }
                    
                }
                
            }
        }
    }
     
    There are many different fields you can add to be returned, use the autofill feature with the AL extensions to see what can be returned.
     
    Upload this to your Dynamics environment, and then you can call to it via endpoint. The endpoint is formed https://api.businesscentral.dynamics.com/v2.0/<ENVIRONMENT NAME>/api/<API PUBLISHER>/<APIGROUP>/v1.0/companies(<COMPANY ID>)/<ENTITYSETNAME>
     
    Calling to that endpoint will return every record, so once you find the id returned from a record you want to update, form the endpoint like so https://api.businesscentral.dynamics.com/v2.0/<ENVIRONMENT NAME>/api/<API PUBLISHER>/<APIGROUP>/v1.0/companies(<COMPANY ID>)/<ENTITYSETNAME>(<id>)
     
    To update the Ship-to Code field, patch to the endpoint with the record id, JSON body, and make sure to set the header with If-Match: * and Content-type : application/json as well as pass in the OAUTH token.
     
    Hope this helps!
     
  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 62,018 Super User on at
    Modifying Ship-to Code on Sales Orders
    Hi, I think you can try Bound Actions.
    Dynamics 365 Business Central: How to use Standard/Custom API Bound Actions
     
    Hope this helps.
    Thanks.
    ZHU
  • Suggested answer
    Saurav.Dhyani Profile Picture
    Saurav.Dhyani 11,244 User Group Leader on at
    Modifying Ship-to Code on Sales Orders
    Hi,
     
    You will have to create a custom API Endpoint to add that field in API Page.
     
    Regards,
    Saurav Dhyani
  • gdrenteria Profile Picture
    gdrenteria 3,620 Super User on at
    Modifying Ship-to Code on Sales Orders
    Hi
    Some posted documents can be modified, complying with certain forms.
    Likewise, not all fields can be changed. I hope this information helps you and gives you some ideas.
    BR
    GR

Helpful resources

Quick Links

Can you answer this forum question?

You could make someone's day!

Community Newsletter - May 2024

Kudos to our community stars!

Community Spotlight of the Month

Kudos to Mohamed Amine Mahmoudi!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 283,632 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 224,528 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,146

Featured topics

Product updates

Dynamics 365 release plans