Caché QuickStart Tutorial
Updating the Phone Numbers
[Back] [Next]
   
Server:docs2
Instance:LATEST
User:UnknownUser
 
-
Search:    

As in the case of displaying phone numbers, it is up to us to provide our form with the logic for saving phone numbers. Again, we need to add a server-side ZenMethod that saves the phone numbers and a line of code in a client side method to invoke the ZenMethod.

Here are the steps:
  1. Add the following method to ContactForm.
    Method SavePhoneNumbers(id As %String) As %Status [ZenMethod]
    {
        Set tContact = ##class(ContactDB.Contact).%OpenId(id)
        if $IsObject(tContact) 
        {
          Do tContact.PhoneNumbers.Clear()
          Set PhoneNumbers=$ZSTRIP(%page.%GetValueById("PhoneNumbers"),"*W")
          for i=1:1:$LENGTH(PhoneNumbers,$char(10))
           {
             Set pn=$PIECE(PhoneNumbers,$char(10),i)
             if (pn'="")
             {
                Set phoneNumber = ##class(ContactDB.PhoneNumber).%New()
                Set phoneNumber.Type = $PIECE(pn,":",1) 
                Set phoneNumber.Number = $PIECE(pn,":",2)
                Set phoneNumber.Contact=tContact
              }   
            }
            Set tSC=tContact.%Save()
        }
        Quit tSC
    }
    
    Note the following about the method:
  2. Next, add the following line of code to the SaveRec method. This method was originally generated by the Zen Form wizard. Find saveRec and expand its first else block to include this line.
    if (zenPage.SavePhoneNumbers(save)!=1) ok = false;
    
    The method should now look like this.
    ClientMethod saveRec(ok) As %Status [ Language = javascript ]
    {
      if (ok == true) {
        var form=zen('ContactForm');
        var save=form.save();
    
        if (save.length == 0) ok = false;
        else {
          var class=zen('mvc').getModelClass();
          //added for saving phone numbers
          if (zenPage.SavePhoneNumbers(save)!=1) ok = false;
        }
      }
      if (ok == true) alert(zenText('SuccessSaveRecord'));
      else alert(zenText('FailedDataValidation'));
      return ok;
    }
    
    Note the following about the method:
Note:
For more information on COS library functions, please read Caché ObjectScript Functions in the Caché ObjectScript Reference. See also Using Caché ObjectScript and the Caché ObjectScript Tutorial.
For more information on the Zen Model-View-Controller (MVC) framework, please read Model View Controller in Using Zen Components.