John
John

Reputation: 45

Check whether new contact already exists in Xero

I am using Visual Studio C# winforms and have installed the Nuget wrapper. I am trying to check whether a contact record already exists in the Xero database.

            string expected = "Test";
            var name=api.Contacts
                .Where(string.Format("Name == \"{0}\"", expected))
                .Find()
                .Select(p => p.Name);
             bool exists = (name.All(p => p == expected)

This code is returning True even when the record does not exist. Assistance would be appreciated.

Upvotes: 0

Views: 397

Answers (1)

Morphis
Morphis

Reputation: 31

I've just done something similar myself, albeit slighly adapted for your question, in the original I am just returning the contact rather than a boolean, but it seems to work either way for me

The 'AccountNumber' am I using here comes from a property on an object that gets passed through to this method.

   Contact contact = api.Contacts.Find()
       .Where(w => w.AccountNumber == client.AccountNumber).FirstOrDefault();

   // Create the contact in xero if it doesn't already exist
   if (contact == null)
   {
       exists = false;
   }
   else 
   {
       exists = true;
   }

Upvotes: 1

Related Questions