Copy Child Branch Fields (code snippet)

Description

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 ptrNew = fhCreateItem(fhGetTag(ptrSource),ptrTarget,true)
	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