Monday, November 23, 2020

Sales return order line registration in D365FO and AX 2012

Introduction: 
Sales return order line registration in X++ 

Details: 
Consider SalesReturnOrderRegisterLine is the table where records are with SalesLine referenced. If we are required to register serialized items than salesReturnOrderRegisterLine.inventSerialId will be considered and if item referred with batch then salesReturnOrderRegisterLine.inventBatchId will be considered.
public static void inventoryRegistration(SalesId _salesId)
{
	SalesReturnOrderRegisterLine 	salesReturnOrderRegisterLine;
	SalesLine                       salesLine;
	TmpInventTransWMS               tmpInventTransWMS;
	InventTransWMS_Register         inventTransWMS_Register;
	InventDim                       inventDim;
	InventTrans                     inventTrans;
	VendPackingSlipTrans            vendPackingSlipTransLoc;
	InventTransOrigin               inventTransOrigin;

	while select salesReturnOrderRegisterLine
		 where salesReturnOrderRegisterLine.SalesId == _salesId
	{
		salesLine =  SalesReturnOrderRegister::updateDispositionCode(salesReturnOrderRegisterLine.InventTransId);

		inventTransWMS_Register = InventTransWMS_Register::newStandard(tmpInventTransWMS);

		select firstonly inventTrans
			where inventTrans.StatusReceipt == StatusReceipt::Ordered
			exists join inventTransOrigin
				where inventTransOrigin.RecId == inventTrans.InventTransOrigin
				&&    inventTransOrigin.InventTransId == salesLine.InventTransId
				&&    inventTransOrigin.ReferenceId == _salesId;

		tmpInventTransWMS.clear();
		tmpInventTransWMS.initFromInventTrans(inventTrans);
		tmpInventTransWMS.InventQty     = salesReturnOrderRegisterLine.SalesQty;
		tmpInventTransWMS.LineNum       = int642int(salesLine.LineNum);
		inventDim                       = salesLine.inventDim();
		inventDim.inventSerialId        = salesReturnOrderRegisterLine.inventSerialId;
		inventDim.inventBatchId         = salesReturnOrderRegisterLine.inventBatchId;
		tmpInventTransWMS.InventDimId   = InventDim::findOrCreate(inventDim).inventDimId;
		tmpInventTransWMS.ItemId        = salesLine.ItemId;

		inventTransWMS_Register.writeTmpInventTransWMS(tmpInventTransWMS, inventTrans, InventDim::find(tmpInventTransWMS.InventDimId));

		if (!inventTransWMS_Register.updateInvent(salesLine))
		{
			throw error("Error during sales return order registration");
		}
	}
}

Consider SalesReturnOrderRegister is the class which has below static methods

public static SalesLine updateDispositionCode(InventTransId _inventTransId) { SalesLine salesLine = SalesLine::findInventTransId(_inventTransId, true); ReturnDispositionCode returnDispositionCode; SalesReturnOrderRegister::runPreReturnOrderRegisterLine(salesLine); salesLine.ReturnDispositionCodeId = returnDispositionCode.DispositionCodeId; salesLine.update(); return salesLine; }


public static void runPreReturnOrderRegisterLine(SalesLine _salesLine) { InventTransOriginId salesLineInventTransOriginId; InventTransOriginId reservationLineInventTransOriginId; SalesLine reservationLine; ReturnDispositionCode returnDispositionCode; if (_salesLine.ReturnStatus == ReturnStatusLine::Awaiting) { if (!_salesLine.ReturnAllowReservation && _salesLine.isStocked()) { SalesLine::changeReturnOrderType(_salesLine.InventTransId); _salesLine = SalesLine::findInventTransId(_salesLine.InventTransId, true); } select firstOnly returnDispositionCode where returnDispositionCode.DispositionAction == DispositionAction::Credit; _salesLine.ReturnDispositionCodeId = returnDispositionCode.DispositionCodeId; _salesLine.update(); } else if (_salesLine.ReturnStatus == ReturnStatusLine::Registered) { select forupdate firstonly reservationLine where reservationLine.InventRefTransId == _salesLine.InventTransId; if (reservationLine || _salesLine.qtyMarked()) { if ((_salesLine.returnDispositionCode().DispositionAction == DispositionAction::ReplaceScrap || _salesLine.returnDispositionCode().DispositionAction == DispositionAction::ReturnToCust || _salesLine.returnDispositionCode().DispositionAction == DispositionAction::Scrap)) { if (reservationLine.SalesQty == reservationLine.RemainSalesPhysical) { reservationLineInventTransOriginId = InventTransOriginSalesLine::findInventTransOriginId(reservationLine.DataAreaId, reservationLine.InventTransId); salesLineInventTransOriginId = InventTransOriginSalesLine::findInventTransOriginId(_salesLine.DataAreaId, _salesLine.InventTransId); InventTransOrigin::deleteMarking(salesLineInventTransOriginId, reservationLineInventTransOriginId, -_salesLine.QtyOrdered); reservationLine.delete(); } } else { throw error("@SYS332911"); } } } }

Thanks for reading and stay connected with us for more updates!!!

Jagdish Solanki | Senior Technical Consultant | CloudFronts

Business Empowering Solutions Team

“Solving Complex Business Challenges with Microsoft Dynamics 365 & Power Platform”