Esempio n. 1
0
        private EntryList TryQuery(string searchText, string moduleName, List <string> fieldsToSeek)
        {
            EntryList queryResult = null;

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

            try
            {
                queryResult = RestAPIWrapper.GetEntryList(moduleName, queryText, Properties.Settings.Default.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 = RestAPIWrapper.GetEntryList(moduleName, queryText, Properties.Settings.Default.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);
        }
Esempio n. 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 */
                EntryList entriesPage = RestAPIWrapper.GetEntryList(crmModule,
                                                                    String.Format(fetchQueryPrefix, RestAPIWrapper.GetUserId()),
                                                                    0, "date_start DESC", thisOffset, false,
                                                                    RestAPIWrapper.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);
        }
Esempio n. 3
0
 /// <summary>
 /// Specialisation: also fetch names and email ids of recipients.
 /// </summary>
 /// <param name="offset">The offset into the resultset at which the page begins.</param>
 /// <returns>A set of entries.</returns>
 protected override EntryList GetEntriesPage(int offset)
 {
     return(RestAPIWrapper.GetEntryList(this.DefaultCrmModule,
                                        String.Format(fetchQueryPrefix, RestAPIWrapper.GetUserId()),
                                        Properties.Settings.Default.SyncMaxRecords, "date_start DESC", offset, false,
                                        RestAPIWrapper.GetSugarFields(this.DefaultCrmModule), new[] {
         new { @name = "users", @value = new[] { "id", "email1", "phone_work" } },
         new { @name = "contacts", @value = new[] { "id", "account_id", "email1", "phone_work" } },
         new { @name = "leads", @value = new[] { "id", "email1", "phone_work" } }
     }));
 }
        private void btnSearch_Click(object sender, EventArgs e)
        {
            string[] strArray = new string[2];
            string   str      = "OR";

            using (WaitCursor.For(this))
            {
                if (this.txtSearch.Text == string.Empty)
                {
                    MessageBox.Show("Please enter something to search for", "Error");
                }
                else
                {
                    this.lstViewResults.Items.Clear();
                    string[] strArray2 = new string[] { "Leads", ContactSynchroniser.CrmModule };
                    if (this.txtSearch.Text.Contains <char>(' '))
                    {
                        strArray = this.txtSearch.Text.Split(new char[] { ' ' });
                    }
                    else
                    {
                        strArray[0] = this.txtSearch.Text;
                        strArray[1] = this.txtSearch.Text;
                    }
                    if ((strArray[1] != string.Empty) && (strArray[0] != strArray[1]))
                    {
                        str = "AND";
                    }
                    foreach (string str2 in strArray2)
                    {
                        string query = "(" + str2.ToLower() + ".first_name LIKE '%" + strArray[0] + "%' " + str + " " + str2.ToLower() + ".last_name LIKE '%" + strArray[1] + "%')";
                        bool   flag1 = str2 == ContactSynchroniser.CrmModule;
                        if (this.cbMyItems.Checked)
                        {
                            string str8 = query;
                            query = str8 + "AND " + str2.ToLower() + ".assigned_user_id = '" + Globals.ThisAddIn.SuiteCRMUserSession.id + "'";
                        }
                        foreach (EntryValue _value in RestAPIWrapper.GetEntryList(str2, query, 0, "date_entered DESC", 0, false, new string[] { "first_name", "last_name", "email1" }).entry_list)
                        {
                            string str4       = string.Empty;
                            string str5       = string.Empty;
                            string valueByKey = string.Empty;
                            string str7       = string.Empty;
                            valueByKey = RestAPIWrapper.GetValueByKey(_value, "first_name");
                            str7       = RestAPIWrapper.GetValueByKey(_value, "last_name");
                            RestAPIWrapper.GetValueByKey(_value, "id");
                            str5 = RestAPIWrapper.GetValueByKey(_value, "email1");
                            str4 = valueByKey + " " + str7;
                            this.lstViewResults.Items.Add(new ListViewItem(new string[] { str4, str5, str2 }));
                        }
                    }
                }
            }
        }
Esempio n. 5
0
        private void btnSearch_Click(object sender, EventArgs e)
        {
            string[] tokens          = new string[2];
            string   logicalOperator = "OR";

            using (WaitCursor.For(this))
            {
                if (this.txtSearch.Text == string.Empty)
                {
                    MessageBox.Show("Please enter something to search for", "Error");
                }
                else
                {
                    this.lstViewResults.Items.Clear();
                    string[] moduleNames = new string[] { "Leads", ContactSynchroniser.CrmModule };
                    if (this.txtSearch.Text.Contains <char>(' '))
                    {
                        tokens = this.txtSearch.Text.Split(new char[] { ' ' });
                    }
                    else
                    {
                        tokens[0] = this.txtSearch.Text;
                        tokens[1] = this.txtSearch.Text;
                    }
                    if ((tokens[1] != string.Empty) && (tokens[0] != tokens[1]))
                    {
                        logicalOperator = "AND";
                    }
                    foreach (string moduleName in moduleNames)
                    {
                        string query      = "(" + moduleName.ToLower() + ".first_name LIKE '%" + tokens[0] + "%' " + logicalOperator + " " + moduleName.ToLower() + ".last_name LIKE '%" + tokens[1] + "%')";
                        bool   isContacts = moduleName == ContactSynchroniser.CrmModule;
                        if (this.cbMyItems.Checked)
                        {
                            string str8 = query;
                            query = str8 + "AND " + moduleName.ToLower() + ".assigned_user_id = '" + Globals.ThisAddIn.SuiteCRMUserSession.id + "'";
                        }
                        foreach (EntryValue value in RestAPIWrapper.GetEntryList(moduleName, query, 0, "date_entered DESC", 0, false, new string[] { "first_name", "last_name", "email1" }).entry_list)
                        {
                            string primaryEmail = RestAPIWrapper.GetValueByKey(value, "email1");
                            string firstName    = RestAPIWrapper.GetValueByKey(value, "first_name");
                            string lastName     = RestAPIWrapper.GetValueByKey(value, "last_name");
                            string fullName     = firstName + " " + lastName;
                            this.lstViewResults.Items.Add(new ListViewItem(new string[] { fullName, primaryEmail, moduleName }));
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Get the id of the record with the specified `smtpAddress` in the module with the specified `moduleName`.
        /// </summary>
        /// <param name="smtpAddress">The SMTP email address to be sought.</param>
        /// <param name="moduleName">The name of the module in which to seek it.</param>
        /// <returns>The corresponding id, if present, else the empty string.</returns>
        public string GetID(string smtpAddress, string moduleName)
        {
            StringBuilder bob = new StringBuilder($"({moduleName.ToLower()}.id in ")
                                .Append($"(select eabr.bean_id from email_addr_bean_rel eabr ")
                                .Append($"INNER JOIN email_addresses ea on eabr.email_address_id = ea.id ")
                                .Append($"where eabr.bean_module = '{moduleName}' ")
                                .Append($"and ea.email_address LIKE '%{SuiteCRMAddIn.clsGlobals.MySqlEscape(smtpAddress)}%'))");

            string query = bob.ToString();

            Log.Debug($"AppointmentSyncing.GetID: query = `{query}`");

            string[]  fields  = { "id" };
            EntryList _result = RestAPIWrapper.GetEntryList(moduleName, query, Properties.Settings.Default.SyncMaxRecords, "date_entered DESC", 0, false, fields);

            return(_result.result_count > 0 ?
                   RestAPIWrapper.GetValueByKey(_result.entry_list[0], "id") :
                   string.Empty);
        }
        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";
            EntryList _result = RestAPIWrapper.GetEntryList(sModule, str5, Properties.Settings.Default.SyncMaxRecords, "date_entered DESC", 0, false, fields);

            if (_result.result_count > 0)
            {
                return(RestAPIWrapper.GetValueByKey(_result.entry_list[0], "id"));
            }
            return(String.Empty);
        }
Esempio n. 8
0
        public static IEnumerable <EntryValue> Search(string module, string token, IEnumerable <string> fields,
                                                      string logicalOperator = "OR")
        {
            var bob         = new StringBuilder("(");
            var fieldsArray = fields.ToArray();

            foreach (var field in fieldsArray)
            {
                switch (field)
                {
                case "first_name":
                case "last_name":
                case "name":
                    if (field != fieldsArray.First())
                    {
                        bob.Append($"{logicalOperator} ");
                    }
                    bob.Append($"{module.ToLower()}.{field} ").Append(token.Length < 4
                            ? $"= '{token}' "
                            : $"LIKE '%{token}%' ");
                    break;

                case "email1":
                    if (field != fieldsArray.First())
                    {
                        bob.Append($"{logicalOperator} ");
                    }
                    bob.Append(
                        $"({module.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 = '{module}' and ea.email_address ");
                    bob.Append(token.Length < 4 ? $"= '{token}'))" : $"LIKE '%{token}%'))");
                    break;
                }
            }
            bob.Append(")");

            var result = RestAPIWrapper.GetEntryList(module, bob.ToString(), 1000, "date_entered DESC", 0, false, fieldsArray)
                         .entry_list;

            return(result);
        }