web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Question about how to Create Sales Order By API

(5) ShareShare
ReportReport
Posted on by 282
Dear All,
 
I develop Class to create salesorder, I got comment from my partner and research from youtube and internet I got
So when I follow up and test I still have a problem that can't create salesOrder(For Prototype I need only SaleOrder Header) .
------------------------
 I don't know that relate or not that My system I setup salesorder number sequence by Order Pools like this image below
 
Can someone guide me how to do? 
Best Regards,
------------------------
this is my class
 
public str PowerAutomateCreateSOHeader(DataAreaId Company, str CustomerCode, str SalesPoolID)
{
    str returnStr = "";
    SalesTable salesTable;
    SalesId salesID;
    CustTable custTable;
    NumberSeq numberSeq;
    changeCompany(Company)
    {
        try
        {
            ttsbegin;
            // Initialize sales order header
            salesTable.clear();
            salesTable.initValue();
            // Validate customer
            custTable = CustTable::find(CustomerCode, true);
            if (!custTable)
            {
                throw error(strFmt("Customer %1 not found.", CustomerCode));
            }
            // Assign mandatory fields
            salesTable.CustAccount   = CustomerCode;
            salesTable.SalesType     = SalesType::Sales;
            salesTable.SalesPoolID   = SalesPoolID;   // Fixed to 'SB1'
            salesTable.CurrencyCode  = 'THB';         // Example, adjust if needed
            salesTable.InventSiteId  = 'MOT';         // Example, adjust if needed
            // Generate SalesId using number sequence
            numberSeq = NumberSeq::newGetNum(SalesParameters::numRefSalesId(), true);
            if (!numberSeq)
            {
                throw error("Number sequence for SalesId could not be retrieved. Check setup in Sales parameters.");
            }
            salesTable.SalesId = numberSeq.num();
            // Insert sales order header using standard API
            SalesTableForm::salesCreateOrder_WriteServer(
            salesTable,
            '',            // salesAgreementId
            false,         // replacementOrder
            NoYes::No,     // copyRMALines
            false,         // isCopyFromTemplate
            0              // templateRecId
        );
            salesID = salesTable.SalesId;
            ttscommit;
            returnStr = strFmt("Sales order created successfully: %1", salesID);
            info(returnStr);
        }
        catch (Exception::Error)
        {
            ttsabort;
            throw error("Error creating sales order header. Verify number sequence setup and required fields.");
        }
    }
    return returnStr;
}
// end method
Categories:
I have the same question (0)
  • Sohaib Cheema Profile Picture
    49,677 Super User 2026 Season 1 on at
     
    What is your requirement? If you are expecting to have number sequence of Sales Order by Pool Id, that is not available in the standard system.
    The standard system provide you setup of sequence of Sales Order on AR Parameters, where you can change the format, but nothing allows the use of Pools.
    By customization, inside Dynamics, one can create the number sequence as needed.
     
     
  • Martin Dráb Profile Picture
    239,040 Most Valuable Professional on at
    It seems that your actual is about having number sequences by per order pool and the title ("Question about how to Create Sales Order By API") doesn't really describe the topic.
     
    But if I don't ignore the title for a moment, let me ask: Why don't you want to use data entities? They're intended for a scenario like this and you can easily use them from Power Automate through a connector. You don't need to develop anything, because the entity already exists (you could have simply used it instead of tryin g to figure out how to replicate the logic by your own code) and APIs exist too (therefore you don't need to create your own either).
  • André Arnaud de Calavon Profile Picture
    303,730 Super User 2026 Season 1 on at
    Hi Chomchanok,

    The sales order number series per pool per year is a customization. Martin mentioned using the standard data entity, but then that entity should be extended with the logic to get the correct number sequence code to draw a new number.
     
    As we don't know the exact data model and object names of your customization, it is hard to help you. Do you need a number from the current year or based on the (expected) delivery date of the sales order?
     
    If you have development skills or can contact the developer who created this customization, you can find out where and how the current customization is implemented. You can then use the same logic as already implemented in your customization for number sequences.
     
     
  • Verified answer
    Sohaib Cheema Profile Picture
    49,677 Super User 2026 Season 1 on at
    I think I know your problem now. I have executed your code for test and it produced funny results. Look here the order header gets created without customer-Id and other details.
     
     
    And do you know the reasons why? You are missing important methods like salesTable.initFromCustTable();
    You need to pass that before you insert the transaction at SalesTableForm::salesCreateOrder_WriteServer(
     
     
    I would still be interesting to know why you are doing customization when you can do the same thing without code.
  • Chomchanok Profile Picture
    282 on at
    Thank you for all comment,
     
     
    Dear  , For my code it up to a parameter Customer code and salesPoolID, If It can generate new SaleOrder Header that Ok I can continue in a next step. Can you share a resource from your guideline?
    "and do you know the reasons why? You are missing important methods like salesTable.initFromCustTable();
    You need to pass that before you insert the transaction at SalesTableForm::salesCreateOrder_WriteServer("
     
     
    I would still be interesting to know why you are doing customization when you can do the same thing without code.


     
     
    Dear    for this project I plan to connect with power automate with more complex logic but for simple logic need to meet my requirement first.

     
  • Verified answer
    Chomchanok Profile Picture
    282 on at
    Dear All,
     
    Thank you for your comment for now I can continue on this subject for my code when I drill down I found that ex-partner customize to get saleOrder ID with sales pool  for stanard this research can operated.
     
    Thank you
  • Sohaib Cheema Profile Picture
    49,677 Super User 2026 Season 1 on at
    Could you explain or rephrase "Can you share a resource from your guideline"
  • Martin Dráb Profile Picture
    239,040 Most Valuable Professional on at
    Regarding "for this project I plan to connect with power automate with more complex logic but for simple logic need to meet my requirement first":
     
    My point is that the logic already exists and it's better than yours. In my opinion, you're wasting time reinventing the wheel and you'll likely make quite a few other mistakes than just one that Sohaib fixed. Simply using the already existing logic that was tested by many customers and is supported by Microsoft makes much better sense to me. The choice is yours, of course, but maybe you've designed the solution in the wrong way because you don't know the correct solution and you may appreciate the chance to correct before making more investment in the wrong direction.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 658

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 468 Super User 2026 Season 1

#3
Syed Haris Shah Profile Picture

Syed Haris Shah 333 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans