* Change template for existing source

Questions regarding use of any Version of Family Historian. Please ensure you have set your Version of Family Historian in your Profile. If your question fits in one of these subject-specific sub-forums, please ask it there.
Post Reply
avatar
JoopvB
Superstar
Posts: 328
Joined: 02 May 2015 14:32
Family Historian: V7

Change template for existing source

Post by JoopvB » 24 Dec 2020 17:21

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?

avatar
JoopvB
Superstar
Posts: 328
Joined: 02 May 2015 14:32
Family Historian: V7

Re: Change template for existing source

Post by JoopvB » 28 Dec 2020 11:43

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

User avatar
tatewise
Megastar
Posts: 27088
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: Change template for existing source

Post by tatewise » 28 Dec 2020 11:52

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)
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry

avatar
JoopvB
Superstar
Posts: 328
Joined: 02 May 2015 14:32
Family Historian: V7

Re: Change template for existing source

Post by JoopvB » 28 Dec 2020 12:43

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.
Attachments
plugin test.jpg
plugin test.jpg (69.34 KiB) Viewed 1587 times

User avatar
ColeValleyGirl
Megastar
Posts: 4854
Joined: 28 Dec 2005 22:02
Family Historian: V7
Location: Cirencester, Gloucestershire
Contact:

Re: Change template for existing source

Post by ColeValleyGirl » 28 Dec 2020 12:46

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) then

avatar
JoopvB
Superstar
Posts: 328
Joined: 02 May 2015 14:32
Family Historian: V7

Re: Change template for existing source

Post by JoopvB » 28 Dec 2020 13:45

Thanks Mike and Helen!

It's now working fine,

Best wishes for 2021!

Post Reply