ObjectScript Tutorial
Exercise 4: Add Storage Procedure
[Home] [Back] [Next]
InterSystems: The power behind what matters   

  1. Start Studio, and open the mydatent routine. The finished routine is also available in the SAMPLES namespace as datent.mac.
  2. Edit the main routine. Call the store procedure after calling display.
        do display()
        do store()
  3. Add an If construct to the $$validPhone function, that checks to see if the phone exists in the index already, just after the line that adds the default area code.
        set:(phone?3n1"-"4n) phone = "617-" _ phone ; add default area code
        if $data( ^PersonI( "Phone", phone)) {
            write !, "Phone number in use"
            quit 0
        else {
            quit phone 
  4. Add the following new store procedure.
    store() [name, phone, intdob]
     ; store the data
      read !, "Store? (y/n): ", yn                 ; see if user wants to store
      if ( yn '= "y" ) {                           ; only go on if user says yes
          write "...not stored."
      set id = $increment( ^PersonD )              ; use $increment to generate a new ID
      set rec = name _ "^" _ phone _ "^" _ intdob  ; concatenate data into a record
      set ^PersonD( id ) = rec                     ; store the record
      set ln = $piece(name, ",", 1),
          fn = $piece(name, ",", 2)                ; break name for storage in index
      /* the next three statements store data in subscripts
         because of the automatic sorting of subscripts,
         this has the effect of building 3 indices: name, phone, and DOB */
      set ^PersonI( "Name", ln, fn, id) = ""       ; store the name
      set ^PersonI( "Phone", phone) = id           ; store the UNIQUE phone
      set ^PersonI( "DOB", intdob, id) = ""        ; store the DOB
      write "...stored"                            ; done
  5. Store some people, and look at the globals with the Management Portal.

Send us comments on this page
Copyright © 1997-2019 InterSystems Corporation, Cambridge, MA