1
votes

No matter what I do, I can't update the line item values in an inbound shipment record. I have a ton of debug statements, and I know that I technically am at the correct line, but when I try to get or set values, they are empty.

I am working in dynamic mode, and so in order to loop, I found something online that said I needed to stringify the record. I see all the correct data, but any time I try to manipulate the values, it doesn't do anything, or the values are empty. I also keep getting asked to provide the required fields, but for this particular line, they are already there.

Here is my code:

    function doPost(restletBody){

    log.debug('Called from POST', restletBody);

    var success = [],
    errors = [];

    restletBody.data.forEach(function(e) 
    {
        try 
        {
            //Inbound Shipment Stuff
            var ibsID = e.ibShipments.inboundShipmentRecordID;
            var containerNumber = e.ibShipments.containerNumber;
            var memo = e.ibShipments.memo;
            var cargoReturnDate = e.ibShipments.cargoReturnDate;
            var billoflading = e.ibShipments.billoflading;
            var quantityexpected = e.ibShipments.quantityexpected;
            var destCountry = e.ibShipments.destCountry;
            var actualdeliverydate = e.ibShipments.actualdeliverydate;
            var expecteddeliverydate = e.ibShipments.expecteddeliverydate;
            var poDueDate = e.ibShipments.poDueDate;
            var shipmentstatus = e.ibShipments.shipmentstatus;
            var factory = e.ibShipments.factory;
            var vesselNumber = e.ibShipments.vesselNumber;
            var actualshippingdate = e.ibShipments.actualshippingdate;
            var expectedshippingdate = e.ibShipments.expectedshippingdate;
            var vesselBookingNumber = e.ibShipments.vesselBookingNumber;
            var bookingDate = e.ibShipments.bookingDate;

            var recID = null;

            if((ibsID == "" || ibsID == null) && (containerNumber != "" || memo != "" || cargoReturnDate != "" ||
                billoflading != "" || quantityexpected != "" || destCountry != "" || actualdeliverydate != "" ||
                expecteddeliverydate != "" || poDueDate != "" || shipmentstatus != "" || factory != "" || vesselNumber != "" ||
                actualshippingdate != "" || expectedshippingdate != "" || vesselBookingNumber != "" || bookingDate != ""))
            {
                //Create a new record
                log.debug('Create Record', "");
                var rec =
                    r.create({
                        type: "inboundshipment",
                        isDynamic: true,
                        defaultValues: null
                    });

                //Might need to save the record, then add the fields
                recID = rec.save();
                var i = 1;
            }
            else
            {
                log.debug('Update Record Instead', ibsID);
                recID = ibsID;
            }
            var inboundShipmentUpdate = r.load({
                type:   'inboundshipment', 
                id:     recID,
                isDynamic: true
                });

            //Get Values from purchase order
            var poID = e.poID;
            var poName = e.poName;
            var poShipDate = e.poShipDate;
            var poShipWindowStart = e.poShipWindowStart;
            if(poShipWindowStart != "")
                poShipWindowStart = new Date(poShipWindowStart);
            else
                poShipWindowStart = "";

            var poShipWindowEnd = e.poShipWindowEnd;
            if(poShipWindowEnd != "")
                poShipWindowEnd = new Date(poShipWindowEnd);
            else
                poShipWindowEnd = "";

            //Item Fields
            var itemID = e.items.itemID;
            var itemDisplayName = e.items.displayName;
            var itemName = e.items.name;

            //Get Values from inbound shipment entry            
            var shipmentnumber = e.ibShipments.shipmentnumber;
            var bookingDate = e.ibShipments.bookingDate;
            if(bookingDate != "")
                bookingDate = new Date(bookingDate);
            else
                bookingDate = "";
            var vesselBookingNumber = e.ibShipments.vesselBookingNumber;
            var expectedshippingdate = e.ibShipments.expectedshippingdate;
            if(expectedshippingdate != "")
                expectedshippingdate = new Date(expectedshippingdate);
            else
                expectedshippingdate = "";          
            var actualshippingdate = e.ibShipments.actualshippingdate;
            if(actualshippingdate != "")
                actualshippingdate = new Date(actualshippingdate);
            else
                actualshippingdate = "";
            var vesselNumber = e.ibShipments.vesselNumber;
            var containerNumber = e.ibShipments.containerNumber;
            var factory = e.ibShipments.factory;
            var shipmentStatus = e.ibShipments.shipmentstatus;
            var poDueDate = e.ibShipments.poDueDate;
            var expecteddeliverydate = e.ibShipments.expecteddeliverydate;
            if(expecteddeliverydate != "")
                expecteddeliverydate = new Date(expecteddeliverydate);
            else
                expecteddeliverydate = "";
            var actualdeliverydate = e.ibShipments.actualdeliverydate;
            if(actualdeliverydate != "")
                actualdeliverydate = new Date(actualdeliverydate);
            else
                actualdeliverydate = "";
            var destCountry = e.ibShipments.destCountry;
            var quantityexpected = e.ibShipments.quantityexpected;
            var quantityreceived = e.ibShipments.quantityreceived;
            var billoflading = e.ibShipments.billoflading;
            var cargoReturnDate = e.ibShipments.cargoReturnDate;
            if(cargoReturnDate != "")
                cargoReturnDate = new Date(cargoReturnDate);
            else
                cargoReturnDate = "";
            var memo = e.ibShipments.memo;

            //Set Field Values
            inboundShipmentUpdate.setValue('actualshippingdate', actualshippingdate);
            inboundShipmentUpdate.setValue('actualdeliverydate',actualdeliverydate);
            inboundShipmentUpdate.setValue('custrecord_kk_booking_date', bookingDate);
            inboundShipmentUpdate.setValue('custrecord_kk_cargo_return_dt',cargoReturnDate);
            inboundShipmentUpdate.setValue('expectedshippingdate',expectedshippingdate);
            inboundShipmentUpdate.setValue('expecteddeliverydate',expecteddeliverydate);
            inboundShipmentUpdate.setValue('shipmentmemo', memo);           
            inboundShipmentUpdate.setValue('externaldocumentnumber',containerNumber);
            //inboundShipmentUpdate.setValue('shipmentstatus',shipmentStatus);
            inboundShipmentUpdate.setValue('billoflading',billoflading);
            inboundShipmentUpdate.setValue('custrecord_kk_vesselbooking_number',vesselBookingNumber);
            inboundShipmentUpdate.setValue('vesselnumber',vesselNumber);
            inboundShipmentUpdate.setValue('custrecord_kk_destination',destCountry);

            log.debug("Inside Line Items", e.items.itemID  + " " + poID);               

            //The item must be unique, so it cannot already be on the inbound shipment. Do not set the current sublist value to the item
            //if it already exists
            var objCurRecIBS = JSON.parse(JSON.stringify(inboundShipmentUpdate));
            log.debug("objCurRecIBS", objCurRecIBS );
            var objSublistsIBS = objCurRecIBS.sublists ? objCurRecIBS.sublists : '';
            log.debug("objSublistsIBS", objSublistsIBS );
            var objIBSItems = objSublistsIBS.items ? objSublistsIBS.items : '';
            log.debug("objIBSItems", objIBSItems );
            var itemLineCountIBS = Object.keys(objSublistsIBS.items).length; //First Item is an empty item

            var itemPresent = false;

            //First Item is an empty item, so start at 1
            for (var k = 1; k < itemLineCountIBS; k++) 
            {
                //This keeps coming back empty
                var currentQuantity = inboundShipmentUpdate.getCurrentSublistValue({
                    sublistId: "items",
                    fieldId: "quantityexpected"
                });
                log.debug("Current Quantity", currentQuantity + "Line  " + k );

                //So does this
                var currentItem = inboundShipmentUpdate.getCurrentSublistValue({
                    sublistId: "items",
                    fieldId: "shipmentitem"
                });
                log.debug("Current Item", currentItem + "Line  " + k );

                var currItemIDIBS = objIBSItems[Object.keys(objSublistsIBS.items)[k]].itemid;
                log.debug("Current Line Item Id", currItemIDIBS);

                var currentLineIBS = objIBSItems[Object.keys(objSublistsIBS.items)[k]];
                log.debug("cURRENT iTEM IBS Line", currentLineIBS);

                if(currItemIDIBS == itemID)
                {
                    //Update the existing line, since it is already present
                    itemPresent = true;
                    log.debug("Current Item Exists in the IBS", itemPresent);

                    //This is empty, too
                    var linePO = inboundShipmentUpdate.getCurrentSublistValue({
                        sublistId: "items",
                        fieldId: "purchaseorder"
                    });

                    log.debug("Line PO: ", linePO);

                    var currentQuantity = inboundShipmentUpdate.getCurrentSublistValue({
                        sublistId: "items",
                        fieldId: "quantityexpected"
                    });

                    log.debug("Current Quantity", currentQuantity + "Line  " + k );

                    var newQuantity = currentQuantity + 2;

                    log.debug("New Quantity", newQuantity );

                    inboundShipmentUpdate.selectLine({
                        sublistId: 'items',
                        line: k
                    });

                    inboundShipmentUpdate.setCurrentSublistValue({
                        sublistId: "items",
                        fieldId: "quantityexpected",
                        value: newQuantity
                            //quantityexpected
                    });
                    inboundShipmentUpdate.commitLine({sublistId:"items"});
                    log.debug("Inside SELECT Line Items", "Line Committed" );
                }
            }

            if(!itemPresent)
            {
                log.debug("Inside Item Present Check", itemPresent);
                inboundShipmentUpdate.setCurrentSublistValue({
                    sublistId: "items",
                    fieldId: "shipmentitem",
                    value: shipmentitem
                });
                inboundShipmentUpdate.setCurrentSublistValue({
                    sublistId: "items",
                    fieldId: "quantityexpected",
                    value: quantityexpected
                });
                log.debug("Inside Line Items", "Shipment ITEM Added " + shipmentitem );
                inboundShipmentUpdate.commitLine({sublistId:"items"});
                log.debug("Inside Line Items", "Line Committed" );
            }

        ++i;        

        inboundShipmentUpdate.save();

        }
        catch(err) 
        {
            var msg = '';
            log.debug("There was an error", err);
        }
    })
    return "";
}

Does anyone have any idea why everything is coming back empty for me when I try to use NetSuite's methods, but showing correctly when I read the actual record's JSON?

1
//First Item is an empty item, so start at 1 ...can you explain what this means?Krypton
It was me testing some stuff, but whenever I grab the list of items, the first item is an empty line, so it has all of the fields, but empty data. I just skipped it to see if that would work any better.shawleigh17

1 Answers

1
votes

I made this much more difficult than I needed to. I used the findSublistLineWithValue function, and was able to get the correct line value (it WAS 0, for whatever reason the json of the object was coming back with two entries, the first being empty values, so I incorrectly assumed that was line 0). I also had to compare against an actual string instead of a value, or I would get -1.

var lineNumber = inboundShipmentUpdate.findSublistLineWithValue({
                sublistId: 'items',
                fieldId: 'itemid',
                value: itemID.toString()
            });

Once I did this, I was able to get the correct values using my other methods. I just really dumbed down my api calls, and that helped me figure out what was going on.