i have this code, but it says "The amounts in a jo...
# suitescript
v
i have this code, but it says "The amounts in a journal entry must balance." i am not able to find any thing wrong on this. could anyone please help sme? for(var j = 0; j < (resultArray.length * 2); j++){ journalRecord.selectLine({ sublistId : 'line', line : j }); if(j === 0 || 2){ log.debug('j',j); journalRecord.setCurrentSublistValue({ sublistId : 'line', fieldId : 'account', value : accountID, ignoreFieldChange : true }); if(nonDeductableP < 0) log.debug('less than 0') journalRecord.setCurrentSublistValue({ sublistId : 'line', fieldId : 'debit', value : (nonDeductableP * -1), ignoreFieldChange : true }); }else if (nonDeductableP > 0){ log.debug('greater than 0') journalRecord.setCurrentSublistValue({ sublistId : 'line', fieldId : 'credit', value : nonDeductableP, ignoreFieldChange : true }); } else if (j === 1 || 3){ log.debug('j',j); journalRecord.setCurrentSublistValue({ sublistId : 'line', fieldId : 'account', value : 255, ignoreFieldChange : true }); if (nonDeductableP > 0){ log.debug('greater than 0') journalRecord.setCurrentSublistValue({ sublistId : 'line', fieldId : 'debit', value : nonDeductableP, ignoreFieldChange : true }); }else if (nonDeductableP < 0){ log.debug('less than 0') journalRecord.setCurrentSublistValue({ sublistId : 'line', fieldId : 'credit', value : (nonDeductableP * -1), ignoreFieldChange : true }); } }
s
right off the top, there are problems with this. Both of these if statements: j === 0 || 2 and j === 1 || 3 will always be true. If you don’t understand why, you need to learn more about “truthy” values in Javascript and how the || operator works. Hint: 2 and 3 are both truthy. Since both the if and else if above are always true, then the only logic that matters is whether nonDeductableP is greater than 0 or less than 0 (if it’s equal to 0 nothing happens). When nonDeductableP is greater than 0, you set debit values for each loop iterations, but have no credits to balance them. debits and credits should balance. When nonDeductableP iis less than 0, you set credit values for each loop iterations, but have no debits to balance them. credits and debits should balance. That’s what the error message “The amounts in a journal entry must balance.” is telling you, but the problems are greater than just that.
💯 1
d
Hey, here's how to create a new snippet for posting long scripts. It makes it easy to scroll the channel and also adds syntax highlighting, making the script easier to read 👍
💯 1
v
what could be done to correct this? Actually when i worked for 2 lines , it worked fine.
s
All you did is post some code, so all I can tell you is what “appears” to be wrong with the code, and explain the exact error you provided. At fundamental level, the two if statements make no sense, since they are always true anyway, they may as well be gotten rid of. However, I don’t really believe that is the intention, I think you actually meant for those if statements to do something, but as written they can never be false, and therefore the code will never skip those blocks. I actually suspect that is the reason this code works sometimes, because both of the outer if blocks always run, though if you didn’t intend for that to happen, it’s a problem. What you have not shown or told is what this code is supposed to do, what is the business need being solved or automated by this code. Without knowing that, no one can answer the question of how to correct it, since one needs to first understand what correct means, in other words, the expected outcome. Before coding this, you should go through the UI and create some Journal Entries, trying to manually do what you expect the code to do in. If you are able to succeed manually, then the code is not doing what you think it should. If the manual test fails, then you need to understand how to properly create a Journal Entry first, or the script will have no chance of success.
v
Thank you all, i did it