Contents

Create Purchase Orders - Confirm - Product receipt - Post using X++ in Dynamics 365 Finance & Operations

Contents

The script will work for Dynamics 365 Finance & Operations version

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
class MaxGeneratePO
{
    public static void main(Args _args)
    {
        int i = 0; // number of purchase orders
        NumberSeq numberSeq;
        PurchTable purchTable;
        PurchLine purchLine;
        InventDim inventDim;

        while (i <= 3)
        {
            ttsBegin;
            MaxGeneratePO createPO = new MaxGeneratePO();
            numberSeq = NumberSeq::newGetNum(PurchParameters::numRefPurchId());
            numberSeq.used();
            purchTable.PurchId = numberSeq.num();
            purchTable.initValue();
            purchTable.initFromVendTable(VendTable::find('US-101'));

            if (!purchTable.validateWrite())
            {
                throw Exception::Error;
            }

            purchTable.insert();
            inventDim.clear();
            purchLine.clear();
            purchLine.initValue();

            purchLine.PurchId = purchTable.PurchId;
            purchLine.ItemId = 'D0002';
            inventDim.InventSiteId = "1";
            inventDim.InventLocationId = "11";
            purchLine.InventDimId=InventDim::findOrCreate(inventDim).inventDimId ;
            purchLine.createLine(true, true, true, true, true, true);

            purchLine.PurchQty = 5;
            purchLine.PurchUnit = "ea";
            purchLine.PurchPrice = createPO.randomAmount(); // get random amount nubmer
            purchLine.LineAmount = purchLine.calcLineAmount();
            purchLine.update();
            
            //PO confirm
            PurchFormLetter purchFormLetter;
            PurchFormLetter purchFormLetterPack;
            purchFormLetter = PurchFormLetter::construct(DocumentStatus::PurchaseOrder);
            purchFormLetter.update(purchTable,
                                strFmt("ConNum_%1", purchTable.PurchId),
                                systemDateGet(),
                                PurchUpdate::All,
                                AccountOrder::None,
                                NoYes::No,
                                NoYes::no);
            //Product receipt
            createPO.proceed(purchTable.PurchId, purchLine.ItemId,purchLine.InventDimId,purchLine.PurchQty,strFmt("RptNum_%1", purchTable.PurchId));
            //Post PO
            createPO.postPOInvoice(purchTable.PurchId, strFmt("RptNum_%1", purchTable.PurchId));

            info(strFmt("Purchase order '%1' has been created", purchTable.PurchId));
            ttsCommit;
            i++;
        }
    }

    public boolean proceed(PurchId _purchId, ItemId _itemId,inventDimId _inventDimId, PurchQty _qty, PackingSlipId _productReceiptNumber)
    {
        return
            this.generateProductReceipt(_purchId, this.addToPurchLineList(_purchId, _itemId, _inventDimId, _qty), _productReceiptNumber);
    }

    public boolean generateProductReceipt(PurchId _purchId, List _purchLineList, PackingSlipId _productReceiptNumber)
    {
        boolean ret = true;
        PurchFormLetter purchFromLetter;
        PurchTable purchTable = PurchTable::find(_purchId);

        try
        {
            ttsbegin;
            purchFromLetter = PurchFormLetter::construct(DocumentStatus::PackingSlip);
            purchFromLetter.createFromLines(true);
            purchFromLetter.parmLineList(_purchLineList.pack());
            purchFromLetter.update(purchTable, _productReceiptNumber,
                                DateTimeUtil::getToday(DateTimeUtil::getUserPreferredTimeZone()),
                                PurchUpdate::All);
            ttscommit;
        }
        catch
        {
            ret = false;
        }
        return ret;
    }

    public List addToPurchLineList(PurchId _purchId, ItemId _itemId,inventDimId _inventDimId, PurchQty _qty)
    {
        List purchLineList = new List(Types::Record);
        PurchLine purchLine = PurchLine::findItemIdInventDimId(_purchId, _itemId, _inventDimId);

        if(purchLine && _qty > 0)
        {
            purchLine.PurchReceivedNow = _qty;
            purchline.modifiedField(fieldNum(PurchLine, PurchReceivedNow));
            purchLineList.addEnd(purchLine);
        }
        return purchLineList;
    }

    public void postPOInvoice(PurchId purchId, PackingSlipId packingSlipId)
    {
        TmpFrmVirtual               tmpFrmVirtualVend;
        PurchFormLetter_Invoice     purchFormLetter;
        VendPackingSlipJour         vendPackingSlipJour;
        SysQueryRun                 chooseLinesQuery;
        SysQueryRun                 chooseLinesPendingInvoiceQuery;
        container                   conTmpFrmVirtual;
        List                        selectedList    = new List(Types::Record);

        select firstonly vendPackingSlipJour
                    where vendPackingSlipJour.PurchId == purchId
            && vendPackingSlipJour.PackingSlipId == packingSlipId;

        if (vendPackingSlipJour)
        {
            tmpFrmVirtualVend.clear();
            tmpFrmVirtualVend.TableNum  = vendPackingSlipJour.TableId;
            tmpFrmVirtualVend.RecordNo  = vendPackingSlipJour.RecId;
            tmpFrmVirtualVend.NoYes     = NoYes::Yes;
            tmpFrmVirtualVend.Id        = vendPackingSlipJour.PurchId;
            tmpFrmVirtualVend.insert();
        }

        chooseLinesQuery = new SysQueryRun(queryStr(PurchUpdate));
        chooseLinesQuery.query().addDataSource(tableNum(VendInvoiceInfoTable)).enabled(false);

        // chooseLinesPendingInvoiceQuery needs to be initialized, although it will not be used
        chooseLinesPendingInvoiceQuery = new SysQueryRun(queryStr(PurchUpdatePendingInvoice));
        chooseLinesPendingInvoiceQuery.query().dataSourceTable(tableNum(PurchTable)).addRange(fieldNum(PurchTable,PurchId)).value(queryValue(''));
           
        purchFormLetter = PurchFormLetter::construct(DocumentStatus::Invoice);
        purchFormLetter.chooseLinesQuery (chooseLinesQuery);
        purchFormLetter.parmQueryChooseLinesPendingInvoice(chooseLinesPendingInvoiceQuery);
        purchFormLetter.purchTable (PurchTable::find(PurchId));
        purchFormLetter.transDate (systemDateGet());
        purchFormLetter.parmParmTableNum (strFmt("%1",packingSlipId)); //This is invoice number
        purchFormLetter.printFormLetter (NoYes::No);
        purchFormLetter.sumBy (AccountOrder::Auto);
        purchFormLetter.specQty (PurchUpdate::PackingSlip);
      
        while select tmpFrmVirtualVend
        {
            selectedList.addEnd(tmpFrmVirtualVend);
            conTmpFrmVirtual = selectedList.pack();
        }
        purchFormLetter.selectFromJournal(conTmpFrmVirtual);
        purchFormLetter.reArrangeNow(true);
        purchFormLetter.run();
    }

    public int randomAmount()
    {
        RandomGenerate randomGenerate;
        randomGenerate = RandomGenerate::construct();
        randomGenerate.parmSeed(new Random().nextInt());
        return RandomGenerate.randomInt(100, 800);
    }

}

Thank you for reading.