How can I find the last labelId in AX2009?

1.6k Views Asked by At

I'd like to insert all Labels from a labelModuleId in an AX2009 table.

I have this job, that does nearly everything I need. But I have to enter the max Id (toLabel = 1000):

static void OcShowAllLabel(Args _args)
{
    xInfo               xinfo;
    LanguageId          currentLanguageId;
    LabelModuleId       labelModuleId = 'OCM'; // hier evt eine Eingabe durch Benutzer zur Auswahl
    LabelIdNum          frLabel;
    LabelIdNum          toLabel = 1000;
    LabelId             labelId;
    OcShowAllLabels_RS  tab;
    Label               blub = new Label();
    str                 label;
    ;

    xInfo = new xInfo();
    currentLanguageId = xInfo.language();
    delete_from tab
        where tab.LanguageId == currentLanguageId
        && tab.LabelModuleId == labelModuleId;

    for (frLabel = 1; frLabel <= toLabel; frLabel++)
    {
        labelId = strfmt('@%1%2', labelModuleId, frLabel);
        label = SysLabel::labelId2String(labelId, currentLanguageId);
        if (labelId != label)
        {
            tab.initValue();
            tab.LabelId = labelId;
            tab.Label = label;
            tab.LanguageId =  currentLanguageId;
            tab.LabelModuleId = labelModuleId;
            tab.insert();
        }
    }

    Info('done');
}
3

There are 3 best solutions below

0
On

Here is another option I suppose. You can insert a label to get the next label number and then just immediately delete it:

static void Job32(Args _args)
{
    SysLabel sysLabel = new SysLabel(LanguageTable::defaultLanguage());
    SysLabelEdit sysLabelEdit = new SysLabeLEdit();
    LabelId labelid;
    ;

    labelId = syslabel.insert('alextest', '', 'OCM');

    info(strfmt("%1", labelId));

    sysLabelEdit.labelDelete(labelId, false);
}

It does seem to consume the number from the number sequence though. You could just do a Label::Flush(...) and then check the text file via code. Look at Classes\SysLabel* to see some of how the system deals with labels. It doesn't look very simple by any means.

0
On

Here is another option that might work for you. This will identify missing labels too. Change 'en-us' to your language. This is a "dirty" alternative I suppose. You might need to add something to say "if we find 5 labels in a row where they're like '@OCM'".

for (i=1; i<999; i++)
{
    labelId = strfmt("@%1%2", 'OCM', i);
    s = SysLabel::labelId2String(labelId, 'en-us');

    if (s like '@OCM*')
    {
        info (strfmt("%1: Last is %2", i, s));
        break;
    }
    info(strfmt("%1: %2", i, s));
}
1
On

If this is a one-time job, you can just stop the AOS and open the label file in notepad. It's in your application folder called axXXXen-us.ald, where XXX is your label file name and en-us is your language.

Look at classes\Tutorial_ThreadWork\doTheWork to see where they use a while(sLabel) instead of a for loop like you have.

container doTheWork(Thread t,LabelType searchFor)
{
    container   retVal;
    SysLabel    sysLabel = new SysLabel(LanguageTable::defaultLanguage());
    str         slabel;
    ;

    slabel = sysLabel.searchFirst(searchFor);
    while (slabel)
    {
        retVal += sLabel;
        slabel = sysLabel.searchNext();
    }

   return retVal;
}

Since the label file is a text file, it would make sense that you can't just select the last one, but you have to iterate through the file. AX caches the labels however, but I don't believe you can just readily access the label cache as far as I know.

Lastly, hopefully you won't try this, but don't try to just read in the label text file, because AX sometimes has labels that it hasn't flushed to that file from the cache. I think Label::Flush(...) will flush them, but I'm not sure.