April 04, 2013

Finding unused lables

Finding unused labels in Axapta

Here is a small job for finding labels, from a particular label file, that are not used in your whole application code. You need an updated cross reference to use the job. I assumed that you are familiar with label files.


static void findUnXedLabels(SysOperationProgress _progress = null)
{
    Label     label       = new Label('da');// The languageto use for finding the labels
    str 250   labelId     = label.searchFirst('');
    Map       mapLabels   = new Map(types::String, types::String);
    MapEnumerator   mapEnumerator;
    ;

    setPrefix("Finding UnX'ed labels");

    while (labelId)
    {
        if (label.moduleId(labelId) == "XYZ") // The particular label file
        {
            if ((select xRefNames
                     index hint Name
                     where  xRefNames.Kind == xRefKind::Label &&
                            xRefNames.Name == labelId &&
                            xRefNames.TypeName == '').RecId == 0)
            {
                mapLabels.insert(labelId, label.extractString(labelId));
            }
        }
        labelId = label.searchNext();
    }

    // The maps is used to sort the lables
    mapEnumerator = mapLabels.getEnumerator();

    while (mapEnumerator.moveNext())
    {
        info (strFmt("%1 %2", mapEnumerator.currentKey(), mapEnumerator.currentValue()));
    }
}

In standard AX you can run this check for all labelfiles and save the result to a file, by using the \Classes\SysApplCheck\findUnXedLabels method.
-Harry

1 comment:

Thanks