Skip to main content

Exercise 2: Add Storage Subroutine

  1. Start Studio, and open the myBASdatent routine. The finished routine is also available in the SAMPLES namespace as BASdatent2.BAS.

  2. Add an If statement to the validPhone function, that checks to see if the phone exists in the index already.

    
    public function validPhone(phone As %String) As %String
    'validate a phone - just checks for 3 pieces using "-" and length
    'returns 0 for an invalid phone and writes error message
    'else returns unchanged phone with default area code added
        if (len(phone) = 8 and len(phone, "-") = 2) then
            phone = "617-" & phone ' add default area code
        end if
        
        if (len(phone) = 12 and len(phone, "-") = 3) then
            if exists( ^PersonI( "Phone", phone)) then
                print "Phone number in use" : println
                return 0
            else
                return phone
            end if        
        else
            print "###-###-#### or ###-####" : println
            return 0
        end if
    end function
    
  3. Add the following new store subroutine.

    
    public sub store()
    ' store the data
        dim yn, id, rec, ln, fn
    
        input "Store? (y/n): ", yn ' see if user wants to store
        ' only go on if user says yes
        if ( yn <> "y" ) then
            println "...not stored."
            exit sub
        end if
     
        id = increment(^PersonD) ' generate a new id
        rec = name & "^" & phone & "^" & intdob ' concatenate data into a record
        ^PersonD( id ) = rec ' store the record
    
        ' break name for storage in index
        ln = piece(name, ",", 1) : fn = piece(name, ",", 2)
     
        ' 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
        ^PersonI( "Name", ln, fn, id) = "" ' store the name
        ^PersonI( "Phone", phone) = id     ' store the UNIQUE phone
        ^PersonI( "DOB", intdob, id) = ""  ' store the DOB
        println "...stored"                ' done
    
    end sub
    
  4. Store some people, and look at the globals with the Management Portal.

FeedbackOpens in a new tab