Thursday, October 23, 2008

Finding unused labels

Here is a small job for finding labels, from a particular label file, that are not used in the application. You need an updated cross reference to use the job.
static void findUnXedLabels(SysOperationProgress _progress = null)
Label label = new Label('da'); // The language to 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.