Class ObjectScript.Lookup3
{
/// save the updated record (start a txn, updating data and index globals using set and kill, commit txn)
ClassMethod Update(id as %Integer, currentdata as %String, ByRef newanswers as %String)
{
read !, "Store updates? (y/n): ", yn // ask if user wants to store
// only go on if user says yes
if ((yn '= "y") && (yn '= "Y")) {
write "...not stored."
quit
}
// get current and new data for comparisons
set $listbuild(currentname, currentphone, currentintdob) = currentdata
set currentlast = $piece(currentname, ",", 1), currentfirst = $piece(currentname, ",", 2)
set $listbuild(newname, newphone, newintdob) = newanswers
set newlast = $piece(newname, ",", 1), newfirst = $piece(newname, ",", 2)
// update all globals inside a transaction
// only update indexes if the data was changed
tstart
set ^PersonD(id) = newanswers
if (newname '= currentname) {
// kill old name and add new name to index
kill ^PersonI("Name", currentlast, currentfirst, id)
set ^PersonI("Name", newlast, newfirst, id) = ""
}
if (newphone '= currentphone) {
// kill old phone and add new phone to index
kill ^PersonI("Phone", currentphone)
set ^PersonI("Phone", newphone) = id
}
if (newintdob '= currentintdob) {
// kill old dob and add new dob to index
kill ^PersonI("DOB", currentintdob, id)
set ^PersonI("DOB", newintdob, id) = ""
}
tcommit // commit the transaction
write "...updated."
}
}