I am trying to refactor my code so I dont have to ...
# suitescript
k
I am trying to refactor my code so I dont have to write 95 setSublistValues. is there a shorter way to do this
Copy code
const beforeSubmit = (scriptContext) => {

    var itemId = scriptContext.newRecord.id;
    var item = scriptContext.newRecord;

    var priceID = 'price1'

    var basePrice = item.getValue({
        fieldId: 'custitem6'
    }); //64

    var onefourtyN = item.getValue({
        fieldId: 'custitem7'
    }) ; //17

    var fourtyEightN = item.getValue({
        fieldId: 'custitem8'
    }); //39

    var seventyTwoN = item.getValue({
        fieldId: 'custitem9'
    }); //60

    var folletN = item.getValue({
        fieldId: 'custitem10'
    }); //90

    var mDenPrice = item.getValue({
        fieldId: 'custitem12'
    })

    var rallyHousePrice = item.getValue({
        fieldId:'custitem13'
    })

    var fanaticsPrice = item.getValue({
        fieldId: 'custitem14'
    })

    var fanInVenue = item.getValue({
        fieldId: 'custitem15'
    })

    var flcPrice = item.getValue({
        fieldId: 'custitem11'
    })




    var sublistValueText = item.getSublistText({
        sublistId: priceID,
        fieldId: 'pricelevel',
        line: 1
    })

   var objField =  item.getSublistField({
        sublistId: priceID,
        fieldId: 'pricelevel',
        line: 5
    })



   for( var i = 0; i <= 21 ; i++){

       var sublistValue = item.getSublistValue({
           sublistId: priceID,
           fieldId: 'pricelevel',
           line: i
       })
       if(sublistValue == "1"){
           var number = onefourtyN * 1.111111
           item.setSublistValue({
               sublistId:priceID,
               fieldId:'price_1_',
               line: i,
               value: number.toFixed(2)
           })
       }else if (sublistValue == "2"){
           var number = onefourtyN * 1.123596
           item.setSublistValue({
               sublistId:priceID,
               fieldId:'price_1_',
               line: i,
               value: number.toFixed(2)
           })
       }else if(sublistValue == "3"){
           var number = onefourtyN * 1.136364
           item.setSublistValue({
               sublistId:priceID,
               fieldId:'price_1_',
               line: i,
               value: number.toFixed(2)
           })
       }else if(sublistValue == "4"){
           var number = onefourtyN * 1.136364
           item.setSublistValue({
               sublistId:priceID,
               fieldId:'price_1_',
               line: i,
               value: number.toFixed(2)
           })
       }else if(sublistValue == "5"){
           var number = onefourtyN * 1.149425
           item.setSublistValue({
               sublistId:priceID,
               fieldId:'price_1_',
               line: i,
               value: number.toFixed(2)
           })
       }else if(sublistValue == "6"){
           var number = onefourtyN * 1.162791
           item.setSublistValue({
               sublistId:priceID,
               fieldId:'price_1_',
               line: i,
               value: number.toFixed(2)
           })
       }else if(sublistValue == "7"){
           var number = onefourtyN * 1.176471
           item.setSublistValue({
               sublistId:priceID,
               fieldId:'price_1_',
               line: i,
               value: number.toFixed(2)
           })
       }else if(sublistValue == "8"){
           var number = onefourtyN * 1.190476
           item.setSublistValue({
               sublistId:priceID,
               fieldId:'price_1_',
               line: i,
               value: number.toFixed(2)
           })
       }else if(sublistValue =="9"){
           var number = onefourtyN * 1.204819
           item.setSublistValue({
               sublistId:priceID,
               fieldId:'price_1_',
               line: i,
               value: number.toFixed(2)
           })
       }else if(sublistValue == "10"){
           var number = onefourtyN * 1.219512
           item.setSublistValue({
               sublistId:priceID,
               fieldId:'price_1_',
               line: i,
               value: number.toFixed(2)
           })
       }else if(sublistValue == "11"){
           var number = onefourtyN * 1.25
           item.setSublistValue({
               sublistId:priceID,
               fieldId:'price_1_',
               line: i,
               value: number.toFixed(2)
           })
       }else if(sublistValue == "12"){
           var number = onefourtyN * 1.265823
           item.setSublistValue({
               sublistId:priceID,
               fieldId:'price_1_',
               line: i,
               value: number.toFixed(2)
           })
       }else if(sublistValue == "13"){
           var number = onefourtyN * 1.315789
           item.setSublistValue({
               sublistId:priceID,
               fieldId:'price_1_',
               line: i,
               value: number.toFixed(2)
           })
       }else if(sublistValue == "14"){
           var number = onefourtyN * 1.333333
           item.setSublistValue({
               sublistId:priceID,
               fieldId:'price_1_',
               line: i,
               value: number.toFixed(2)
           })
       }else if(sublistValue == "15"){
           var number = onefourtyN * 1.081081
           item.setSublistValue({
               sublistId:priceID,
               fieldId:'price_1_',
               line: i,
               value: number.toFixed(2)
           })
       }else if(sublistValue == "16"){
           var number = onefourtyN * 1.315789
           item.setSublistValue({
               sublistId:priceID,
               fieldId:'price_1_',
               line: i,
               value: number.toFixed(2)
           })
       }else if(sublistValue =="17"){
           var number = onefourtyN * 1.086957
           item.setSublistValue({
               sublistId:priceID,
               fieldId:'price_1_',
               line: i,
               value: number.toFixed(2)
           })
       }else if(sublistValue == "18"){
           var number = onefourtyN * 1.098901
           item.setSublistValue({
               sublistId:priceID,
               fieldId:'price_1_',
               line: i,
               value: number.toFixed(2)
           })
       }else if(sublistValue == "19"){
           var number = onefourtyN
           item.setSublistValue({
               sublistId:priceID,
               fieldId:'price_1_',
               line: i,
               value: number.toFixed(2)
           })
       }else if(sublistValue == "20"){
           var number = onefourtyN * 1.1
           item.setSublistValue({
               sublistId:priceID,
               fieldId:'price_1_',
               line: i,
               value: number.toFixed(2)
           })
       }else if(sublistValue == "21"){
           var number = onefourtyN * 1.07
           item.setSublistValue({
               sublistId:priceID,
               fieldId:'price_1_',
               line: i,
               value: number.toFixed(2)
           })
       }else if(sublistValue == "22"){
           var number = fourtyEightN * 1.111111
           item.setSublistValue({
               sublistId:priceID,
               fieldId:'price_1_',
               line: i,
               value: number.toFixed(2)
           })
       }else if(sublistValue == "23"){
           //48 11%
           var number = fourtyEightN * 1.123596
           item.setSublistValue({
               sublistId:priceID,
               fieldId:'price_1_',
               line: i,
               value: number.toFixed(2)
           })
       }else if(sublistValue == "24"){
           var number = fourtyEightN * 1.136364
           item.setSublistValue({
               sublistId:priceID,
               fieldId:'price_1_',
               line: i,
               value: number.toFixed(2)
           })
       }else if(sublistValue == "25"){
           var number = fourtyEightN * 1.149425
           item.setSublistValue({
               sublistId:priceID,
               fieldId:'price_1_',
               line: i,
               value: number.toFixed(2)
           })
       }else if(sublistValue == "21"){
           var number = onefourtyN * 1.07
           item.setSublistValue({
               sublistId:priceID,
               fieldId:'price_1_',
               line: i,
               value: number.toFixed(2)
           })
       }

   }


}
s
This is going to be hard, for a lot of reasons. Where to start: The “number” that is computed is based off of two separate values, one a constant, the other one of multiple variables. You could easily make a map of keys to constant values, but would still need separate logic to determine which variable to multiply. Look into concept like data-oriented design as they would be very useful here. Also, the field ids here are not helping with understanding the code. I’d strongly suggest changing custitem7, custitem8, price_1_, etc. to have script ids that reflect what they really are are. It will help prevent bugs, and you’ll be glad you did it (hopefully these are not legacy values that have already been referenced in other scripts, in which case you may be out of luck). Also, naming variables things like “sublistValue” or “number” is equally confusing, as it doesn’t describe what those values are. One easy simplification: separate the computing of the “number” from the setting of the sublist value, since that part is the same for each if block
👍 1
e
I would do something like this
Copy code
var lookup = {
  "1": onefourtyN * 1.111111,
  ...
  "19": onefourtyN * 1,
 ...
  "22": fourtyEightN * 1.111111,
etc
};

for (var i = 0; i <= 21; i++) {
  var sublistValue = item.getSublistValue({
    sublistId: priceID,
    fieldId: 'pricelevel',
    line: i
  });

  var multipliedVal = lookup[sublistValue];
  if (multipliedVal) {   
    item.setSublistValue({
      sublistId: priceID,
      fieldId: 'price_1_',
      line: i,
      value: multipliedVal.toFixed(2)
    });
  }
}
s
Agree with @ehcanadian, though youd probably have to make lookup a function, and pass in all possible variables needed (onefourtyN, fourtyEightN, sublistValue, etc.), since the variables may be out of scope otherwise
e
that would be better
r
toFixed
is unreliable. Use exponential notation. https://www.jacklmoore.com/notes/rounding-in-javascript/
s
Agreed, toFixed has actually reared it’s ugly head on us in production (not NetSuite, but a customer-facing application) and lead to the wrong rates being displayed. Either use the solution by @reptar or use a library like big.js that has an improved version of toFixed
e
Why does exponential notation increase the accuracy?
r
@erictgrubaugh I haven't read the algorithms, but there are lots of examples of 3digit→2digit where toFixed fails to accurately round. some are on the link i shared. if there are edge cases where exponential doesnt work, im not aware of them.
e
Oh I'm absolutely not arguing about `toFixed()`; we use
BigNumber.js
to overcome it. I'm just curious why exponential works. I certainly haven't dug in to the algorithms either
👍 1
It feels to me like exponential notation would sort of just be a convenience abstraction but still use IEEE floats underneath, but that's a completely uneducated guess
How I might start out refactoring this, trying to make it a bit more declarative and employing the rounding strategy linked above. My interpretation of what's happening is that you have a long list of Price Levels, but the only difference between each Price Level is the Base Price and a multiplicative Factor. Looking at the patterns of these base prices and factors, there's probably even a higher level of abstraction/declarations I could make if I saw all 95 calculations, but this seems like a decent start. Note line 36 that if a mapping isn't found for a Price Level, I default the base price to
0
just to prevent an error, but y'all probably have a more sensible default value for your pricing.
👍 2
In Production code I'd likely have an enumeration of the Price Levels to give them more human-readable names instead of
1
,
2
, ...
22
,
23
, ...
With this code, you would only need to fill out
baseFields
and
levelToPrice
with the appropriate values, and the rest of the work should be done for you
b
my vote is to put the price level, multiplier, and which custom item field to use in a custom record list that your script searches for to find the matching numbers
allows someone to make changes without touching the script