Contacts example


The index.html file containing JavaScript that makes use of the Contact Database API functionality is listed below.

<!DOCTYPE html>
<html manifest="cache.mf">
<head>
    <script type="text/javascript">

        var now = new Date();
        var nowUtc = new Date(now.getUTCFullYear(), now.getUTCMonth(),
                     now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(),
                     now.getUTCSeconds());
        var dateStampGlobalVariable = nowUtc.toJSON().toString();

        function printText(str) {
            var d = document.getElementById("results");
            d.appendChild(document.createTextNode(str));
            d.appendChild(document.createElement('br'));
            d.scrollTop = d.scrollHeight;
        }
        ;

        function clearOutput() {
            var d = document.getElementById("results");
            d.innerHTML = "";
        }
        ;

        function saveContact() {
            var newContact = {
                //id,
                readOnly: true,
                //lastUpdated, read-only
                name: ["name"],
                honorificPrefix: ["honorificPrefix"],
                givenName: ["givenName"],
                additionalName: ["additionalName"],
                // {String[]}          Only first item is supported on Android/iOS.
                familyName: ["familyName"],
                // {String[]}          Only first item is supported on Android/iOS.
                honorificSuffix: ["honorificSuffix"],
                // {String[]}          Only first item is supported on Android/iOS.
                nickname: ["nickname"],
                // {String[]}          Only first item is supported on Android/iOS.
                email: [
                    {value: '[email protected]', type: "work"},
                    {value: '[email protected]', type: "home"}
                ],
                // {ContactField[]}
                //photo,
                // {String[]}          Data URI, only first item is supported on Android/iOS.
                // On iOS thumbnails are returned for performance reasons.
                url: [
                    {value: 'http/google.com', type: "work"},
                    {value: 'http://onet.pl', type: "home"}
                ],
                // {ContactField[]}
                //categories,
                // {String[]}          Not Supported
                adr: [
                    {type: "work", streetAddress: "streetAddress", postalCode: "11-222"  },
                    {type: "home", streetAddress: "streetAddress2", postalCode: "11-333" }
                ],
                // {ContactAddress[]}
                tel: [
                    {type: "work", value: "123456789"},
                    {type: "home", value: "987654321"}
                ],
                // {ContactTelField[]} Carrier name is not supported on iOS.
                org: ["Organization"],
                // {String[]}          Only first item is supported on Android/iOS.
                jobTitle: ["jobTitle"],
                // {String[]}          Only first item is supported on Android/iOS.
                bday: dateStampGlobalVariable,
                note: ["Lorem ipsum dolor sit amet, consectetur adipiscing elit."],
                // {String[]}          Only first item is supported on iOS.
                //impp,
                // {ContactField[]}    Not supported.
                anniversary: dateStampGlobalVariable
                // {Date}
                //sex,
                // {String}            Not supported (no corresponding native field).
                //genderIdentity,
                // {String}            Not supported (no corresponding native field).
            };

            var callbacks = {
                onSuccess: function (contact) {
                    printText("save onSuccess called!");
                    printText(JSON.stringify(contact));
                },
                onFailure: function (err) {
                    printText("save onFailure called!");
                    printText(err.code);
                    printText(err.description);
                }
            };

            var contacts = launchbox.Contacts;
            contacts.save(newContact, callbacks);
        }
        ;

        function findContact(){
            var options = {
                filterValue: "jobTitle",
                filterOp: "is",
                filterBy: ["jobTitle"],
                sortBy: "givenName",
                sortOrder: "ascending",
                filterLimit: 100
            };

            var callbacks = {
                onSuccess: function (contact) {
                    printText("find onSuccess called!");
                    printText(JSON.stringify(contact));
                },
                onFailure: function () {
                    printText("find onFailure called!");
                }
            };

            var contacts = launchbox.Contacts;
            contacts.find(options, callbacks);
        };

        function removeContact(){

            var options = {
                filterValue: "jobTitle",
                filterOp: "is",
                filterBy: ["jobTitle"],
                sortBy: "givenName",
                sortOrder: "ascending",
                filterLimit: 100
            };

            var removeCallbacks = {
                onSuccess: function (contact) {
                    printText("remove onSuccess called!");
                    printText(JSON.stringify(contact));
                },
                onFailure: function () {
                    printText("remove onFailure called!");
                }
            };

            var findCallbacks = {
                onSuccess: function (contact) {
                    printText("find onSuccess called!");
                    printText(JSON.stringify(contact));
                    window.launchbox.Contacts.remove(contacts[0], removeCallbacks);
                },
                onFailure: function () {
                    printText("find onFailure called!");
                }
            };

            var contacts = launchbox.Contacts;
            contacts.find(options, findCallbacks);
        }

    </script>
</head>
<body>
<input type="button" onclick="clearOutput();" value="Clear output">
<hr/>
<p>Contacts Launchbox API</p>
<input type="button" onclick="saveContact();" value="save Contact">
<input type="button" onclick="findContact();" value="find Contact">
<input type="button" onclick="removeContact();" value="remove Contact">
<hr/>
<div id="results"></div>
</body>
</html>

The contents of the cache manifest file called manifest.appcache for this application are listed below:

CACHE MANIFEST

CACHE:
index.html

NETWORK:
*

Related topics

Public API reference
Contacts
Legal notice | Copyright © 2019 and Confidential to Pegasystems Inc. All rights reserved
Pega Community | Feedback
Advanced...