Page 1 of 1

Populating citation fields in a prepared citation

Posted: 04 Mar 2021 11:22
by shoshk
I can't figure out how to set values for citation fields in a prepared citation (pCite). Can somebody help?

Re: Populating citation fields in a prepared citation

Posted: 04 Mar 2021 11:48
by Jane
The fhUtils pCite option is read only, so you will need to access the fields directly via the HEAD record and the _PCIT field.

Have a look at the source for the loadPreparedCitation in the fhutils source for ideas.

Re: Populating citation fields in a prepared citation

Posted: 04 Mar 2021 11:50
by ColeValleyGirl
If you're struggling to work out the data references, I ended up looking at the header record in the Gedcom to work them out.

Re: Populating citation fields in a prepared citation

Posted: 04 Mar 2021 12:59
by tatewise
It depends on what kinds of Citation fields.

(1) Generic GEDCOM citation fields will have standard data references such as:
%HEAD._PCIT.SOUR.PAGE% is Where within Source
%HEAD._PCIT.SOUR.DATA.DATE% is Entry Date
%HEAD._PCIT.SOUR.DATA.TEXT% is Text from Source

(2) For metafields there are multiple instances of %HEAD._PCIT.SOUR._FIELD[n]%
That data reference must be changed to its metafield shortcut:
local strMeta = fhGetMetafieldShortcut(ptrMeta)
local ptrMeta = fhGetItemPtr(ptrSour,"~." .. strMeta)
Then you can use such as fhSetValueAsText(ptrMeta,"New Text")
e.g.

Code: Select all

local ptrHead = fhNewItemPtr()
ptrHead:MoveToFirstRecord("HEAD")
local ptrSour = fhGetItemPtr(ptrHead,"~._PCIT.SOUR")
local ptrDate = fhGetItemPtr(ptrSour,"~.DATA.DATE")
local ptrMeta = fhGetItemPtr(ptrSour,"~._FIELD")
local strMeta = fhGetMetafieldShortcut(ptrMeta)
local ptrMeta = fhGetItemPtr(ptrSour,"~."..strMeta)
fhSetValueAsText(ptrMeta,"New Text")

Re: Populating citation fields in a prepared citation

Posted: 04 Mar 2021 15:06
by shoshk
I have no problem setting values for fields associated with the source.

I cannot set the values for citation-specific fields.

I use createUpdateItem to set values for fields associated with the source. It works fine.

When I try to use it for citation-specific fields, it doesn't work. I get the message "createUpdateItem - failed to create ~.DT-SupDate.

My code looks like this:

Code: Select all

local ptrBase = fhNewItemPtr()
ptrBase:MoveToFirstRecord('HEAD')
local ptr = fhGetItemPtr(ptrBase,'~._PCIT')
local pSource = fhGetItemPtr(ptr,'~.SOUR>')
fh.createUpdateItem(pSource, '~.DT-SupDate', util.GetTodayAsText())

Re: Populating citation fields in a prepared citation

Posted: 04 Mar 2021 15:37
by tatewise
When you use SOUR> that sets the pointer to the Source record and NOT the Citation. ( Note the > )

You must use SOUR without > which sets the pointer to the Citation as in my script:
local ptrHead = fhNewItemPtr()
ptrHead:MoveToFirstRecord("HEAD")
local ptrSour = fhGetItemPtr(ptrHead,"~._PCIT.SOUR")

Also, I think the metafield shortcut is using the wrong syntax and should be:
'~.~DT-SupDate'

It is important to understand the difference between SOUR> & SOUR and PLAC> & PLAC and FAMS> & FAMS, etc.
The former of each pair follow the link to point to another record.
The latter of each pair refers to the tag in this record that holds the pointer.

Re: Populating citation fields in a prepared citation

Posted: 04 Mar 2021 15:46
by shoshk
Mike, thank you, thank you, thank you! I wouldn't have solved this without your help.

BTW, the syntax ~DT-SupDate works fine.

Re: Populating citation fields in a prepared citation

Posted: 04 Mar 2021 15:52
by tatewise
Shos, I think some typos may be creeping in.

Your earlier script used ~.DT-SupDate with a tilda and a dot, which I think is wrong.
Now you have posted ~DT-SupDate with a tilda but no dot, which is the correct shortcut format.
But when used as a relative data ref parameter that must be prefixed with ~. producing ~.~DT-SupDate
So it depends on the context.
fhCreateItem( '~DT-SupDate', ptrParent )
fhGetItemPtr( ptrParent, '~.~DT-SupDate' )

I use the following to compose the shortcut
fhGetMetafieldShortcut(...)