Search code examples
group-byaxaptax++dynamics-ax-2009select-for-update

Axapta: "The record has never been selected" error after Group By clause


I'm new in Ax 2009. I would like to update the field of a record in table A after joining with another table B on which I want to apply Group by on its ID.

I tried to run the following code but it throw me an error "Cannot edit a record in tableA. The record has never been selected":

where select forupdate tableA group by tabAid, Dimension[5]
join tableB group by tabBid, Dimension[5]
where tableA.tabAid == tableB.tabBid && tableB.Dimension[5] != "" && tableA.Dimension[5]
{
     if(tableA.Dimension[5] != lines.Dimension[5])
     {
          ttsbegin;
          tableA.Dimension[5] = tableB.Dimension[5];
          tableA.update()
          ttscommit;
     }
}

I think it should be caused by the usage of Group By clause on tableA but if i remove it, on the if check, the tableA.Dimension[5] is empty.

Please someone help me. Thank you.


Solution

  • Yes, the problems are the Group by clauses. Try something like this.

    ttsbegin;
    while select forupdate tableA where tableA.Dimension[5] != ""
    join tableB where tableB.tabBid == tableA.tabAid && tableB.Dimension[5] != ""
    {
         if(tableA.Dimension[5] != tableB.Dimension[5])
         {          
              tableA.Dimension[5] = tableB.Dimension[5];
              tableA.update()          
         }
    }
    ttscommit;
    

    *I made the code without compiler, maybe it needs some adjustment.