Find Duplicate Custom IDs.fh_lua

--[[
@Title: Find Duplicate Custom IDs
@Author: Jane Taubman
@Version: 1.3
@LastUpdated: 4 Jan 2020
@Description: Searches Selected Record Type(s) for Records with Duplicate Custom IDs
V1.1 Added Custom ID Column
1.3 remove ' from name
]]

function main()
    local tblId = {}
    local tblDup1 = {}
    local tblDup2 = {}
    local tblDupID = {}
    for type in recordtypes() do
        for pi in records(type) do
            strId = fhGetItemText(pi,'~.REFN')
            if strId and strId ~= '' then
                if tblId[strId] then
                    -- Duplicate Found
                    table.insert(tblDup1,tblId[strId])
                    table.insert(tblDup2,pi:Clone())
                    table.insert(tblDupID,strId)
                else
                    tblId[strId] = pi:Clone()
                end
            end
        end
    end
    if #tblDup1 > 0 then
        fhOutputResultSetTitles('Records with Duplicate Custom Ids')
        fhOutputResultSetColumn('Custom ID', 'text', tblDupID, #tblDup1,40)
        fhOutputResultSetColumn('First Record', 'item', tblDup1, #tblDup1,140)
        fhOutputResultSetColumn('Second Record', 'item', tblDup2, #tblDup1,140)

    else
        fhMessageBox('No Duplicates Found')
    end
end
------------------ Functions

function records(type)
    local pi = fhNewItemPtr()
    local p2 = fhNewItemPtr()
    pi:MoveToFirstRecord(type)
    return function ()
        p2:MoveTo(pi)
        pi:MoveNext()
        if p2:IsNotNull() then return p2 end
    end
end

function recordtypes()
    local t = 0
    local m = fhGetRecordTypeCount()
    return function()
        t = t + 1
        if t <= m then
            return fhGetRecordTypeTag(t)
        end
    end
end
------------------- Run Main
main()

Source:Find-Duplicate-Custom-IDs-1.fh_lua