コード例 #1
0
        /// <summary>
        /// Query salesforce object async
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="conditions"></param>
        /// <param name="client"></param>
        /// <param name="outputFields"></param>
        /// <returns></returns>
        private async Task <ProxyQueryResult <T> > QueryObjectAsync <T>(ICaseClientProxy client, IDictionary <string, object> conditions, params string[] outputFields)
        {
            if (client == null)
            {
                client = await CaseService.GetUserNamePasswordForceClientAsync(ClientEnum.Default);
            }
            //build outputFields
            var fields = string.Join(", ", outputFields.Select(s => s));
            //build where clause
            var whereClause = string.Empty;

            foreach (var conditionValue in conditions)
            {
                if (!string.IsNullOrWhiteSpace(whereClause))
                {
                    whereClause += " and ";
                }
                if (conditionValue.Value.IsNumber() || conditionValue.Value.IsNumber())
                {
                    whereClause += string.Join("=", conditionValue.Key, conditionValue.Value);
                }
                if (conditionValue.Value.IsBoolean() || conditionValue.Value.IsBoolean())
                {
                    whereClause += string.Join("=", conditionValue.Key, conditionValue.Value);
                }
                else
                {
                    whereClause += string.Join("=", conditionValue.Key, $"'{conditionValue.Value}'");
                }
            }
            var soql     = $"select {fields} from {typeof(T).Name} where {whereClause}";
            var response = await client.QueryAsync <T>(soql);

            return(response);
        }
コード例 #2
0
        private async Task <CaseResponse> HandleCaseResponse(ICaseClientProxy client, ProxySuccessResponse response, bool isError, MetricWatcher metric)
        {
            var caseResponse = await CreateResponse(response, client);

            if (!isError)
            {
                metric.Options.LogMessage = metric.Options.LogMessage.Merge(new Dictionary <string, object>
                {
                    { "Id", caseResponse.Id },
                    { "CaseNumber", string.IsNullOrWhiteSpace(caseResponse.CaseNumber) ? null : caseResponse.CaseNumber }
                });
            }
            return(caseResponse);
        }
コード例 #3
0
        //protected virtual async Task<string> GetCustomerId(string accountKey)
        //{
        //    ProxySuccessResponse response = null;
        //    var isError = false;
        //    var metric = new MetricWatcher(SalesforceEventTypeEnum.CreateCustomer.ToString(),
        //        new MetricWatcherOption
        //        {
        //            ManualStartStop = true,
        //            LoggingOption = LogOptionEnum.FullLog,
        //            LogMessage = new Dictionary<string, object>
        //            {
        //                {"AccountKey", accountKey},
        //                {"EventType", SalesforceEventTypeEnum.CreateCustomer}
        //            }
        //        });
        //    try
        //    {
        //        metric.Start();

        //        if (string.IsNullOrWhiteSpace(accountKey))
        //        {
        //            return "";
        //        }
        //        var customer =
        //            await QueryObjectAsync<Customer__c>(new Dictionary<string, object> {{ "AccountKey__c", accountKey}}, "Id");

        //        if (customer != null && customer.Records.Count > 0)
        //        {
        //            return customer.Records[0].Id;
        //        }
        //        var customerNew = new Customer__c()
        //        {
        //            Name = accountKey,
        //            AccountKey__c = accountKey
        //        };
        //        var client = await CaseService.GetUserNamePasswordForceClientAsync(ClientEnum.Default);
        //        response = await client.CreateAsync("Customer__c", customerNew);
        //        if (response != null && response.Success)
        //        {
        //            return response.Id;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        isError = true;
        //        if (ex is ForceException)
        //        {
        //            throw new ExternalErrorException(ex.GetExceptionMessages(),
        //                new LogObject(SalesforceEventTypeEnum.CreateCustomer.ToString(),
        //                    new Dictionary<string, object>
        //                    {
        //                        {"AccountKey", accountKey},
        //                        {"EventType", SalesforceEventTypeEnum.CreateCustomer}
        //                    }));
        //        }
        //    }
        //    finally
        //    {
        //        if (response != null
        //            && !isError)
        //        {
        //            metric.Options.LogMessage = metric.Options.LogMessage.Merge(new Dictionary<string, object>
        //            {
        //                {"Id", response.Id},
        //                {"EventType", SalesforceEventTypeEnum.CreateCustomer},
        //                {"Error", response.Errors}
        //            });
        //        }
        //        metric.Stop(isError);
        //    }
        //    return "";
        //}

        #endregion

        #region Get / Create Customer Object

        public async Task <Account> GetPersonAccount(PersonAccountInput input, ICaseClientProxy client = null)
        {
            ProxySuccessResponse response = null;
            var isError = false;
            var logData = new Dictionary <string, object>
            {
                { "AccountIdentifier", input.AccountIdentifier },
                { "EventType", SalesforceEventTypeEnum.CreatePersonAccount }
            };
            var metric = new MetricWatcher(SalesforceEventTypeEnum.CreatePersonAccount.ToString(),
                                           new MetricWatcherOption
            {
                ManualStartStop = true,
                LoggingOption   = LogOptionEnum.FullLog,
                LogMessage      = logData
            });

            try
            {
                metric.Start();
                if (client == null)
                {
                    client = await CaseService.GetUserNamePasswordForceClientAsync(ClientEnum.Default);
                }
                if (string.IsNullOrWhiteSpace(input.AccountIdentifier))
                {
                    return(null);
                }
                var account = await QueryObjectAsync <Account>(
                    new Dictionary <string, object> {
                    { "Account_Identifier__c", input.AccountIdentifier }
                }, "Id",
                    "PersonContactId");

                if (account != null && account.Records.Count > 0)
                {
                    return(account.Records[0]);
                }
                if (string.IsNullOrEmpty(input.LastName))
                {
                    return(null);
                }
                var waveRecordTypeId = await GetRecordTypeId("WaveAccount");

                var personAccount = new Account
                {
                    LastName              = input.LastName,
                    FirstName             = input.FirstName,
                    RecordTypeId          = waveRecordTypeId,
                    Account_Identifier__c = input.AccountIdentifier,
                    Product__c            = input.Product,
                    Brand__c              = input.Brand
                };
                response = await client.CreateAsync("Account", personAccount);

                if (response != null && response.Success)
                {
                    var newAccount = await QueryObjectAsync <Account>(new Dictionary <string, object> {
                        { "ID", response.Id }
                    },
                                                                      "PersonContactId");

                    var contactId = string.Empty;
                    if (newAccount != null && newAccount.Records.Count > 0)
                    {
                        contactId = newAccount.Records[0].PersonContactId;
                    }
                    return(new Account {
                        Id = response.Id, PersonContactId = contactId
                    });
                }
            }
            catch (Exception ex)
            {
                isError = true;
                var log = new LogObject(SalesforceEventTypeEnum.CreatePersonAccount.ToString(), logData);
                if (ex is ForceException)
                {
                    throw new ExternalErrorException(ex.GetExceptionMessages(), log, ex);
                }
                throw new GdErrorException("Unable to get Person Account data", log, ErrorCodeEnum.ExternalError, ex);
            }
            finally
            {
                if (response != null && isError)
                {
                    metric.Options.LogMessage = metric.Options.LogMessage.Merge(new Dictionary <string, object>
                    {
                        { "Id", response.Id },
                        { "EventType", SalesforceEventTypeEnum.CreatePersonAccount },
                        { "Error", response.Errors }
                    });
                }
                metric.Stop(isError);
            }
            return(null);
        }
コード例 #4
0
        private async Task <CaseResponse> UpdateAsync(SalesforceEventTypeEnum eventType, string sObject, string caseId, object record, ICaseClientProxy client = null, IDictionary <string, object> logData = null)
        {
            ProxySuccessResponse response     = null;
            CaseResponse         caseResponse = null;
            var isError = false;
            var metric  = new MetricWatcher(eventType.ToString(),
                                            new MetricWatcherOption
            {
                ManualStartStop = true,
                LoggingOption   = LogOptionEnum.FullLog,
                LogMessage      = logData
            });

            try
            {
                metric.Start();
                if (client == null)
                {
                    client = await CaseService.GetUserNamePasswordForceClientAsync(ClientEnum.Header);
                }
                response = await client.UpdateAsync(sObject, caseId, record);
            }
            catch (Exception ex)
            {
                isError = true;
                if (ex is ForceException)
                {
                    throw new ExternalErrorException(ex.GetExceptionMessages(), new LogObject(eventType.ToString(), logData), ex);
                }
                throw;
            }
            finally
            {
                if (response != null)
                {
                    response.Id  = caseId;
                    caseResponse = await HandleCaseResponse(client, response, isError, metric);
                }
                metric.Stop(isError);
            }
            return(caseResponse);
        }
コード例 #5
0
        private CaseResponse CreateResponse(ProxySuccessResponse response, string caseNumber, ICaseClientProxy client)
        {
            var caseResponse = new CaseResponse
            {
                CaseNumber = string.IsNullOrWhiteSpace(caseNumber) ? null : caseNumber,
                Success    = response.Success,
                Errors     = response.Success ? null : response.Errors,
                Id         = response.Id
            };

            return(caseResponse);
        }
コード例 #6
0
        private async Task <CaseResponse> CreateResponse(ProxySuccessResponse response, ICaseClientProxy client)
        {
            var caseResponse = await QueryObjectAsync <Case>(new Dictionary <string, object> {
                { "Id", response.Id }
            }, "CaseNumber");

            var caseNumber = string.Empty;

            if (caseResponse?.Records.Count > 0)
            {
                caseNumber = caseResponse.Records[0].CaseNumber;
            }
            return(CreateResponse(response, caseNumber, client));
        }