Checking if customer exists in Quantify

Hello,

I know this is a basic question, but I am unable to find any sample code that addresses it - how should I be going about checking if a customer exists in Quantify?

We will be doing a one-way integration of Customers and Vendors into Quantify, so I need to first check if the customer exists, and if so update the existing Business Partner, and if not insert a new Business Partner. I am using C# as my language.

I was thinking of doing something like the following, but I know there is no ‘IsNull’ method in C# (that part is just pseudocode):

// Instantiate customer we are inserting/updating; check if it already exists first
if(IsNull(BusinessPartner.GetBusinessPartnerByNumber(CustomerNumber)))
{
// Create new customer
BusinessPartner customer = BusinessPartner.NewBusinessPartner(PartnerTypes.Customer);

            // Set general customer fields
            customer.AccountingID = CustomerNumber;
            customer.Name = CustomerName;
            customer.PhoneNumber = CustomerPhone;
            customer.EmailAddress = CustomerEmail;
            customer.FaxNumber = CustomerFax;
            customer.PartnerNumber = CustomerNumber;

            customer.Save();
        }
        else
        {
            // Get existing customer
            BusinessPartner customer = BusinessPartner.GetBusinessPartnerByNumber(CustomerNumber);

            // Set general customer fields
            customer.AccountingID = CustomerNumber;
            customer.Name = CustomerName;
            customer.PhoneNumber = CustomerPhone;
            customer.EmailAddress = CustomerEmail;
            customer.FaxNumber = CustomerFax;
            customer.PartnerNumber = CustomerNumber;

            customer.Save();
        }

Am I going about this correctly overall? Are there other methods I need to be utilizing to ensure an accurate update/insert of the customer record?

Finally, what are the required fields needed when importing/updating a customer record in this fashion? We will need to be sure to set values for each of these fields.

Please let me know if there is anything I can clarify on this.

Thanks for the assistance,
Alex

You’re close! To determine if a customer exists you can attempt to load it. If the ID of the item that you’re attempting to load is an empty Guid, then the load was not successful.

// Get existing customer
BusinessPartner customer = 
    BusinessPartner.GetBusinessPartnerByNumber(CustomerNumber);

// Check if it exists
if (customer.BusinessPartnerID == Guid.Empty)
    Debug.Print("Customer not found");

Hello,

This unfortunately does not work - If you attempt to load a brand new customer by using GetBusinessPartnerByNumber, the ID of the item is not an empty Guid: it actually generates a GUID immediately upon running the function (see screenshot - I can provide more details if you’d like). I ran this in debug mode just to confirm exactly what line the Guid was being generated at.

Is there another standard method to check this? If not, what I will likely do is just check the PartnerNumber, since that is not generated immediately upon running GetBusinessPartnerByNumber - this is not perfect, but I think it would do the trick for the most part. I’ll build my code around that to make sure things are in order before proceeding with creating a new customer/editing one.

Thanks so much,
Alex