Flags Add and Remove Function (code snippet)

Description

Simple function to set and unset flags. It takes 3 parameters

  • Individual Pointer
  • Flag Name (it will create the flag if it's new to the project)
  • boolean value true to create and false to delete

It returns true if the action was successful and false if it failed, e.g. you tried to delete a flag which was not set or create one which was already set.

Requires: None

Code

SetFlag.fh_lua
function setFlag(ptrInd,sFlag,bValue)
    strTag, strError = fhGetFlagTag(sFlag, true) -- Obtain tag for Flag
    if strError then
        error('Flag Creation Failed')
    end
    local ptrFlags = fhGetItemPtr(ptrInd, "~._FLGS") -- Check "_FLGS" tag
    if ptrFlags:IsNull() then
        ptrFlags = fhCreateItem("_FLGS", ptrInd) -- Create "_FLGS" tag if necessary
    end
    local ptrFlag = fhGetItemPtr(ptrFlags, "~."..strTag)
    if bValue then
        if ptrFlag:IsNull() then
            ptrFlag = fhCreateItem(strTag, ptrFlags) -- Create the Flag
            if ptrFlag:IsNotNull() then
                return true
            end
        end
    else
        if ptrFlag:IsNotNull() then
            local res = fhDeleteItem(ptrFlag)
            return res
        end
    end
    return false
end

Usage

local ptrRec = fhNewItemPtr()      
ptrRec:MoveToFirstRecord("INDI")        -- Grab the first individual record
-- Create a TestFlag
res = setFlag(ptrRec,'TestFlag',true)
print(res)
-- Delete a TestFlag
res = setFlag(ptrRec,'TestFlag',false)
print(res)