Mohammad Yusuf
Mohammad Yusuf

Reputation: 17074

Update table using only variables

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;

}

Upvotes: 1

Views: 1164

Answers (1)

Mohammad Yusuf
Mohammad Yusuf

Reputation: 17074

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;
}

Upvotes: 1

Related Questions