Skip to main content

Notifications

Supply chain | Supply Chain Management, Commerce
Unanswered

Register the return sales order through code issues.

(2) ShareShare
ReportReport
Posted on by 4
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
  • Martin Dráb Profile Picture
    Martin Dráb 230,370 Most Valuable Professional on at
    Register the return sales order through code issues.
    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

News and Announcements

Announcing Category Subscriptions!

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,359 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,370 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans