Transform item recepit from PO

I want to transform 2 items in PO to one item receipt.

my problem is that when I save the receipt record I am getting an error of

“The record has been deleted since you retrieved it”

looks like its happened when I save the item receipt.

 

my code:

function Run(irList) {
    try {
        

        var poName = irList[Object.keys(irList)[0]]["po"]; 

        var poId = getPO(poName)

        var irRec = context.record.transform({
            fromType: 'purchaseorder',
            fromId: poId,
            toType: 'itemreceipt'
        })

        var count = irRec.getLineCount('item')

        for (; i < irList.length; i++) {
            var irData = irList[i];


            createIR(irData, irRec, count, poId)

            var irId = irRec.save({
                ignoreMandatoryFields: true
            });

    } catch (e) {
        log.error('Run ' + i, e)
        errorsList.push({
            action: 'Run ' + i,
            m: e.message
        })
    }




function createIR(irData, irRec, count, poId) {

//irData = one item object
//irRec = item receipt object (after transform from po)
//count = the number of items in po
//poId = PO id

    try {

        var sublistId = 'item'

        for (var i = 0; i < count; i++) {
            irRec.setSublistValue(sublistId, 'quantity', i, '')
        }

        var itemId = getItemId(irData.item)

        var quantity = irData.quantity


        for (var i = 0; i < count && quantity; i++) {

            var item = irRec.getSublistValue(sublistId, 'item', i)

            if (item == itemId) {

                var qtyRem = irRec.getSublistValue(sublistId,
                    'quantityremaining', i)

                var diff = qtyRem - quantity

                if (diff >= 0) {
                    irRec.setSublistValue(sublistId, 'quantity', i, quantity)
                    break;
                } else {
                    quantity -= qtyRem
                    irRec.setSublistValue(sublistId, 'quantity', i, qtyRem)
                }
            }
        }

        var irId = irRec.save({
            ignoreMandatoryFields: true
        });
Rookie Asked on May 27, 2021 in SuiteScript.
Add Comment
1 Answer(s)

You cant save the same record multiple times. Save the item receipt once, after you are done making all your changes. Do not save it in between for loop iterations in your run function; do not save it in your createIR function if you are going to continue making changes on other lines.

I can’t really tell if that itself is the problem since the code you shared is missing parts

Advanced Answered on May 27, 2021.

I get this, but if i save the record outside the loop its save only the last line. (I have 2 items to insert and its saves only the last item)

on May 27, 2021.

Again, I only have fragments of your code. As far as I can tell, your code is exlicitly designed to only set one line per item receipt and then save it. For example running the following multiple times

for (var i = 0; i < count; i++) {
  irRec.setSublistValue(sublistId, "quantity", i, "");
}
is code designed to make it so that you only have one item on your item receipt.

Same thing for your multiple attempts at saving the item receipt. You cannot save the item receipt before you are done setting all the items, you can only save it once.

on May 28, 2021.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.
  • This site made possible by our sponsors:   Tipalti   Celigo   Limebox   Become a Sponsor   Become a Sponsor