How to choose PackingSlipId for CustPackingSlip by code in AX 365

The standard AX procedure allows to manually choose the packingslipId just for VendPackingSlipJour.  To choose the packingSlipId for CustPackingSlipJour in AX 365 is not a trivial task, because developers cannot perform overlayering to change the system behaviour.

Class SalesFormLetter , LetterFormCreate and their subclasses, used by the system to create the custpackingslip,  are very “armored”. I’ve not found any useful hookable method in these classes.

Reading the code into the classes , i noticed the procedure first gets the sequence number for the packingSlipId , later on it initializes the custpackingslipjour tablebuffer, using the field in relative SalesTable record.

Then i decided to add a new field in SalesTable Table called “ChoosenPackingSlipId” and to resolve the task using an event handler ad the end of initialization method of CustPackingslipJour. It is the following:

 [PostHandlerFor(tableStr(CustPackingSlipJour),tableMethodStr(CustPackingSlipJour, initFromSalesTable))]
 public static void CustPackingSlipJour_Post_initFromSalesTable(XppPrePostArgs args)
    SalesTable salestable = Args.getArg("_salesTable");
    CustPackingSlipJour CustpackSlipjour = args.getthis();
    CustPackingSlipJour existentCustpackSlipjour;
    select firstonly existentCustpackSlipjour where existentCustpackSlipjour.PackingSlipId == salestable.ChoosenPackingSlipId;
       CustpackSlipjour.PackingSlipId= salestable.ChoosenPackingSlipId;

So that, when the procedure to create CustPackingSlip is called by code or a user create a Custpackingslip by user side, as result the file custpackingslipId is equal to the field “ChoosenPackingslipId”