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 :
Finance | Project Operations, Human Resources, ...
Answered

Negative sales line quantity with X++

(0) ShareShare
ReportReport
Posted on by 55

Hi.

I want to create a sales order via X code which lines have negative values on the SalesQty field.

I already have a class that creates the sales orders correctly with positive quantities, the problem comes when I try to create a line with a negative quantity, when I try to do It, I get the following messages:

"Quantity may not change sign." "Update has been canceled."

I am able to change the quantities of the already created SO lines to negative values correctly from the sales orders menu in the graphic interface, so how would I do It by code?

This is the code I tried:

SalesQty negSalesQty;

negSalesQty = -salesLine.SalesQty;
salesLine.LineAmount = salesLine.calcLineAmount();
salesLine.update();

  • Alex VN Profile Picture
    1,994 on at

    Hi,

    I can see in the code that you are trying to update a Sales Line with LineAmount = salesLine.calcLineAmount() but nothing related to the Sales Qty and also it is update instead of create a new line. May I know what you are trying to achieve?

  • chrisrojas Profile Picture
    55 on at

    Hi Alex,

    I´m sorry, I forgot to write the line of code where I assign the negSalesQty value to the sales qty field in the question, but it is in the code and I'm getting the same messages when I try to update.

    SalesQty negSalesQty;
    negSalesQty = -lines.SalesQty;
    salesLine.SalesQty = negSalesQty;
    salesLine.LineAmount = salesLine.calcLineAmount();
    salesLine.update();

    I'm trying to create a credit sales order, so I need to create a new sales order that has the same values of an original SO but with the sales line quantities changed to negative values.

    If it helps, the "Quantity may not change sign" message I'm getting is a warning message.

  • Verified answer
    Gunjan Bhattachayya Profile Picture
    35,429 on at

    Hi Chris,

    From your code, I believe you are creating sales order lines and then updating them with the negative quantity from another sales order line record. Instead of doing an update, can you try to set the negative quantity before creating the new line?

    salesLine.SalesQty = negSalesQty;
    ...
    ...
    salesLine.createLine(true, true, true, true, true, true);

    Is there any any reason you are inserting the line first and then doing an update?

  • chrisrojas Profile Picture
    55 on at

    Hi Gunjan,

    I was inserting the line first and doing the update because It was written like that in the example I was using, but I tried setting the negative value before inserting the line as you said and It worked fine, thank you!

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

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 528 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 455 Super User 2026 Season 2

#3
CU10121822-0 Profile Picture

CU10121822-0 376

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans