Can anyone help me? I've got what seems to be jus...
# suitescript
m
Can anyone help me? I've got what seems to be just a basic 'math doesn't work' in a conditional.
Copy code
var marginDelta = (parseFloat(fbaContributionMarginAmt) - parseFloat(fbmContributionMarginAmt)).toFixed(2);

    logModule.debug({title: 'Margin Delta', details: marginDelta});
    logModule.debug({title: 'FBM Margin', details: fbmContributionMarginAmt});
    logModule.debug({title: 'FBA Margin', details: fbaContributionMarginAmt});

if (fbaContributionMarginAmt > fbmContributionMarginAmt) {  //in example if 5.73 > 10.02 which... it's not.
    rec.setValue({fieldId: 'custrecord_br_cst1_margin_review', value: true}); //box is being checked
    rec.setValue({fieldId: 'custrecord_br_cst1_memo', value: 'Difference of: ' + marginDelta}); //gives a negative value
}
Log: Title Details FBA Margin 5.73 FBM Margin 10.02 Margin Delta -4.29 But it's filling the fields with:
message has been deleted
b
the guess is that fbmContributionMarginAmt and fbaContributionMarginAmt are strings
are you are doing a string comparison instead of a number comparison
m
They're formatted with parseFloat earlier in the code. I've done them as parseFloat at the conditional level with the same result... Should have mentioned. And they are subtracting to give the -4.29. Strangely, if our test margins are both negative, it works fine.
b
do
Copy code
var fbaContributionMarginAmt = 5.73;
var fbmContributionMarginAmt = 10.02;

var marginDelta = (fbaContributionMarginAmt - fbmContributionMarginAmt).toFixed(2);

    logModule.debug({title: 'Margin Delta', details: marginDelta});
    logModule.debug({title: 'FBM Margin', details: fbmContributionMarginAmt});
    logModule.debug({title: 'FBA Margin', details: fbaContributionMarginAmt});

if (fbaContributionMarginAmt > fbmContributionMarginAmt) {  //in example if 5.73 > 10.02 which... it's not.
    rec.setValue({fieldId: 'custrecord_br_cst1_margin_review', value: true}); //box is being checked
    rec.setValue({fieldId: 'custrecord_br_cst1_memo', value: 'Difference of: ' + marginDelta}); //gives a negative value
}
m
Yeah. That worked.
s
You didn't format them with parseFloat, you temporarily casted them
m
huh. I've never heard of that. I have research to do!
b
you need to share more code to see whats wrong
m
I think I understand. Is it that they didn't maintain their 'formatting' (casting) inside the conditional loop?
b
but either you dont know how to tell the difference between a string and a number
m
Because I just re-cast(?) them and it worked when I did it inside the conditional
b
or you dont know how toFixed works
m
I don't know how toFixed works. As soon as I clicked that link I realized.
Learning is fun. Thank you both!
b
you also generally dont want to use parseFloat or parseInt on Numbers
parseInt in particular will parse exponents wrong
m
Oh, I totally see that now. It's all Math. I've been turning them back and forth into strings!
The mind boggles
Need to find me my derp hat.