1

I have a table EmpTable like so: enter image description here

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;

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

}

Update:

This code works:

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;

}

But this code doesn't:

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;

}
Mohammad Yusuf
  • 16,554
  • 10
  • 50
  • 78

1 Answers1

1

Bounding the string to fixed length resolved the issue:

The below code works now:

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;

}

Even Better solution by Martin Drab:

static void Job1(Args _args)
{

    TableName table = 'EmpTable';
    FieldName fieldToUpdate= 'Salary';
    FieldName fieldToSelect= 'Id';
    int rowId = 1;
    real sal = 6546456;
    
    SysDictTable dt = SysDictTable::newName(table);
    Common common = dt.makeRecord();
    
    ttsbegin;
    while select forUpdate common
        where common.(dt.fieldName2Id(fieldToSelect)) == rowId;
    {
        common.(dt.fieldName2Id(fieldToUpdate)) = sal;
    
        if (!common.validateWrite())
        {
            throw error("Nope");
        }
        common.update();
    }
    
    ttscommit;
}
Community
  • 1
  • 1
Mohammad Yusuf
  • 16,554
  • 10
  • 50
  • 78
  • 1
    You should consider adding error checking to verify the `fieldName2Id` returns a valid `FieldId`. If you use a `table` that does not have the field to update, it might error. You can easily test though by changing `fieldIdToUpdate='asdf'` or `fieldToSelect`...that type of thing. It may *always* work with your code, but in the future, another developer may pass another table to whatever code you're developing. – Alex Kwitny Nov 28 '18 at 18:44
  • Thanks Alex for the additional scenario. I wanted to implement this for a common field spanning multiple tables. Earlier multiple update methods were implemented for each table. – Mohammad Yusuf Jan 09 '19 at 17:21