Create Item fulfillment if the sales order has multiple location

We have recall the same function to multiple IFs creation. 

 IFRecordObj = createIFrecord(salesOrderInternalID, transactionDate, IFStatus, reduceContext, salesOrderNO);

                if (IFRecordObj && IFRecordObj.isMutipleLocation) {

                    // If isMutipleLocation is true, call createIFrecord again

                    let IFRecordObjNew = createIFrecord(salesOrderInternalID, transactionDate, IFStatus, reduceContext, salesOrderNO);

                    if (IFRecordObjNew.key == ‘error’) {

                        reduceContext.write({

                            key: ‘error’,

                            value: { salesOrderInternalID, salesOrderNO, ‘error’: IFRecordObj.error }

                        });

                    }

const createIFrecord = (salesOrderInternalID, transactionDate, IFStatus, reduceContext, salesOrderNO) => {

            try {

                log.debug(“reduce context”, reduceContext)

                let IFRecordObj = record.transform({

                    fromType: record.Type.SALES_ORDER,

                    fromId: salesOrderInternalID,

                    toType: record.Type.ITEM_FULFILLMENT,

                    isDynamic: true

                });

                log.debug(“transform obj”, IFRecordObj);

                IFRecordObj.setValue({

                    fieldId: ‘trandate’,

                    value: new Date(transactionDate)

                });

                IFRecordObj.setValue({

                    fieldId: ‘shipstatus’,

                    value: IFStatus

                });

                let lineCount = IFRecordObj.getLineCount({ sublistId: ‘item’ });

                log.debug(“line count”, lineCount);

                let flag = 0;

                let firstLocation;

                let isMutipleLocation = false;

                for (let i = 0; i < lineCount; i++) {

                    IFRecordObj.selectLine({ sublistId: ‘item’, line: i })

                    let itemType = IFRecordObj.getCurrentSublistValue({

                        sublistId: ‘item’,

                        fieldId: ‘itemtype’,

                        line: i

                    });

                    let location = IFRecordObj.getCurrentSublistValue({

                        sublistId: ‘item’,

                        fieldId: ‘location’,

                        line: i

                    });

                    if (i == 0) {

                        firstLocation = location;

                    }

                    log.debug(“itemtype”, firstLocation);

                    if (itemType == ‘NonInvtPart’ && location == firstLocation) {

                        flag += 1;

                        IFRecordObj.setCurrentSublistValue({

                            sublistId: ‘item’,

                            fieldId: ‘itemreceive’,

                            value: true,

                            line: i

                        });

                    }

                    else {

                        isMutipleLocation = true;

                        IFRecordObj.setCurrentSublistValue({

                            sublistId: ‘item’,

                            fieldId: ‘itemreceive’,

                            value: false,

                            line: i

                        });

                    }

                    IFRecordObj.commitLine({ sublistId: ‘item’ });

                }

                if (flag < 0) {

                    let e = {}

                    e.message = “There is no item to fulfill or either the item is already picked or packed. So, the IF record is already exist for that item.”;

                    return { key: ‘error’, error: e };

                }

                else {

                    let IFinternalID = IFRecordObj.save();

                    return { IFinternalID, isMutipleLocation };

                }

            }

            catch (e) {

                log.error(“error@createIFrecord”, e);

                return { key: ‘error’, error: e };

            }

        }

Leave a comment

Your email address will not be published. Required fields are marked *