this.SalesQty value changes when looping !

Hi guys, I’m new to X++, I’m having this weird issue, It seems like this.SalesQty value dicreases on every iteration on a loop,

This happens only when i make some calculations on this value, This is my code (located in Tables\SalesLine\Methods\modifiedField):

case fieldNum(SalesLine, DistributionId):

if(this.DistributionId) // check if user filled the field

{

while select distributionsVendors where distributionsVendors.DistributionId == this.DistributionId

{

ttsBegin;

salesLineInsert.SalesId = this.SalesId;

//salesLineInsert.SalesStatus = this.SalesStatus;

salesLineInsert.Invoiceable = NoYes::Yes;

salesLineInsert.ItemId = this.ItemId;

salesLineInsert.SalesCategory = this.SalesCategory;

salesLineInsert.SalesQty = this.SalesQty * (distributionsVendors.Percentage / 100);

salesLineInsert.SalesUnit = this.SalesUnit;

salesLineInsert.SalesPrice = this.SalesPrice * (distributionsVendors.Percentage / 100);

salesLineInsert.LineDisc = this.LineDisc;

salesLineInsert.LinePercent = this.LinePercent;

salesLineInsert.LineAmount = this.LineAmount * (distributionsVendors.Percentage / 100);

salesLineInsert.SalesDeliverNow = this.SalesDeliverNow;

salesLineInsert.createLine(true, true, true, true, true, true);

salesLineInsert.clear(); // Preparing the buffer for the next iteration

info(strFmt(“qte: %1”, this.SalesQty));

this.dataSource().refresh();

this.dataSource().research();

ttsCommit;

}

}

break;

The line salesLineInsert.SalesQty = this.SalesQty * (distributionsVendors.Percentage / 100); changes this.SalesQty value !

Thank you guys in advance .

I think after the insertion “this” starts referencing to the last inserted record , If this true, Is there any way to force it to keep selecting the original record ?

I found the solution,

this.dataSource().refresh();

this.dataSource().research();

must be put after the loop , Because when refreshing data source it selects automatically the last record, Thanks .

What if the DistributionId is cleared on sales line, i am not sure how you are handling the already inserted sales lines.

Not sure what you mean exactly, But if DistributionId is cleared or not filled then nothing will be executed as i’ve already put this condition :

if(this.DistributionId)

You selected a wrong distribution id and you want to clear it Off, what happens to the created sales lines?

There is a field I created on the SalesLine table (ParentLineId) that relates those added sales lines to their parent, Beside that there are methods that contain controllers in case of a delete or a modification .