Skip to main content

Notifications

Announcements

No record found.

Supply Chain Management forum
Unanswered

Register the return sales order through code issues.

Posted on by 2
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 228,501 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

Quick Links

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,532 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,501 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans