I cloned an existing source template and want to change the template for a number of source records with the old template to the new one. Afterwards I can change the new template and sync the changes.
But how do I change the template of an existing source?
* Change template for existing source
Re: Change template for existing source
To expand on my search for an answer: I wrote a compact script that - I assumed - would do the trick. But, I must have done something wrong since I can't get it to change the source's template.
Any help would be greatly apricated!
The script is:
function main()
local pOldTemp, pNewTemp
local pt = fhNewItemPtr()
pt:MoveToFirstRecord("_SRCT")
while pt:IsNotNull() do
local sTempName = fhGetItemText(pt,"~.NAME")
if sTempName == 'Identificatie documenten' then -- the name of the template that was cloned
pOldTemp = pt:Clone()
elseif sTempName == 'Uittreksels' then -- the name to change to
pNewTemp = pt:Clone()
end
pt:MoveNext()
end
-- Prompt for source records
local tSources = {}
tSources = fhPromptUserForRecordSel('SOUR')
if next(tSources) == nil then
return
end
-- Loop over all selected source records
for _,pSource in pairs(tSources) do
local pTemplate = fhGetItemPtr(pSource,'SOUR._SRCT>') -- pointer to Template
if pTemplate == pOldTemp then
fhSetValueAsLink(pTemplate,pNewTemp)
else
fhMessageBox('Selected record is not "Identificatie documenten", but ' .. fhGetItemText(pSource,'SOUR._SRCT>'))
return
end
fhSrcEnableAutoTitle(pSource,true)
end
fhUpdateDisplay ()
end
Any help would be greatly apricated!
The script is:
function main()
local pOldTemp, pNewTemp
local pt = fhNewItemPtr()
pt:MoveToFirstRecord("_SRCT")
while pt:IsNotNull() do
local sTempName = fhGetItemText(pt,"~.NAME")
if sTempName == 'Identificatie documenten' then -- the name of the template that was cloned
pOldTemp = pt:Clone()
elseif sTempName == 'Uittreksels' then -- the name to change to
pNewTemp = pt:Clone()
end
pt:MoveNext()
end
-- Prompt for source records
local tSources = {}
tSources = fhPromptUserForRecordSel('SOUR')
if next(tSources) == nil then
return
end
-- Loop over all selected source records
for _,pSource in pairs(tSources) do
local pTemplate = fhGetItemPtr(pSource,'SOUR._SRCT>') -- pointer to Template
if pTemplate == pOldTemp then
fhSetValueAsLink(pTemplate,pNewTemp)
else
fhMessageBox('Selected record is not "Identificatie documenten", but ' .. fhGetItemText(pSource,'SOUR._SRCT>'))
return
end
fhSrcEnableAutoTitle(pSource,true)
end
fhUpdateDisplay ()
end
- tatewise
- Megastar
- Posts: 27088
- Joined: 25 May 2010 11:00
- Family Historian: V7
- Location: Torbay, Devon, UK
- Contact:
Re: Change template for existing source
It is this fragment of code that needs changing:
local pTemplate = fhGetItemPtr(pSource,'SOUR._SRCT>') -- pointer to Template
if pTemplate == pOldTemp then
fhSetValueAsLink(pTemplate,pNewTemp)
pTemplate is pointing at the Source Template record.
You need a pointer to the SOUR._SRCT link field.
local pTemplate = fhGetItemPtr(pSource,'SOUR._SRCT>') -- pointer to Template
if pTemplate == pOldTemp then
local pSourceLink = fhGetItemPtr(pSource,'SOUR._SRCT')
fhSetValueAsLink(pSourceLink,pNewTemp)
local pTemplate = fhGetItemPtr(pSource,'SOUR._SRCT>') -- pointer to Template
if pTemplate == pOldTemp then
fhSetValueAsLink(pTemplate,pNewTemp)
pTemplate is pointing at the Source Template record.
You need a pointer to the SOUR._SRCT link field.
local pTemplate = fhGetItemPtr(pSource,'SOUR._SRCT>') -- pointer to Template
if pTemplate == pOldTemp then
local pSourceLink = fhGetItemPtr(pSource,'SOUR._SRCT')
fhSetValueAsLink(pSourceLink,pNewTemp)
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
Re: Change template for existing source
I understand and tried it, but the test:
if pTemplate == pOldTemp then
local pSourceLink = fhGetItemPtr(pSource,'SOUR._SRCT')
fhSetValueAsLink(pSourceLink,pNewTemp)
ends up in the else branch (although pTemplate and pOldTemplate appear to be the same).
See attachment.
if pTemplate == pOldTemp then
local pSourceLink = fhGetItemPtr(pSource,'SOUR._SRCT')
fhSetValueAsLink(pSourceLink,pNewTemp)
ends up in the else branch (although pTemplate and pOldTemplate appear to be the same).
See attachment.
- Attachments
-
- plugin test.jpg (69.34 KiB) Viewed 1586 times
- ColeValleyGirl
- Megastar
- Posts: 4854
- Joined: 28 Dec 2005 22:02
- Family Historian: V7
- Location: Cirencester, Gloucestershire
- Contact:
Re: Change template for existing source
If you're comparing pointers, you need to use IsSame to detect if they're pointing to the same thing:
Code: Select all
if pTemplate:IsSame(pOldTemp) thenHelen Wright
ColeValleyGirl's family history
ColeValleyGirl's family history
Re: Change template for existing source
Thanks Mike and Helen!
It's now working fine,
Best wishes for 2021!
It's now working fine,
Best wishes for 2021!