I am creating a Item Fulfillment using salesOrder....
# suitescript
g
I am creating a Item Fulfillment using salesOrder.Transform My Goal is to create an IF for each location selected amongst the items. I am testing with a SO that has 2 items in 2 locations. I get One IF, with just the first item. But I am getting the error: You must enter at least one line item for this transaction. for both locations when I try to create a fulfillment. Any advice?
Copy code
for (il = 0; il < items_by_location.length; il++) {
                    var item_loc = ''
                    item_loc = items_by_location[il]

                    if (item_loc.location == loc) {
                        loc_items.push(item_loc)
                    }
                }

                log.debug('total location items: ' + loc_items.length)
                if (loc_items.length == 0) { continue }

                log.debug('loc_items: ', loc_items)
                // var fulfillment = ''
                //transform SO to create a new Item fulfillment
                // try {
                var fulfillment = record.transform({
                    fromType: record.Type.SALES_ORDER,
                    fromId: sales_order.id,
                    toType: record.Type.ITEM_FULFILLMENT,
                    isDynamic: true
                });
                log.debug('got past transform')
                // } catch (err) {
                //     var subj = 'Failed to transform ' + transaction_id + ' to IF'
                //     var msg = err.message
                //     sendEmail_forceSenderReceiver(subj, msg)
                // }
                log.debug('fulfillment', fulfillment)


                //get line count of newly created fulfillment
                var lineCount = fulfillment.getLineCount('item');
                log.debug('fulfillment line count', lineCount)

                for (li = 0; li < loc_items.length; li++) {

                    var loc_item = loc_items[li]
                    log.debug('loc_item', loc_item)
                    if (loc_item.committedQty < loc_item.qty) {
                        log.debug(loc.item_name + ' is on BackOrder. Not Creating fulfillment for these items')
                        email_message += '</ br> Sales Order: ' + sales_order.id + ', Location: ' + loc + ' not creating an IF right now because ' + loc_item.item_name + ' is on backorder.'
                        break
                    }


                    //for each line set the "itemreceive" field to true
                    for (var i = 0; i < lineCount; i++) {
                        log.debug('i in lineCount: ', i)

                        var prod_id = fulfillment.getSublistValue({
                            sublistId: 'item',
                            fieldId: 'item',
                            line: i
                        })
                        log.debug('Item Name: ', prod_id)

                        fulfillment.selectLine({
                            sublistId: 'item',
                            line: i
                        });

                        // let items_location = loc_items.map(a => a.item_id);
                        var items_location = []
                        for (ll = 0; ll < loc_items.length; ll++) {
                            log.debug(ll + ': itemID: ' + loc_items[ll].item_id)
                            items_location.push(loc_items[ll].item_id)
                        }


                        if (!items_location.includes(prod_id)) {
                            continue;
                        }

                        log.debug('setting items to add')

                        fulfillment.setCurrentSublistValue({
                            sublistId: 'item',
                            fieldId: 'itemreceive',
                            value: true
                        });

                        fulfillment.commitLine({
                            sublistId: 'item'
                        });

                        log.debug('commited')
                    } //looping through line items in the SO

                } //loop of location items

                var ful_id = fulfillment.save({ ignoreMandatoryFields: true })
k
have you checked through UI ?
this is just because of inventory not available in specific location
g
@Kevin Both locations selected have inventory available
b
create the item fulfillment in the ui
pay attention to the parameters in the url's query
they are defaultValues values you need to set
e
A cleaner approach would be to build your JSON object for each fulfillment that you need to create before you transform the SO into an IF and set the itemreceive sublist field to false for the ones that should not be included on the IF as you loop through your array of JSON objects. You would need to keep track of the line id for each SO line in your JSON object to know which ones to set to false for itemreceive.
g
@Eric B I am doing it by location... I have an array/obj with items per location and i am looping through those... You are saying I should loop through those and instead of setting what matches to true, set what doest not match to false