web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

Copying an invoice from Posted Sales Invoices to Sales Invoices (Unposted) in an Al Extension

(0) ShareShare
ReportReport
Posted on by 911

I've written an AL Extension that clones / copies an invoice from Posted Sales Invoices to Sales Invoices (Unposted) in an Al Extension using Record.TransferFields. At the same time it edits a description in a Comment line within the invoice. This works fine, but when I try to post the invoice in question, it reports an error stating that fields 'Qty. To Invoice' & 'Qty.To Ship' should contain a value that appears to be the same as field Quantity. (i.e. If Quantity = 4, then it complains that the aforementioned Qty fields are not also set to 4.

On investigation, fields 'Qty. To Invoice' & 'Qty.To Ship' do not exist in the posted table (Sales Invoice Line) but do in the unposted table (Sales Line) . This, I guess, explains why Record.TransferFields doesn't put a value in these 2 fields because there's no source.

So, I wrote an extra procedure to pass through the lines and copy the value in Quantity to 'Qty. To Invoice' & 'Qty.To Ship'. This changed the error to 'You cannot post these lines because you have not entered a quantity on one or more of the lines'.

There is a comment line that does not contain a quantity value because it is a comment (stating the obvious). The whole purpose of the extension is to copy a posted invoice to unposted and change the comment.

Question 1:
Did I do the right thing in copying the value(s) from Quantity to 'Qty. To Invoice' & 'Qty.To Ship'?

Question 2:
Is it valid for an invoice to contain a Comment line having no Quantity?

Any other thoughts would be welcome.

  • Suggested answer
    JAngle Profile Picture
    161 on at

    Transferfields is doing plenty of heavy lifting here but a pattern without would of been my shout. Especially with the differing type lines. Perhaps your setting of those fields should be conditional based on the type value. So a case statement is the norm with enum/option fields.

  • Nick Webb Profile Picture
    911 on at

    Thanks for the response. I've tried with TransferFields and with a loop through each field feeding the values across. Same issue with the Qty fields as previously described. I wonder if some sort of Validate or CalcFields loop is required?

  • Suggested answer
    JAngle Profile Picture
    161 on at

    It’s worth considering what you are doing already exists in the copy document feature. Codeunit exists and the design pattern there is to piece meal validate fields. In your case you could just call the appropriate function if it is global and save yourself a lot of coding effort

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 554 Super User 2026 Season 2

#2
YUN ZHU Profile Picture

YUN ZHU 360 Super User 2026 Season 2

#3
Teagen Boll Profile Picture

Teagen Boll 201 Super User 2026 Season 2

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans