* How to update data fields

For plugin authors to discuss plugin programming
Post Reply
avatar
ogulbran
Gold
Posts: 22
Joined: 26 Dec 2017 22:31
Family Historian: V7
Location: Norway
Contact:

How to update data fields

Post by ogulbran »

Hi
I am new to plugin writing and hope someone can help me.
Trying to make a plugin where I correct my data. Example here is to set the nickname field to the same as the usedname field. I am able to get the current values in the fields - but I do not understand how to update the same fields.
Have tried the following code:

Code: Select all

pi:MoveToFirstRecord('INDI')    -- set the first to point to the first Source record
while not pi:IsNull() do

   -- For each Person 

	-- Retrieving info to variables
	strRefn = fhGetItemText(pi,'INDI.REFN')
   	strName = fhGetItemText(pi,'INDI.NAME')
	strNameUsed = fhGetItemText(pi,'INDI.NAME._USED')
	strNameNick = fhGetItemText(pi,'INDI.NAME.NICK')
	
	-- Example person for testing
	if (strRefn == '52') then

		-- output fields (THIS WORKS FINE)
		print(strRefn, strName, ' Used: ',strNameUsed, ' Nick: ',strNameNick)
	
		-- Updating nickname with used name (DO NOT WORK)
		piNameNick = fhCreateItem('NAME.NICK', pi)    -- create a NAME field within this record, and set ptr to it
		fhSetValueAsText(piNameNick, strNameUsed)  -- set value of the name using passed in parameter
		
	end
   
   pi:MoveNext()
end
How can I get the correct writing of data?

Best regards,
Øivind
Last edited by tatewise on 23 Oct 2023 17:28, edited 1 time in total.
Reason: Edited to use [code] formatting of script.
User avatar
tatewise
Megastar
Posts: 28488
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: How to update data fields

Post by tatewise »

There are a number of issues with your use of the fhCreateItem( ... ) function.

Check the Help for that function and you'll see that the 1st parameter must be a single Tag in this context.
e.g. fhCreateItem( 'NAME', pi )
c.f. fhGetItemText( ... ) where the Help says 2nd parameter is a Data Reference.

So you need to get the pointer to the NAME field and then create tag NICK for that pointer.

HOWEVER, if INDI.NAME.NICK already exists you cannot use fhCreateItem( ... ) to create the NICK Tag.
If you debug your script you will find piNameNick is always NULL because the creation fails.

If INDI.NAME.NICK already exists you need to obtain its existing pointer.
If INDI.NAME.NICK does not exist then use fhCreateItem( 'NICK', piName ) to create its pointer.
Then fhSetValueAsText( piNameNick, strNameUsed ) will work.
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
avatar
ogulbran
Gold
Posts: 22
Joined: 26 Dec 2017 22:31
Family Historian: V7
Location: Norway
Contact:

Re: How to update data fields

Post by ogulbran »

Thank you.

Had not sufficient understanding of how the data structure was accessed and modified.

Read help and used debugging as you proposed.

Got it to work!

Best regards,
Øivind
User avatar
tatewise
Megastar
Posts: 28488
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: How to update data fields

Post by tatewise »

I'm glad you got it working.

Here are some other tips.

You sensibly use variables such as strRefn and strName with the prefix str to hold strings.

I suggest you use variables with the prefix ptr to hold pointers, so instead of pi use ptrIndi.
Then later use ptrName and ptrNick for pointers to the NAME and NICK fields.

Instead of while not ptrIndi:IsNull() do you can use while ptrIndi:IsNotNull() do
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
avatar
ogulbran
Gold
Posts: 22
Joined: 26 Dec 2017 22:31
Family Historian: V7
Location: Norway
Contact:

Re: How to update data fields

Post by ogulbran »

Thanks.
Post Reply