Reputation: 31
I'm working on creating a journal entry from an invoice if the invoice is checkmarked on a field. This is a little more complicated than I'm used to, so I wanted to ask for a little guidance.
What I need to do is: afterSubmit(){}:
The parts I'm not sure about:
Is getValue the best way to retrieve the line item data? Do I have an object at this point that I can iterate through? How do I look at this object with a logDebug() or the debugger? Or if there is another way to get the sublist, how do I see the sublist data?
Should I actually get the invoice values on beforeSubmit() and then create the JE with afterSubmit()? Can I use the values in an afterSubmit() function?
What is the method to pull out line data and apply with setSublistValue()?
If anyone can guide me on this last process, it would help.
Thanks.
Upvotes: 0
Views: 1077
Reputation: 31
Yep. That's what I figured I'd need.
for(var intLine = 0; intLine < intInvlineCount; intLine ++)
{
//retrieve CMO details from PO, then add as items in Inventory Transfer
var jrnAcct = newInvRec.getSublistValue({
sublistId: 'item',
fieldId: 'account',
line: intLine
});
var jrnMemo = newInvRec.getSublistValue({
sublistId: 'item',
fieldId: 'description',
line: intLine
});
var invAmount = newInvRec.getSublistValue({
sublistId: 'item',
fieldId: 'amount',
line: intLine
});
//addJournal Entry lines
recJournal.selectNewLine({
sublistId: 'line'
});
recJournal.setCurrentSublistValue({
sublistId: 'line',
fieldId: 'account',
value: jrnAcct
});
recJournal.setCurrentSublistValue({
sublistId: 'line',
fieldId: 'debit',
value: invAmount
});
recJournal.setCurrentSublistValue({
sublistId: 'line',
fieldId: 'memo',
value: jrnMemo
});
recJournal.commitLine({
sublistId: 'line'
});
recJournal.selectNewLine({
sublistId: 'line'
});
recJournal.setCurrentSublistValue({
sublistId: 'line',
fieldId: 'account',
value: '872'
});
recJournal.setCurrentSublistValue({
sublistId: 'line',
fieldId: 'credit',
value: invAmount
});
recJournal.setCurrentSublistValue({
sublistId: 'line',
fieldId: 'memo',
value: jrnMemo
});
recJournal.commitLine({
sublistId: 'line'
});
}
Works like a charm now! Thanks for the input!
Upvotes: 1
Reputation: 26
I recommend if you plan on just creating 1 journal for the invoice, prior to you loop perform your record.create. Everything should be done in your afterSubmit. Your context.newRecord will have everything you need.
const journalRec = record.create({type: record.Type.JOURNAL, isDynamic: true});
Then during your loop, for each line in your invoice create lines in your journal as you intend to copy it. You will need your journal to balance so ensure you write in a single loop, a debit and credit.
for (let i = 0; i < newRecord.getLineCount("item"); i++) {
const amount = newRecord.getSublistValue("item", "amount", i);
log.debug('Amount field', amount) // This will get you the value you retrieved
//Add information to your line
journal.selectNewLine("line");
if (amount > 0) {
journal.setCurrentSublistValue("debit", amount);
} else {
journal.setCurrentSublistValue("credit", amount);
}
//Add Account
//Add another line if you need it to balance
journal.commitLine("line");
}
journal.save();
If you are consolidating invoices, you will need to create a saved search/query, and run that through a map/reduce. As you mentioned UE afterSubmit I am going to assume 1 to 1 relation with your journal and invoice.
Upvotes: 1