ModifyInstance bad parameter type

Hi,

I try to add private tags in stored instances with Lua script to keep trace of reception date and time :

`
function OnStoredInstance(instanceId, tags, metadata)
if (metadata[‘ModifiedFrom’] == nil and
metadata[‘AnonymizedFrom’] == nil) then

local replace = {}
local remove = {}

replace[‘00410042’] = “MY PRIVATE DATA”
replace[‘00414220’] = string.sub(metadata[‘ReceptionDate’], 1, 8)
replace[‘00414221’] = string.sub(metadata[‘ReceptionDate’], 10)

PrintRecursive(replace)

ModifyInstance(instanceId, replace, remove)
Delete(instanceId)
end

end
`

and it ends with the following in the logs :

`
Lua says: Receiving instance
Lua says: table
Lua says: [00410042], string, MY PRIVATE DATA
Lua says: [00414220], string, 20160310
Lua says: [00414221], string, 151739
ModifyInstanceCommand.cpp:117] Unable to modify instance 83bca853-2998f0c8-694a3abe-ad3fd862-95b0d0ac in a Lua script: Bad type for a parameter

`

it seems that numeric values are not detected as strings in modification function, even if they are passed as strings in Lua.
How can I pass a numeric string value to replacement tag ?

If your modification request contains strings that are encoded like integers (which is your case), Lua will convert them to integers.

You will have to use a more evolved script using combining the REST API and the “DumpJson()” function, with the “keepStrings” argument set to “true”:
https://orthanc.chu.ulg.ac.be/book/users/lua.html#general-purpose-functions

Here is a working example:

function OnStoredInstance(instanceId, tags, metadata, origin)
– Do not modify twice the same file
if origin[‘RequestOrigin’] ~= ‘Lua’ then
local replace = {}
local remove = {}

replace[‘00410042’] = “MY PRIVATE DATA”
replace[‘00414220’] = string.sub(metadata[‘ReceptionDate’], 1, 8)
replace[‘00414221’] = string.sub(metadata[‘ReceptionDate’], 10)

PrintRecursive(replace)

local request = {}
request[‘Replace’] = replace
request[‘Remove’] = remove

– Create the modified instance
local modified = RestApiPost(‘/instances/’ … instanceId … ‘/modify’,
DumpJson(request, true))

– Upload the modified instance to the Orthanc store
RestApiPost(‘/instances/’, modified)

– Delete the original instance
RestApiDelete(‘/instances/’ … instanceId)
end
end

It works, thanks !