Compare two objects in the script

Here, we can have list of the sales order item with quantity and the item fulfillment items with their quantity. We need to compare the items and set the sales order quantity in the item fulfillment object list based on item line number. So, first we need to get the sales order line items as follows.

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

                let quanObj = {};

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

                    let itemLine = record.getSublistValue({

                        sublistId: ‘item’,

                        fieldId: ‘line’,

                        line: i

                    });

                    let quantity = record.getSublistValue({

                        sublistId: ‘item’,

                        fieldId: ‘quantity’,

                        line: i

                    });

                    quanObj[`line_${itemLine}`] = quantity;

                }

Then get the Item fulfillment item object:

 let quanObjIF = {};

let fulfillmentItems = itemFulfillmentRecord.getLineCount({

                        sublistId: ‘item’

                    });

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

                        let itemLine = itemFulfillmentRecord.getSublistValue({

                            sublistId: ‘item’,

                            fieldId: ‘orderline’,

                            line: i

                        });

                        let quantity = itemFulfillmentRecord.getSublistValue({

                            sublistId: ‘item’,

                            fieldId: ‘quantity’,

                            line: i

                        });

                        quanObjIF[`line_${itemLine}`] = quantity;

                    }

Then compare the two objects as follows:

  let resultObject = {};

                    for (let key in quanObjIF) {

                        if (quanObjIF.hasOwnProperty(key) && quanObj.hasOwnProperty(key)) {

                            resultObject[key] = quanObj[key];

                        }

                    }

Leave a comment

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