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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Supply chain | Supply Chain Management, Commerce
Unanswered

Register the return sales order through code issues.

(4) ShareShare
ReportReport
Posted on by 55
I got this code from forums. but i try to run it.. it gives errors 
 
internal final class runnableclass2
{
    public static void Main(Args args)
    {
        Integer                                 lastRec = 0;
        SalesId                                 oldSalesId;
        inventTrans                             inventTrans;
        SalesFormLetter                         salesFormLetter;
        inventTransOrigin                       inventTransOrigin;
        TradeOrderLineRegister                  tradeOrderlineRegister;
        SalesReturnOrderLineRegister        salesReturnOrderLineRegister;
        InventTransWMS_Register                 inventTransWMS_register;
        TmpInventTransWMS                       TmpInventTransWMS;
        SalesLine                               salesLineReg;
        ReturnDispositionCode                   returnDispositionCode;
        //SON_IPB49_InventStkTransHeader          headerTable,headerTableLoc;
        Description255                          errorMsg;
            ttsBegin;
            select firstOnly * from returnDispositionCode
            where returnDispositionCode.DispositionAction == DispositionAction::Credit;
            select forUpdate salesLineReg
            where salesLineReg.SalesId  == "xxx";/*salesLineReg.SalesId
            && _salestable.SalesType == SalesType::ReturnItem*/

            args.record(salesLineReg);
            tradeOrderLineRegister = salesReturnOrderLineRegister::construct();
            tradeOrderLineRegister.parmArgs(args);
            tradeOrderLineRegister.init();
            salesReturnOrderLineRegister = tradeOrderLineRegister;
            salesReturnOrderLineRegister.runPreSuper();
            //update returnDisposioncode
            salesLineReg.ReturnDispositionCodeId = returnDispositionCode.DispositionCodeId ;  //returnDispositionCode.DispositionCodeId;
            salesLineReg.update();
            select firstOnly crossCompany inventTrans
                    join RecId,InventTransId from inventTransOrigin
                        where inventTransOrigin.InventTransId == salesLineReg.InventTransId
                        && inventTrans.InventTransOrigin == inventTransOrigin.RecId;
            inventTransWMS_register = inventTransWMS_register::newStandard(tmpInventTransWMS);
           // inventDim   = inventTranslocal.inventDim();
            InventDim inventDim = inventTrans.inventDim();
            inventDim.inventBatchId = "BatchNew";
            inventDim.InventLocationId =  "xxx";
            inventDim.wMSLocationId = "NA";
            inventDim.InventSiteId = "xxx";
            inventDim.InventStatusId = "AL";
            inventDim = InventDim::findOrCreate(inventDim);
            inventTrans.InventDimId = inventDim.inventDimId;
            tmpInventTransWMS.clear();
            tmpInventTransWMS.initFromInventTrans(inventTrans);
            tmpInventTransWMS.InventDimId = inventTrans.InventDimId;
            tmpInventTransWMS.InventQty = 1;
            tmpInventTransWMS.insert();
            
            inventTrans.selectForUpdate(true);
            inventDim.selectForUpdate(true);
        salesLineReg.selectForUpdate(true);
            
            
            inventTransWMS_register.writeTmpInventTransWMS(tmpInventTransWMS,
                                                               inventTrans,
                                                               inventDim);
            inventTransWMS_register.updateInvent(salesLineReg);
              //  inventTransWMS_register.updateInvent(inventTrans);
            ttscommit;
            Info("completed");
        }
 
 

Site=SYD,Warehouse=GS1,Location=NA,Inventory status=AL,Batch number=BatchNew
Cannot edit a record in Batches (InventBatch). The record has never been selected.
 
Any how to fix this? I have not selected Invent batch table or passed it as argument
I have the same question (0)
  • Martin Dráb Profile Picture
    240,014 Most Valuable Professional on at
    Where is the exception thrown from?
     
    Note that several parts of this code look suspicious at best. For example, you update just one of sales lines, the first one returned by the query. Selecting inventDim record for update makes no sense, because InventDim mustn't be updated. All validations are skipped, therefore it can produce invalid records. And so on. Don't use this code sample for anything real until you improve it to production-level quality.
     
    Moved from D365 general forum, because it's clearly about F&O and not about D365 in general.

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Supply chain | Supply Chain Management, Commerce

#1
André Arnaud de Calavon Profile Picture

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

#2
Laurens vd Tang Profile Picture

Laurens vd Tang 71 Super User 2026 Season 1

#3
Sagar Suman Profile Picture

Sagar Suman 56 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans