[OutSystems Now] Trying to find does the phone number already exist in contacts

[OutSystems Now] Trying to find does the phone number already exist in contacts

  
Forge Component
(34)
Published on 24 Nov (2 weeks ago) by OutSystems
34 votes
Published on 24 Nov (2 weeks ago) by OutSystems
Hello, I m new to outsystems. I came across this awesome forge component “outsystemsNow” and I want to improve it. I m working on add contact component. I already added additional contact fields and now I m trying to add find function, but somehow I cant reach(cheek) did contact was found or not.Help would be really appreciated , thanks in advance Tomas
Code as follows:

function OutSystemsNative_AddToContacts(firstName, lastName, mobilePhone, email, mainAddress, streetAddress, city, region, postalCode, country, orgName, orgDept, orgTitle) {
    
    if ((window.OutSystemsNowWindowsVersion != 'undefined' && window.OutSystemsNowWindowsVersion )){ // windows version
        // Windows OS API
        // {"plugin_name" : "AddContact", 
        //  "plugin_params": '{ "firstName": "'+firstName+'",  "lastName" : "'+lastName+'", "mobilePhone": "'+mobilePhone+'" }', 
        //  "success_action" : "OutSystemsNative_OnCreateContactSuccessWindows", 
        //  "success_params" : "",
        //  "error_action" : "OutSystemsNative_OnCreateContactFailWindows",
        //  "error_params" : ""}
                
        OutSystemsNative_WindowsNotify("AddContact", 
                '{ "firstName": "'+firstName+'",  "lastName" : "'+lastName+'", "mobilePhone": "'+mobilePhone+'", "email": "'+email+'", "mainAddress": "'+mainAddress+'", "streetAddress": "'+streetAddress+'", "city": "'+city+'", "region": "'+region+'", "postalCode": "'+postalCode+'", "country": "'+country+'", "orgName": "'+orgName+'", "orgDept": "'+orgDept+'", "orgTitle": "'+orgTitle+'" }', 
                "OutSystemsNative_OnCreateContactSuccessWindows", 
                null, 
                "OutSystemsNative_OnCreateContactFailWindows", 
                null);
                
    } else{    

        if (!navigator.contacts) {
            alert('Contacts API not supported', 'Error');
            return false;
        }
        //find
        
        var options = new ContactFindOptions();
        options.filter = mobilePhone; 
        var field = ["phoneNumbers"];
        navigator.contacts.find(field, onSuccess, onError, options);
        
        
        //find
       
        var contact = navigator.contacts.create();
        contact.name = {givenName: firstName, familyName: lastName};
        var phoneNumbers = [];    
        phoneNumbers[0] = new ContactField('mobile', mobilePhone, true);
        contact.phoneNumbers = phoneNumbers;
        var emails = [];
        emails[0] = new ContactField('work', email, true);
        contact.emails = emails;
        
        var addresses = [];
        addresses[0] = new ContactAddress(true, 'work',mainAddress, streetAddress, city, region, postalCode, country);
        contact.addresses = addresses;
        
        var organizations = [];
        organizations[0] = new ContactOrganization(true, 'work', orgName, orgDept, orgTitle);
        contact.organizations = organizations;
        
        
        contact.save(OutSystemsNative_OnCreateContactSuccess,
                     OutSystemsNative_OnCreateContactFail);
          
    }        
    
    return true;
};

// Windows

function OutSystemsNative_OnCreateContactSuccessWindows(args) {
    OutSystemsNative_OnCreateContactSuccess();
}

function OutSystemsNative_OnCreateContactFailWindows(args) {   
    OutSystemsNative_OnCreateContactFail();
}

// iOS & Android

function OutSystemsNative_OnCreateContactSuccess("Contact created") {
}

function OutSystemsNative_OnCreateContactFail() {   
    OutSystemsNow_ShowErrorFeedback('Contact not created. Please check your application permissions, in your device settings.')  
}

function onSuccess() {
    OutSystemsNow_ShowSuccessFeedback(contacts[0].phoneNumber)     // trying to reach what i found

}

function onError(contactError) {
    OutSystemsNow_ShowSuccessFeedback(contacts[0].phoneNumbers)
}


// Preparation
function OutSystemsNative_PrepareCreateContact(buttonId, firstName, lastName, phone, email, mainAddress, streetAddress, city, region, postalCode, country, orgName, orgDept, orgTitle) {
    var osNowAddContactButton = $('#' + buttonId);
    osNowAddContactButton.off('click.OSNowNativeAddContact');
    osNowAddContactButton.on('click.OSNowNativeAddContact', function() {
        OutSystemsNative_AddToContacts(firstName, lastName, phone, email, mainAddress, streetAddress, city, region, postalCode, country, orgName, orgDept, orgTitle);
        return false;
    });
}
Solution
Hello Tomas.

OutSystems Now uses a cordova plugin to interact with the Contacts list. Have you tried the example in the documentation?

Thanks
Solution