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 :
Microsoft Dynamics AX (Archived)

Update table using only variables

(0) ShareShare
ReportReport
Posted on by 2,355

Hi,

I have a table EmpTable like so:

EmpTable.png

If I want to update the salary of John I can do it like so:

static void UpdateSal(Args _args)
{
    EmpTable EmpTable;
    real sal=110000;
    int RowId = 1;
    
    ttsBegin;
    select forUpdate EmpTable where EmpTable.Id==RowId;
    EmpTable.Salary=sal;
    EmpTable.update();
    ttsCommit;
    
    
}


I want help in implementing the above code with using only variables:

static void UpdateSal_WithStrValues(Args _args)
{

    str table = 'EmpTable'
    str field = 'Salary'
    int RowId = 1;
    real sal=110000;

    .....??
    .....??

}

*This post is locked for comments

I have the same question (0)
  • AXTechie2120 Profile Picture
    568 on at

    Hi,

    what do you mean by variables.

    You can updates the details by the code given by you.

    But why do you want the other way?

    Can you share more details on that.

  • Suggested answer
    nmaenpaa Profile Picture
    101,172 Moderator on at

    You can't update the data in the table without having a variable that points to the table buffer. This is what you already have in your first code example. So it already works with variables.

    If you want to create a method that can be used to update data in many different tables, you can use a variable of type Common instead of EmplTable.

    And you can reference fields with Common.FieldNum(TableName, FieldName).

    Is this what you are looking for? What is your actual functional requirement?

  • MYGz Profile Picture
    2,355 on at

    I wanted to implement a common function where either:

    1. I can supply 4 arguments, tablename, row to select, field to update, value to be updated. 

    (This case might not be possible as hinted by you since I need a table buffer object first, can't it be created dynamically?)

    or 

    2. I can supply a 3 arguments, single selected buffer table record, field to update, value to update.

    I'm Trying something here but not getting it:

    static void UpdateSal_WithStrValues(Args _args)
    {
    Common common;
    EmpTable Emptable;
    str table = 'EmpTable';
    str fieldToUpdate= 'Salary';
    str fieldToSelect= 'Id';
    int RowId = 1;
    real sal=110000;
    
    select EmpTable where EmpTable.Id==RowId;
    
    Common = EmpTable;
    
    ttsbegin;
    select forupdate common
    where common.(FieldNum(table, fieldToSelect)) == RowId;
    
    common.(FieldNum(table, fieldToUpdate)) = sal;
    common.update();
    
    ttscommit;
    
    
    
    }
    
    


  • Suggested answer
    nmaenpaa Profile Picture
    101,172 Moderator on at

    You can use DictTable class to instantiate the Common buffer with your table id:

    DictTable dictTable = new DictTable(tableId); 
    Common myTable = dictTable.makeRecord();


    And you can use tablename2id function to get a table id if you know the table name.

  • MYGz Profile Picture
    2,355 on at

    Thank you Nikolaos. I was able to implement it with your valuable tips.

    Here's how I did it:

    static void Job1(Args _args)
    {
        SysDictTable dictTable = new SysDictTable(tablename2id('EmpTable'));
        Common common = dictTable.makeRecord();
    
        ttsbegin;
        while select forupdate common
            where common.(fieldName2id(tableName2Id("EmpTable"),'Id')) == 1
        {
            common.(fieldName2id(tableName2Id("EmpTable"),'Salary')) = 110100;
            common.update();
        }
        ttscommit;
    
    }



  • MYGz Profile Picture
    2,355 on at

    Oh wait. TableName2Id requires a compile time string. Won't work with a string variable. Any hacks for this? The below code doesn't works. But the above does.

    static void Job1(Args _args)
    {
    
    str table = 'EmpTable';
    str fieldToUpdate= 'Salary';
    str fieldToSelect= 'Id';
    int RowId = 1;
    real sal=34536;
    
    SysDictTable dictTable = new SysDictTable(tablename2id(table));
    Common common = dictTable.makeRecord();
    
    
    ttsbegin;
    while select forupdate common
    where common.(fieldName2id(tableName2Id(table),fieldToSelect)) == RowId
    {
    common.(fieldName2id(tableName2Id(table),fieldToUpdate)) = sal;
    common.update();
    }
    ttscommit;
    
    }
    
  • Martin Dráb Profile Picture
    239,364 Most Valuable Professional on at

    I don't think you're right; the parameter of tablename2id() can be a variable.

    Anyway, you can simply use the right factory method: SysDictTable::newName(table).

  • MYGz Profile Picture
    2,355 on at

    Thanks Martin.

    You are right actually. It is only throwing error in the Where clause, Attaching screenshots

    1. No Error if I don't use variable in Where clause

    2210.Error.png

    2. Error if I use variable in where clause:

    2210.Error.png

    3. Don't know how to implement the static function that you provided, this is what I tried:

    sameError.png

    Can the where clause be fixed somehow?

    Thanks.

  • Martin Dráb Profile Picture
    239,364 Most Valuable Professional on at

    The error does say that you can't use variables in tableName2Id() - it says "[...] unbounded string fields are not allowed in a WHERE expression".

    The code I gave you is a replacement for new SysDictTable(tablename2id(table)). Both create a SysDictTable table object based on a table name. You're using it at a place where you need a string, not a SysDictTable object.

  • MYGz Profile Picture
    2,355 on at

    Thanks Martin. You were right. New to X++ so don't have much clue about various errors.

    Got a tip from here: https://stackoverflow.com/questions/53513457/update-table-using-only-variables

    This code works now: (Made the string bounded to 50 length)

    static void Job1(Args _args)
    {
        
        str 50 table = 'EmpTable';
        str 50 fieldToUpdate= 'Salary';
        str 50 fieldToSelect= 'Id';
        int RowId = 1;
        real sal=12213;
        
        SysDictTable dictTable = new SysDictTable(tablename2id(table));
        Common common = dictTable.makeRecord();
     
        
        ttsbegin;
        while select forupdate common
            where common.(fieldName2id(tableName2Id(table),fieldToSelect)) == RowId
        {
            common.(fieldName2id(tableName2Id(table),fieldToUpdate)) = sal;
            common.update();
        }
        ttscommit;
    
    }

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
CP04-islander Profile Picture

CP04-islander 39

#2
Michel ROY Profile Picture

Michel ROY 14

#3
imran ul haq Profile Picture

imran ul haq 8

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans