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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Issue with Saving Dynamic Column Values in Sales Order Screens ---- X++

(2) ShareShare
ReportReport
Posted on by 11

I am facing an issue related to saving dynamic column values in a Sales Order scenario.

 

I have two related screens:

 

  1.  

    First screen

    This screen is used to store locations related to a Sales Order.

     

    •  

      The number of locations is dynamic and not fixed per Sales Order.


    •  

      Locations are saved correctly in this screen.




    •  
    •  

  2.  

    Second screen

    In this screen:

     

    •  

      The previously stored locations are dynamically converted into columns.


    •  

      The rows represent the Sales Order items (lines).


    •  

      Each cell should store the quantity/value of a specific item in a specific location.




    •  
    •  



  3.  
  4.  
 

Problem Description

 

When saving data from the second screen:

 

  •  

    The values entered for all rows are incorrectly saved as the same values of the last row only.


  •  

    As a result, in the SQL table, all item lines end up having identical values, matching the last inserted/updated row.




  •  
  •  
 

This behavior occurs despite the UI showing different values per row before saving.

 

I have attached:

 

  •  

    The screen code used for handling the dynamic columns and saving the data.


  •  

    Screenshots showing:

     

    •  

      The dynamic locations setup


    •  

      The dynamic grid layout


    •  

      The incorrect data stored in the SQL table




    •  
    •  



  •  
  •  
 

Question

 

What is the correct approach in Dynamics 365 to handle saving data from a grid with dynamic columns, ensuring that each row is saved with its own correct values and not overwritten by the last row?

 

Any guidance, best practices, or examples would be highly appreciated.









//////////////////////////////////////////////////////
[Form]
public class GIG_PartialShipmentItems extends FormRun
{
    SalesId salesId;
    Map locationControlMap;
    public void init()
    {
        super();
        salesId =  element.args().parm();
        locationControlMap = new Map(Types::String, Types::Class);
    }
    public void run()
    {
        super();
        GIG_PartialShipmentItemsTmp_ds.executeQuery();
        this.populateTmp();
        GIG_PartialShipmentItemsTmp_ds.research(true);
        this.buildDynamicColumns();
        this.fillValues();
    }
    private void populateTmp()
    {
        SalesLine salesLine;
        GIG_PartialShipmentItemsTmp tmp;
        ttsbegin;
        delete_from tmp where tmp.SalesId == salesId;
        while select salesLine
        where salesLine.SalesId == salesId
        {
            tmp.clear();
            tmp.SalesId      = salesLine.SalesId;
            tmp.LineNum      = salesLine.LineNum;
            tmp.ItemId       = salesLine.ItemId;
            tmp.ItemName     = salesLine.ItemName();
            tmp.Qty          = salesLine.QtyOrdered;
            tmp.RemainingQty = salesLine.RemainSalesPhysical;
            tmp.insert();
        }
        ttscommit;
    }
    private void buildDynamicColumns()
    {
        GIG_PartialShipmentDrivers drivers;
        FormRealControl ctrl;
        int idx = 1;
        while select drivers
            where drivers.SalesId == salesId
        {
            ctrl = ItemsGrid.addControl(
                FormControlType::Real,
                strFmt("dyn_%1", idx)
            );
            ctrl.label(drivers.Location);
            ctrl.width(80);
            ctrl.allowEdit(true);
            locationControlMap.insert(drivers.Location, ctrl);
            idx++;
        }
    }
    private void fillValues()
    {
        GIG_PartialShipmentItemsTmp tmp;
        GIG_PartialShipmentItemQty itemQty;
        MapEnumerator mapEnum;
        FormRealControl ctrl;
        real totalQty;
        while select tmp
            where tmp.SalesId == salesId
        {
            mapEnum = locationControlMap.getEnumerator();
            while (mapEnum.moveNext())
            {
                ctrl = mapEnum.currentValue();
                select sum(Qty) from itemQty
                    where itemQty.SalesId == tmp.SalesId
                       && itemQty.LineNum == tmp.LineNum
                       && itemQty.Location == mapEnum.currentKey();
                totalQty = itemQty.Qty;
                ctrl.realValue(totalQty);
            }
        }
    }
    [Control("CommandButton")]
    class btnSave
    {
        public void clicked()
        {
            super();
            GIG_PartialShipmentItemQty itemQty;
            GIG_PartialShipmentItemsTmp tmp;
            MapEnumerator mapEnum;
            FormRealControl ctrl;
            FormDataSource tmp_ds = GIG_PartialShipmentItemsTmp_ds;
            ttsbegin;
            delete_from itemQty
                where itemQty.SalesId == salesId;
            tmp_ds.first();
            while select tmp
            {
                mapEnum = locationControlMap.getEnumerator();
                while (mapEnum.moveNext())
                {
                    ctrl = mapEnum.currentValue();
                    if (ctrl.realValue() != 0)
                    {
                        itemQty.clear();
                        itemQty.SalesId  = tmp.SalesId;
                        itemQty.LineNum  = tmp.LineNum;
                        itemQty.Location = mapEnum.currentKey();
                        itemQty.Qty      = ctrl.realValue();
                        itemQty.insert();
                    }
                }
                tmp_ds.next();
            }
            ttscommit;
            info("Saved successfully");
        }
    }
}
//////////////////////////////////////////////////////
PartialShipmentItems.PNG
PartialShipmentDrivers.PNG
SQL.PNG
I have the same question (0)
  • Suggested answer
    Mohamed Amine Mahmoudi Profile Picture
    26,419 Super User 2025 Season 2 on at
     
    The issue is due to not respecting the unique value for the Map key. Therefore, each time it takes the last value for each location. 
    Key Value
    Location 1 2
    Location 2 2
    Location 3 2
    I suggest changing the Key for the Map locationControlMap. For example, you can use RecId instead of Location.
    locationControlMap.insert(drivers.RecId, ctrl);
    Also you need to change the code four getting the location
    itemQty.Location = GIG_PartialShipmentDrivers::findRecId(mapEnum.currentKey()).Location;
    Best regards,
    Mohamed Amine MAHMOUDI
     
     

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Martin Dráb Profile Picture

Martin Dráb 592 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 478 Super User 2025 Season 2

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 305 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans