コード例 #1
0
        private eGetEntryListResult TryQuery(string searchText, string moduleName, List <string> fieldsToSeek)
        {
            eGetEntryListResult queryResult = null;

            string queryText = ConstructQueryTextForModuleName(searchText, moduleName, fieldsToSeek);

            try
            {
                queryResult = clsSuiteCRMHelper.GetEntryList(moduleName, queryText, settings.SyncMaxRecords, "date_entered DESC", 0, false, fieldsToSeek.ToArray());
            }
            catch (System.Exception any)
            {
                Globals.ThisAddIn.Log.Error($"Failure when custom module included (1)\n\tQuery was '{queryText}'", any);

                queryText = queryText.Replace("%", string.Empty);
                try
                {
                    queryResult = clsSuiteCRMHelper.GetEntryList(moduleName, queryText, settings.SyncMaxRecords, "date_entered DESC", 0, false, fieldsToSeek.ToArray());
                }
                catch (Exception secondFail)
                {
                    Globals.ThisAddIn.Log.Error($"Failure when custom module included (2)\n\tQuery was '{queryText}'", secondFail);
                    queryResult = null;
                    throw;
                }
                if (queryResult == null)
                {
                    throw;
                }
            }

            return(queryResult);
        }
コード例 #2
0
        /// <summary>
        /// Fetch records in pages from CRM, and merge them into Outlook.
        /// </summary>
        /// <param name="folder">The folder to be synchronised.</param>
        /// <param name="crmModule">The name of the CRM module to synchronise with.</param>
        /// <param name="untouched">A list of all known Outlook items, from which those modified by this method are removed.</param>
        protected virtual void MergeRecordsFromCrm(Outlook.MAPIFolder folder, string crmModule, HashSet <SyncState <OutlookItemType> > untouched)
        {
            int thisOffset = 0; // offset of current page of entries
            int nextOffset = 0; // offset of the next page of entries, if any.

            /* get candidates for syncrhonisation from SuiteCRM one page at a time */
            do
            {
                /* update the offset to the offset of the next page */
                thisOffset = nextOffset;

                /* fetch the page of entries starting at thisOffset */
                eGetEntryListResult entriesPage = clsSuiteCRMHelper.GetEntryList(crmModule,
                                                                                 String.Format(fetchQueryPrefix, clsSuiteCRMHelper.GetUserId()),
                                                                                 0, "date_start DESC", thisOffset, false,
                                                                                 clsSuiteCRMHelper.GetSugarFields(crmModule));

                /* get the offset of the next page */
                nextOffset = entriesPage.next_offset;

                /* when there are no more entries, we'll get a zero-length entry list and nextOffset
                 * will have the same value as thisOffset */
                AddOrUpdateItemsFromCrmToOutlook(entriesPage.entry_list, folder, untouched, crmModule);
            }while (thisOffset != nextOffset);
        }
コード例 #3
0
        private void populateTree(eGetEntryListResult search_result, string module, TreeNode root_node)
        {
            foreach (eEntryValue _value in search_result.entry_list)
            {
                string s          = string.Empty;
                string key        = string.Empty;
                string valueByKey = string.Empty;
                key = clsSuiteCRMHelper.GetValueByKey(_value, "id");
                s   = clsSuiteCRMHelper.GetValueByKey(_value, "first_name") + " " + clsSuiteCRMHelper.GetValueByKey(_value, "last_name");
                if (s == " ")
                {
                    s = clsSuiteCRMHelper.GetValueByKey(_value, "name");
                }
                string str4 = module;
                if (str4 != null)
                {
                    if (!(str4 == "Contacts") && !(str4 == "Leads"))
                    {
                        if (str4 == "Cases")
                        {
                            goto Label_00DC;
                        }
                        if (str4 == "Bugs")
                        {
                            goto Label_00F0;
                        }
                    }
                    else
                    {
                        valueByKey = clsSuiteCRMHelper.GetValueByKey(_value, "account_name");
                    }
                }
                goto Label_0102;
Label_00DC:
                valueByKey = clsSuiteCRMHelper.GetValueByKey(_value, "case_number");
                goto Label_0102;
Label_00F0:
                valueByKey = clsSuiteCRMHelper.GetValueByKey(_value, "bug_number");
Label_0102:
                if (valueByKey != string.Empty)
                {
                    s = s + " (" + valueByKey + ")";
                }
                if (!root_node.Nodes.ContainsKey(key))
                {
                    TreeNode node = new TreeNode(s)
                    {
                        Name = key,
                        Tag  = key
                    };
                    root_node.Nodes.Add(node);
                }
            }
            if (search_result.result_count <= 3)
            {
                root_node.Expand();
            }
        }
コード例 #4
0
        /// <summary>
        /// Synchronise items in the specified folder with the specified SuiteCRM module.
        /// </summary>
        /// <remarks>
        /// TODO: candidate for refactoring upwards, in concert with ContactSyncing.SyncFolder.
        /// </remarks>
        /// <param name="folder">The folder.</param>
        /// <param name="crmModule">The module.</param>
        private void SyncFolder(Outlook.MAPIFolder folder, string crmModule)
        {
            Log.Info(String.Format("AppointmentSyncing.SyncFolder: '{0}'", crmModule));
            try
            {
                /* this.ItemsSyncState already contains items to be synced. */
                var untouched  = new HashSet <SyncState <Outlook.AppointmentItem> >(this.ItemsSyncState);
                int nextOffset = -1; // offset of the next page of entries, if any.

                for (int iOffset = 0; iOffset != nextOffset; iOffset = nextOffset)
                {
                    /* get candidates for syncrhonisation from SuiteCRM one page at a time */
                    eGetEntryListResult entriesPage = clsSuiteCRMHelper.GetEntryList(crmModule,
                                                                                     String.Format("assigned_user_id = '{0}'", clsSuiteCRMHelper.GetUserId()),
                                                                                     0, "date_start DESC", iOffset, false,
                                                                                     clsSuiteCRMHelper.GetSugarFields(crmModule));

                    nextOffset = entriesPage.next_offset; // get the offset of the next page

                    if (iOffset != nextOffset)
                    {
                        UpdateItemsFromCrmToOutlook(entriesPage.entry_list, folder, untouched, crmModule);
                    }
                }

                eEntryValue[] invited = clsSuiteCRMHelper.getRelationships("Users",
                                                                           clsSuiteCRMHelper.GetUserId(), crmModule.ToLower(),
                                                                           clsSuiteCRMHelper.GetSugarFields(crmModule));
                if (invited != null)
                {
                    UpdateItemsFromCrmToOutlook(invited, folder, untouched, crmModule);
                }

                try
                {
                    var itemsToBeDeletedFromOutlook = untouched.Where(a => a.ExistedInCrm && a.CrmType == crmModule);
                    foreach (var item in itemsToBeDeletedFromOutlook)
                    {
                        RemoveItemAndSyncState(item);
                    }

                    var itemsToBeAddedToCrm = untouched.Where(a => a.ShouldSyncWithCrm && !a.ExistedInCrm && a.CrmType == crmModule);
                    foreach (var item in itemsToBeAddedToCrm)
                    {
                        AddOrUpdateItemFromOutlookToCrm(item.OutlookItem, crmModule);
                    }
                }
                catch (Exception ex)
                {
                    Log.Error("AppointmentSyncing.SyncFolder: Exception", ex);
                }
            }
            catch (Exception ex)
            {
                Log.Error("AppointmentSyncing.SyncFolder: Exception", ex);
            }
        }
コード例 #5
0
        /// <summary>
        /// Synchronise items in the specified folder with the specified SuiteCRM module.
        /// </summary>
        /// <remarks>
        /// TODO: candidate for refactoring upwards, in concert with AppointmentSyncing.SyncFolder.
        /// </remarks>
        /// <param name="folder">The folder.</param>
        private void SyncFolder(Outlook.MAPIFolder folder)
        {
            Log.Info($"ContactSyncing.SyncFolder: '{folder}'");
            try
            {
                if (HasAccess("Contacts", "export"))
                {
                    var untouched  = new HashSet <SyncState <Outlook.ContactItem> >(ItemsSyncState);
                    int nextOffset = -1; // offset of the next page of entries, if any.

                    for (int iOffset = 0; iOffset != nextOffset; iOffset = nextOffset)
                    {
                        eGetEntryListResult entriesPage = clsSuiteCRMHelper.GetEntryList("Contacts",
                                                                                         "contacts.assigned_user_id = '" + clsSuiteCRMHelper.GetUserId() + "'",
                                                                                         0, "date_entered DESC", iOffset, false, clsSuiteCRMHelper.GetSugarFields("Contacts"));
                        nextOffset = entriesPage.next_offset;
                        if (iOffset != nextOffset)
                        {
                            UpdateItemsFromCrmToOutlook(entriesPage.entry_list, folder, untouched);
                        }
                    }
                    try
                    {
                        // Create the lists first, because deleting items changes the value of 'ExistedInCrm'.
                        var syncableButNotOnCrm = untouched.Where(s => s.ShouldSyncWithCrm);
                        var toDeleteFromOutlook = syncableButNotOnCrm.Where(a => a.ExistedInCrm).ToList();
                        var toCreateOnCrmServer = syncableButNotOnCrm.Where(a => !a.ExistedInCrm).ToList();

                        foreach (var item in toDeleteFromOutlook)
                        {
                            LogItemAction(item.OutlookItem, "ContactSyncing.SyncFolder, deleted item");
                            item.OutlookItem.Delete();
                            ItemsSyncState.Remove(item);
                        }

                        foreach (var oItem in toCreateOnCrmServer)
                        {
                            AddOrUpdateItemFromOutlookToCrm(oItem.OutlookItem);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error("ContactSyncing.SyncContacts", ex);
                    }
                }
                else
                {
                    Log.Warn("ContactSyncing.SyncContacts: CRM server denied access to export Contacts");
                }
            }
            catch (Exception ex)
            {
                Log.Error("ContactSyncing.SyncContacts", ex);
            }
        }
コード例 #6
0
        public static eGetEntryListResult GetEntryList(string module, string query, int limit, string order_by, int offset, bool GetDeleted, string[] fields)
        {
            EnsureLoggedIn();
            eGetEntryListResult _result = new eGetEntryListResult();
            object data = new
            {
                @session       = SuiteCRMUserSession.id,
                @module_name   = module,
                @query         = query,
                @order_by      = order_by,
                @offset        = offset,
                @select_fields = fields,
                @max_results   = limit,
                @deleted       = Convert.ToInt32(GetDeleted)
            };

            _result = SuiteCRMUserSession.RestServer.GetCrmResponse <RESTObjects.eGetEntryListResult>("get_entry_list", data);
            if (_result.error != null)
            {
                throw new Exception(_result.error.description);
            }

            if (_result.entry_list != null)
            {
                try
                {
                    Hashtable hashtable = new Hashtable();
                    int       index     = 0;
                    foreach (eEntryValue _value in _result.entry_list)
                    {
                        if (!hashtable.Contains(_value.id))
                        {
                            hashtable.Add(_value.id, _value);
                        }
                        _result.entry_list[index] = null;
                        index++;
                    }
                    int num2 = 0;
                    _result.entry_list   = null;
                    _result.entry_list   = new eEntryValue[hashtable.Count];
                    _result.result_count = hashtable.Count;
                    foreach (DictionaryEntry entry in hashtable)
                    {
                        _result.entry_list[num2] = (eEntryValue)entry.Value;
                        num2++;
                    }
                }
                catch (System.Exception)
                {
                    _result.result_count = 0;
                }
            }

            return(_result);
        }
コード例 #7
0
        public static Hashtable FindAccounts(string val)
        {
            Hashtable           hashtable = new Hashtable();
            string              query     = "accounts.name LIKE '" + val + "%'";
            eGetEntryListResult _result   = GetEntryList("Accounts", query, 200, "date_entered DESC", 0, false, new string[] { "name", "id" });

            if (_result.result_count > 0)
            {
                foreach (eEntryValue _value in _result.entry_list)
                {
                    string valueByKey = string.Empty;
                    string key        = string.Empty;
                    valueByKey = GetValueByKey(_value, "name");
                    key        = GetValueByKey(_value, "id");
                    hashtable.Add(key, valueByKey);
                }
            }
            return(hashtable);
        }
コード例 #8
0
        public string GetID(string sEmailID, string sModule)
        {
            string str5 = "(" + sModule.ToLower() + ".id in (select eabr.bean_id from email_addr_bean_rel eabr INNER JOIN email_addresses ea on eabr.email_address_id = ea.id where eabr.bean_module = '" + sModule + "' and ea.email_address LIKE '%" + SuiteCRMAddIn.clsGlobals.MySqlEscape(sEmailID) + "%'))";

            Log.Info("-------------------GetID-----Start---------------");

            Log.Info("\tstr5=" + str5);

            Log.Info("-------------------GetID-----End---------------");

            string[] fields = new string[1];
            fields[0] = "id";
            eGetEntryListResult _result = clsSuiteCRMHelper.GetEntryList(sModule, str5, settings.SyncMaxRecords, "date_entered DESC", 0, false, fields);

            if (_result.result_count > 0)
            {
                return(clsSuiteCRMHelper.GetValueByKey(_result.entry_list[0], "id"));
            }
            return(String.Empty);
        }
コード例 #9
0
 /// <summary>
 /// Add a node beneath this parent representing this search result in this module.
 /// </summary>
 /// <param name="searchResult">A search result</param>
 /// <param name="module">The module in which the search was performed</param>
 /// <param name="parent">The parent node beneath which the new node should be added.</param>
 private void PopulateTree(eGetEntryListResult searchResult, string module, TreeNode parent)
 {
     foreach (eEntryValue entry in searchResult.entry_list)
     {
         string key = clsSuiteCRMHelper.GetValueByKey(entry, "id");
         if (!parent.Nodes.ContainsKey(key))
         {
             TreeNode node = new TreeNode(ConstructNodeName(module, entry))
             {
                 Name = key,
                 Tag  = key
             };
             parent.Nodes.Add(node);
         }
     }
     if (searchResult.result_count <= 3)
     {
         parent.Expand();
     }
 }
コード例 #10
0
        public void Search(string query)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                List <string> list = new List <string> {
                    "Accounts", "Contacts", "Leads", "Bugs", "Projects", "Cases", "Opportunties"
                };
                this.tsResults.CheckBoxes = true;
                if (query == string.Empty)
                {
                    MessageBox.Show("Please enter some text to search", "Invalid search", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Cursor = Cursors.Default;
                }
                else
                {
                    query = query.TrimStart(new char[0]);
                    string[] strArray = query.Split(new char[] { ' ' });
                    string   usString = strArray[0];
                    string   str2     = string.Empty;
                    string   str3     = "OR";
                    if (strArray.Length > 1)
                    {
                        str2 = strArray[1];
                        str3 = "AND";
                    }
                    else
                    {
                        str2 = strArray[0];
                    }
                    foreach (ListViewItem item in this.lstViewSearchModules.Items)
                    {
                        try
                        {
                            TreeNode            node;
                            eGetEntryListResult _result;
                            if (!item.Checked)
                            {
                                continue;
                            }
                            string text = item.Tag.ToString();

                            if (!(text != "All"))
                            {
                                continue;
                            }
                            if (this.tsResults.Nodes[text] == null)
                            {
                                node = new TreeNode(text)
                                {
                                    Tag  = "root_node",
                                    Name = text
                                };
                                this.tsResults.Nodes.Add(node);
                            }
                            else
                            {
                                node = this.tsResults.Nodes[text];
                            }
                            string   str5   = text.ToLower() + ".name LIKE '%" + clsGlobals.MySqlEscape(query) + "%'";
                            string[] fields = new string[6];
                            fields[0] = "id";
                            fields[1] = "first_name";
                            fields[2] = "last_name";
                            fields[3] = "name";
                            string str6 = text;
                            if (str6 != null)
                            {
                                if (!(str6 == "Contacts"))
                                {
                                    if (str6 == "Leads")
                                    {
                                        goto Label_030F;
                                    }
                                    if (str6 == "Cases")
                                    {
                                        goto Label_038F;
                                    }
                                    if (str6 == "Bugs")
                                    {
                                        goto Label_03E4;
                                    }
                                    if (str6 == "Accounts")
                                    {
                                        goto Label_03AS;
                                    }
                                }
                                else
                                {
                                    str5      = "(contacts.first_name LIKE '%" + clsGlobals.MySqlEscape(usString) + "%' " + str3 + " contacts.last_name LIKE '%" + clsGlobals.MySqlEscape(str2) + "%') OR (contacts.id in (select eabr.bean_id from email_addr_bean_rel eabr INNER JOIN email_addresses ea on eabr.email_address_id = ea.id where eabr.bean_module = 'Contacts' and ea.email_address LIKE '%" + clsGlobals.MySqlEscape(query) + "%'))";
                                    fields[4] = "account_name";
                                }
                            }
                            goto Label_0446;
                            Label_030F :;
                            str5      = "(leads.first_name LIKE '%" + clsGlobals.MySqlEscape(usString) + "%' " + str3 + " leads.last_name LIKE '%" + clsGlobals.MySqlEscape(str2) + "%')  OR (leads.id in (select eabr.bean_id from email_addr_bean_rel eabr INNER JOIN email_addresses ea on eabr.email_address_id = ea.id where eabr.bean_module = 'Leads' and ea.email_address LIKE '%" + clsGlobals.MySqlEscape(query) + "%'))";
                            fields[4] = "account_name";
                            goto Label_0446;
                            Label_038F :;
                            str5      = "(cases.name LIKE '%" + clsGlobals.MySqlEscape(query) + "%' OR cases.case_number LIKE '" + clsGlobals.MySqlEscape(query) + "')";
                            fields[4] = "case_number";
                            goto Label_0446;
                            Label_03E4 :;
                            str5      = "(bugs.name LIKE '%" + clsGlobals.MySqlEscape(query) + "%' " + str3 + " bugs.bug_number LIKE '" + clsGlobals.MySqlEscape(query) + "')";
                            fields[4] = "bug_number";
                            goto Label_0446;
                            Label_03AS :;
                            str5      = "(accounts.name LIKE '%" + clsGlobals.MySqlEscape(usString) + "%') OR (accounts.id in (select eabr.bean_id from email_addr_bean_rel eabr INNER JOIN email_addresses ea on eabr.email_address_id = ea.id where eabr.bean_module = 'Accounts' and ea.email_address LIKE '%" + clsGlobals.MySqlEscape(query) + "%'))";
                            fields[4] = "account_name";
Label_0446:
                            _result = clsSuiteCRMHelper.GetEntryList(text, str5, settings.SyncMaxRecords, "date_entered DESC", 0, false, fields);
                            if (_result.result_count > 0)
                            {
                                this.populateTree(_result, text, node);
                            }
                            else if (!list.Contains(text) && clsSuiteCRMHelper.GetFields(text).Contains("first_name"))
                            {
                                str5 = "(" + text.ToLower() + ".first_name LIKE '%" + clsGlobals.MySqlEscape(usString) + "%' " + str3 + " " + text.ToLower() + ".last_name LIKE '%" + clsGlobals.MySqlEscape(str2) + "%')  OR (" + text.ToLower() + ".id in (select eabr.bean_id from email_addr_bean_rel eabr INNER JOIN email_addresses ea on eabr.email_address_id = ea.id where eabr.bean_module = '" + text + "' and ea.email_address LIKE '%" + clsGlobals.MySqlEscape(query) + "%'))";
                                eGetEntryListResult _result2 = clsSuiteCRMHelper.GetEntryList(text, str5, settings.SyncMaxRecords, "date_entered DESC", 0, false, fields);
                                if (_result2.result_count > 0)
                                {
                                    this.populateTree(_result2, text, node);
                                }
                            }
                            if (node.GetNodeCount(true) <= 0)
                            {
                                node.Remove();
                            }
                        }
                        catch (System.Exception ex)
                        {
                            ex.Data.Clear();
                            this.tsResults.Nodes.Clear();
                        }
                    }
                    if (this.tsResults.Nodes.Count <= 0)
                    {
                        TreeNode node2 = new TreeNode("No results found")
                        {
                            Name = "No results",
                            Text = "No Result"
                        };
                        this.tsResults.Nodes.Add(node2);
                        this.tsResults.CheckBoxes = false;
                    }
                    this.txtSearch.Enabled = true;
                    this.Cursor            = Cursors.Default;
                }
            }
            catch (System.Exception ex)
            {
                ex.Data.Clear();

                this.tsResults.Nodes.Clear();
                this.Cursor = Cursors.Default;
                TreeNode node2 = new TreeNode("No results found")
                {
                    Name = "No results",
                    Text = "No Result"
                };
                this.tsResults.Nodes.Add(node2);
                this.tsResults.CheckBoxes = false;
            }
        }
コード例 #11
0
        private void SyncFolder(Outlook.MAPIFolder tasksFolder)
        {
            Log.Warn("SyncTasks");
            Log.Warn("My UserId= " + clsSuiteCRMHelper.GetUserId());
            try
            {
                var untouched = new HashSet <SyncState <Outlook.TaskItem> >(ItemsSyncState);
                int iOffset   = 0;
                while (true)
                {
                    eGetEntryListResult _result2 = clsSuiteCRMHelper.GetEntryList("Tasks", String.Empty,
                                                                                  0, "date_start DESC", iOffset, false, clsSuiteCRMHelper.GetSugarFields("Tasks"));
                    var nextOffset = _result2.next_offset;
                    if (iOffset == nextOffset)
                    {
                        break;
                    }

                    foreach (var oResult in _result2.entry_list)
                    {
                        try
                        {
                            var state = UpdateFromCrm(tasksFolder, oResult);
                            if (state != null)
                            {
                                untouched.Remove(state);
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Error("ThisAddIn.SyncTasks", ex);
                        }
                    }

                    iOffset = nextOffset;
                    if (iOffset == 0)
                    {
                        break;
                    }
                }
                try
                {
                    var lItemToBeDeletedO = untouched.Where(a => a.ExistedInCrm);
                    foreach (var oItem in lItemToBeDeletedO)
                    {
                        oItem.OutlookItem.Delete();
                        ItemsSyncState.Remove(oItem);
                    }

                    var lItemToBeAddedToS = untouched.Where(a => !a.ExistedInCrm);
                    foreach (var oItem in lItemToBeAddedToS)
                    {
                        AddToCrm(oItem.OutlookItem);
                    }
                }
                catch (Exception ex)
                {
                    Log.Error("ThisAddIn.SyncTasks", ex);
                }
            }
            catch (Exception ex)
            {
                Log.Error("ThisAddIn.SyncTasks", ex);
            }
        }
コード例 #12
0
 public static eGetEntryListResult GetEntryList(string module, string query, int limit, string order_by, int offset, bool GetDeleted, string[] fields)
 {
     string strUserID = clsSuiteCRMHelper.GetUserId();
     if (strUserID == "")
     {
         SuiteCRMUserSession.Login();
     }
     eGetEntryListResult _result = new eGetEntryListResult();
     try
     {
         object data = new
         {
             @session = SuiteCRMUserSession.id,
             @module_name = module,
             @query = query,
             @order_by = order_by,
             @offset = offset,
             @select_fields = fields,
             @max_results = limit,
             @deleted = Convert.ToInt32(GetDeleted)
         };
         _result = clsGlobals.GetResponse<RESTObjects.eGetEntryListResult>("get_entry_list", data);
         if (_result.error != null)
         {
             throw new Exception(_result.error.description);
         }
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
     try
     {
         Hashtable hashtable = new Hashtable();
         int index = 0;
         foreach (eEntryValue _value in _result.entry_list)
         {
             if (!hashtable.Contains(_value.id))
             {
                 hashtable.Add(_value.id, _value);
             }
             _result.entry_list[index] = null;
             index++;
         }
         int num2 = 0;
         _result.entry_list = null;
         _result.entry_list = new eEntryValue[hashtable.Count];
         _result.result_count = hashtable.Count;
         foreach (DictionaryEntry entry in hashtable)
         {
             _result.entry_list[num2] = (eEntryValue)entry.Value;
             num2++;
         }
     }
     catch (System.Exception)
     {
         _result.result_count = 0;
     }
     return _result;
 }
コード例 #13
0
        public void Search(string searchText)
        {
            using (WaitCursor.For(this))
                try
                {
                    List <string> list = new List <string> {
                        "Accounts", "Contacts", "Leads", "Bugs", "Projects", "Cases", "Opportunties"
                    };
                    this.tsResults.CheckBoxes = true;
                    if (searchText == string.Empty)
                    {
                        MessageBox.Show("Please enter some text to search", "Invalid search", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        searchText = searchText.TrimStart(new char[0]);
                        string[] strArray = searchText.Split(new char[] { ' ' });
                        string   usString = strArray[0];
                        string   str2     = string.Empty;
                        string   str3     = "OR";
                        if (strArray.Length > 1)
                        {
                            str2 = strArray[1];
                            str3 = "AND";
                        }
                        else
                        {
                            str2 = strArray[0];
                        }
                        foreach (ListViewItem item in this.lstViewSearchModules.Items)
                        {
                            try
                            {
                                TreeNode            node;
                                eGetEntryListResult queryResult;
                                if (!item.Checked)
                                {
                                    continue;
                                }
                                string moduleName = item.Tag.ToString();

                                if (moduleName != "All")
                                {
                                    if (this.tsResults.Nodes[moduleName] == null)
                                    {
                                        node = new TreeNode(moduleName)
                                        {
                                            Tag  = "root_node",
                                            Name = moduleName
                                        };
                                        this.tsResults.Nodes.Add(node);
                                    }
                                    else
                                    {
                                        node = this.tsResults.Nodes[moduleName];
                                    }
                                    string[] fields = new string[6];
                                    fields[0] = "id";
                                    fields[1] = "first_name";
                                    fields[2] = "last_name";
                                    fields[3] = "name";

                                    string queryText = ConstructQueryTextForModuleName(searchText, usString, str2, str3, moduleName, fields);

                                    try
                                    {
                                        queryResult = clsSuiteCRMHelper.GetEntryList(moduleName, queryText, settings.SyncMaxRecords, "date_entered DESC", 0, false, fields);
                                    }
                                    catch (System.Exception any)
                                    {
                                        Globals.ThisAddIn.Log.Error($"Failure when custom module included (1)\n\tQuery was '{queryText}'", any);
                                        // Swallow exception(!)
                                        try {
                                            queryResult = clsSuiteCRMHelper.GetEntryList(moduleName, queryText.Replace("%", ""), settings.SyncMaxRecords, "date_entered DESC", 0, false, fields);
                                        }
                                        catch (Exception secondFail)
                                        {
                                            queryText = queryText.Replace("%", "");
                                            Globals.ThisAddIn.Log.Error($"Failure when custom module included (2)\n\tQuery was '{queryText}'", secondFail);
                                            MessageBox.Show(
                                                $"An error was encountered while querying module '{moduleName}'. The error has been logged",
                                                "Query error",
                                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                            queryResult = null;
                                        }
                                    }
                                    if (queryResult != null && queryResult.result_count > 0)
                                    {
                                        this.populateTree(queryResult, moduleName, node);
                                    }
                                    else if (!list.Contains(moduleName) && clsSuiteCRMHelper.GetFields(moduleName).Contains("first_name"))
                                    {
                                        queryText = "(" + moduleName.ToLower() + ".first_name LIKE '%" + clsGlobals.MySqlEscape(usString) + "%' " + str3 + " " + moduleName.ToLower() + ".last_name LIKE '%" + clsGlobals.MySqlEscape(str2) + "%')  OR (" + moduleName.ToLower() + ".id in (select eabr.bean_id from email_addr_bean_rel eabr INNER JOIN email_addresses ea on eabr.email_address_id = ea.id where eabr.bean_module = '" + moduleName + "' and ea.email_address LIKE '%" + clsGlobals.MySqlEscape(searchText) + "%'))";
                                        eGetEntryListResult _result2 = clsSuiteCRMHelper.GetEntryList(moduleName, queryText, settings.SyncMaxRecords, "date_entered DESC", 0, false, fields);
                                        if (_result2.result_count > 0)
                                        {
                                            this.populateTree(_result2, moduleName, node);
                                        }
                                    }
                                    if (node.GetNodeCount(true) <= 0)
                                    {
                                        node.Remove();
                                    }
                                }
                            }
                            catch (System.Exception any)
                            {
                                Globals.ThisAddIn.Log.Error("Failure when custom module included (3)", any);

                                // Swallow exception(!)
                                this.tsResults.Nodes.Clear();
                            }
                        }
                        if (this.tsResults.Nodes.Count <= 0)
                        {
                            TreeNode node2 = new TreeNode("No results found")
                            {
                                Name = "No results",
                                Text = "No Result"
                            };
                            this.tsResults.Nodes.Add(node2);
                            this.tsResults.CheckBoxes = false;
                        }
                        this.txtSearch.Enabled = true;
                    }
                }
                catch (System.Exception)
                {
                    // Swallow exception(!)

                    this.tsResults.Nodes.Clear();
                    TreeNode node2 = new TreeNode("No results found")
                    {
                        Name = "No results",
                        Text = "No Result"
                    };
                    this.tsResults.Nodes.Add(node2);
                    this.tsResults.CheckBoxes = false;
                }
        }