Copy Child Branch Fields

CopyChildren and CopyBranch call each recursively to copy all subsidiary field tags and values from one database entry to another.

Requires: None

Code

function CopyBranch(ptrSource,ptrTarget)
  local strTag = fhGetTag(ptrSource)
  if strTag == "_FMT" then return end    -- Skip rich text format code
  if strTag == "_FIELD" then             -- Substitute metafield shortcut
    strTag = fhGetMetafieldShortcut(ptrSource)
  end
  local ptrNew = fhCreateItem(strTag,ptrTarget,true)
  if ptrNew:IsNull() then return end     -- Escape if item not created
  fhSetValue_Copy(ptrNew,ptrSource)
  CopyChildren(ptrSource,ptrNew)
end -- function CopyBranch

function CopyChildren(ptrSource,ptrTarget) 
  local ptrFrom = fhNewItemPtr() 
  ptrFrom = ptrSource:Clone() 
  ptrFrom:MoveToFirstChildItem(ptrFrom) 
  while ptrFrom:IsNotNull() do 
    CopyBranch(ptrFrom,ptrTarget) 
    ptrFrom:MoveNext() 
  end 
end -- function CopyChildren

 

Usage

-- Convert all Christening Events to Baptism Events
ptrInd = fhNewItemPtr()
ptrInd:MoveToFirstRecord("INDI")
while ptrInd:IsNotNull() do
	ptrOld = fhNewItemPtr()
	ptrOld:MoveTo(ptrInd,"~.CHR")
	while ptrOld:IsNotNull() do
		ptrNew = fhCreateItem("BAPM",ptrInd)
		CopyChildren(ptrOld,ptrNew)
		fhDeleteItem(ptrOld)
		ptrOld:MoveTo(ptrInd,"~.CHR")
	end
	ptrInd:MoveNext()
end

 

Last update: 15 Jan 2021