Skip to main content

Updating the Phone Numbers

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:

    • Like GetPhoneNumbers, its code is ObjectScript (COS).

    • Like GetPhoneNumbers, it is a ZenMethod that executes on the server. It will also be invoked from a method executing on the client (Web browser).

    • The method receives the id of the contact being saved from the calling code. It then opens the contact and deletes its phone numbers. It uses the %page variable to retrieve the phone numbers from the Zen form. It parses the phone numbers and creates PhoneNumber objects, which it then associates with the Contact object.

    • It uses several COS library functions, including $ZSTRIP, $PIECE, and $LENGTH. See the note below for more information on COS library functions.

  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');
        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:

    • Like getRecord, the code is JavaScript and the method executes on the client (Web browser).

    • It uses the save method of the Zen form object (%ZEN.Component.formOpens in a new tab) to save the form. This method works in conjunction with the Zen MVC framework to save the form data to the database. See the note below for more information on Zen MVC. The save method returns the id of the saved contact.

    • It tests that the initial save of the contact was successful, and then uses our new line of code to save the phone numbers with the contact.


For more information on COS library functions, please read ObjectScript Functions in the Caché ObjectScript Reference. See also Using Caché ObjectScript and the ObjectScript Tutorial.

For more information on the Zen Model-View-Controller (MVC) framework, please read Model View Controller in Using Zen Components.

FeedbackOpens in a new tab