Page 1 of 1

How to update data fields

Posted: 23 Oct 2023 17:02
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

Re: How to update data fields

Posted: 23 Oct 2023 17:43
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.

Re: How to update data fields

Posted: 25 Oct 2023 17:10
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

Re: How to update data fields

Posted: 25 Oct 2023 21:24
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

Re: How to update data fields

Posted: 26 Oct 2023 07:41
by ogulbran
Thanks.