Skip to main content

Notifications

How to post packing slip or invoice for a particular line in a Sales Order using X++

Requirement:
We have a requirement to post only specified sales lines in a Sales Order from X++. To address this requirement I did my own analysis and achieved this with the below solution. 
 
So lets check what is the solution for this..:)
 
 
Solution:
We can leverage the existing SalesFormLetter class for this requirement. This class is commonly used for posting Sales Orders, and it provides a convenient chooseLinesQuery() method to select specific lines. Simply passing a standard SalesUpdate query with the appropriate filters like the Sales Id and the lines numbers to this method helps us in posting specific lines.
 
 
Explanation:
Here I have created a query object for SalesUpdate query and collected the lines into a container to pass them as a range along with the Sales ID. This combined information is then used to construct a query. Once the query is built, a queryRun object is created and fed it into the chooseLinesQuery() method of the SalesFormLetter class. This method instructs the SalesFormLetter to post only the lines included in the provided range.
 
The below code does the Invoice Posting. Same thing can be used for Packing Slip as well.
 
 
SalesFormLetter salesFormLetter = SalesFormLetter::construct(DocumentStatus::Invoice);

if (this.parmLineNums() != conNull())
{
       Query query = new Query(queryStr(SalesUpdate));
       query.dataSourceTable(tableNum(SalesLine)).addRange(fieldNum(SalesLine, SalesId)).value(salesTable.SalesId);
       query.dataSourceTable(tableNum(SalesLine)).addRange(fieldNum(SalesLine, LineNum)).value(con2Str(this.parmLineNums()));

       SysQueryRun queryRun = new SysQueryRun(query);
       salesFormLetter.chooseLinesQuery(queryRun);
}

salesFormLetter.update(salesTable, DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone()), SalesUpdate::All, AccountOrder::None, NoYes::No, NoYes::No, NoYes::No, NoYes::Yes);
 
Using the chooseLinesQuery() method this scenario can be achieved.
 
 
 
 
PS:- To stick with the discussion, I explained or added the code only w.r.t to posting of specific line. Table buffers like SalesTable which I am using doesn't have initialization here since complete code has not been added.

Comments