https://netsuiteprofessionals.com logo
r

Rywin

04/21/2022, 7:43 AM
Good day! I'm transforming my 1.0 User Event script to 2.x for a Sales Order under Before Submit I'm also manipulating the newRecord in getting and setting values I noticed that when I'm saving the Amount Field is being set to null even there is a Value when encoding which leads to an error "Please enter a value for Amount". The Amount / Rate Field is not included in any part in my script in getting and setting values. Am I missing something? Thank you
1
b

battk

04/21/2022, 7:50 AM
what does the code look like
r

Rywin

04/21/2022, 7:55 AM
Copy code
const beforeSubmit = (scriptContext) => {
            var LogTitle = 'beforeSubmit';
            log.debug(LogTitle, LogTitle + LOG_START);
            //Get Type IF CREATE
            if(scriptContext.type == 'create'){
                var SORec = scriptContext.newRecord;
                //Get Subsidiary
                var Subs = SORec.getValue({
                    fieldId : 'subsidiary'
                });
                //IF Subsidiary is CFS
                if(Subs == '1'){
                    //Get IF UI
                    var ctx = runtime.executionContext;
                    if(ctx == "USERINTERFACE"){
                        //Get IF Intecompany
                        var Interco = SORec.getValue({
                            fieldId : 'intercotransaction'
                        });
                        if(!Interco){
                            //Get Sales Rep ID
                            var sALESrEP = SORec.getValue({
                                fieldId : 'salesrep'
                            })
                            //Get Sales Rep Team
                            var emp_team = search.lookupFields({
                                type    : search.Type.EMPLOYEE,
                                id      : sALESrEP,
                                columns : 'custentity3'
                            });
                            log.debug('TEAM',emp_team);
                            //Get Item Line Count
                            var count = SORec.getLineCount({
                                sublistId : 'item'
                            });
                            for(var x = 0; x < count; x++){
                                //Get Item ID
                                var ItemID = SORec.getSublistValue({
                                    sublistId : 'item',
                                    fieldId   : 'item',
                                    line      : x
                                });
                                log.debug('Item ' + x, JSON.stringify(ItemID));
                                //Get Item Type
                                var ItemType = SORec.getSublistValue({
                                    sublistId : 'item',
                                    fieldId   : 'itemtype',
                                    line      : x
                                });
                                //IF Inventory
                                if(ItemType == 'InvtPart'){
                                    //Get Item Name/Code
                                    var LookItem = search.lookupFields({
                                        type    : search.Type.INVENTORY_ITEM,
                                        id      : ItemID,
                                        columns : 'itemid'
                                    });
                                    SORec.setSublistText({
                                        sublistId : 'item',
                                        fieldId   : 'item',
                                        line      : x,
                                        text      : LookItem.itemid
                                    })
                                    var ItemCode = SORec.getSublistText({
                                        sublistId : 'item',
                                        fieldId   : 'item',
                                        line      : x
                                    });
                                    //Get Item Quantity
                                    var qty = SORec.getSublistValue({
                                        sublistId : 'item',
                                        fieldId   : 'quantity',
                                        line      : x
                                    });
                                    //Get Location
                                    var loc = SORec.getSublistValue({
                                        sublistId : 'item',
                                        fieldId   : 'location',
                                        line      : x
                                    });
                                    //Get If Closed
                                    var closed = SORec.getSublistValue({
                                        sublistId : 'item',
                                        fieldId   : 'isclosed',
                                        line      : x
                                    });
                                    //If Closed and Not Dropship Loc
                                    if(loc !== '246'){
                                        if(closed == 'F' || closed == false || closed == null){
                                            //Load Item Record
                                            var ItemRec = record.load({
                                                type : record.Type.INVENTORY_ITEM,
                                                id   : ItemID
                                            });
                                            //Get Item QS
                                            var QS = ItemRec.getValue({
                                                fieldId : salesrep_itemfld_map[emp_team["custentity3"][0].value].qs
                                            });
                                            //Get Item SL
                                            var SL = ItemRec.getValue({
                                                fieldId : salesrep_itemfld_map[emp_team["custentity3"][0].value].ts
                                            });
                                            //Round Off QS / SL
                                            QS = Math.round( parseFloat(QS) * 100) / 100;
                                            SL = Math.round( parseFloat(SL) * 100) / 100;
                                            //Get new QS
                                            var RUE = parseFloat(QS) + parseFloat(qty);
                                            //Get Remaining 
                                            var Rem = parseFloat(SL) - parseFloat(QS);
                                            //If Selling Limit
                                            if(parseFloat(RUE) > parseFloat(SL)){
                                                //Create Error
                                                var SL_CREATE = error.create({
                                                    message   : salesrep_itemfld_map[emp_team["custentity3"][0].value].id + ' -- Item:'+ " " + ItemCode  + " --" + 'Quantity Sold exceeds Selling Limit.Please contact Merchandising Department for adjustments.' + "\n" + "\n" + 'Mayroon ka na lamang ' + Rem + ' kgs na maaaring ibenta para dito ☺',
                                                    name      : 'SL_CREATE',
                                                    notifyOff : true
                                                });
                                                //Throw Error
                                                throw SL_CREATE.message;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
}
@battk
b

battk

04/21/2022, 7:57 AM
that is way too much nesting
a lot of it unnecessary
r

Rywin

04/21/2022, 7:58 AM
Any points that I can improve on? Thank you @battk
b

battk

04/21/2022, 7:59 AM
nested if statements like what you have going increases code complexity
each if creates 2 branches, one for true, one for false
each if nested under that multiples the branches by 2
your branching is mostly unnecessary
r

Rywin

04/21/2022, 8:03 AM
Does these branching that causes why the Amount Value is missing?
b

battk

04/21/2022, 8:04 AM
your first 2 if statements (and a lot of the others) could be combined using a guard clause
Copy code
var SORec = scriptContext.newRecord;
var Subs = SORec.getValue({
  fieldId: "subsidiary",
});

if (scriptContext.type !== "create" || Subs !== "1") {
  return;
}
basically return early
r

Rywin

04/21/2022, 8:05 AM
My version for this in SuiteScript 1.0 is running smoothly, I do not know how does it differ in 2.x?
b

battk

04/21/2022, 8:05 AM
Your amount issue is probably because you are overwriting the item
Copy code
SORec.setSublistText({
  sublistId: "item",
  fieldId: "item",
  line: x,
  text: LookItem.itemid,
});
r

Rywin

04/21/2022, 8:07 AM
what do you suggest in order for me to get the Text or the Item Name?
b

battk

04/21/2022, 8:10 AM
i wouldnt have done it the way you did it, but you already looked up the item code
using search.lookupFields
r

Rywin

04/21/2022, 8:11 AM
Oh I see. Thank you for your feedback @battk
2 Views