Ejemplo n.º 1
0
        public async Task <IEnumerable <BatchInfoResult> > SubmitTimeSheets(IEnumerable <entities.TimeSheet> timeSheets)
        {
            const int batchSize = 100;
            var       jobInfo   = await _forceClient.CreateJobAsync("BP_Weekly_Timesheet__c", BulkConstants.OperationType.Insert);

            var tsBatchList         = Batch_TimeSheets(timeSheets, batchSize);
            var batchInfoResultList = new List <BatchInfoResult>();

            foreach (var tsBatch in tsBatchList)
            {
                var batch = new SObjectList <SObject>();
                foreach (var ts in tsBatch)
                {
                    var rec = new SObject {
                        { "Name", $"Week of {ts.StartDate.ToString("yyyy-MM-dd")}" },
                        { "Submitted_By__c", $"{ts.UserId}" },
                        { "start_date__c", $"{ts.StartDate.ToString("yyyy-MM-dd")}" }
                    };
                    batch.Add(rec);
                }
                batchInfoResultList.Add(await _forceClient.CreateJobBatchAsync(jobInfo, batch));
            }

            var jonInfoResult = await _forceClient.CloseJobAsync(jobInfo); // Closing job prevents any more batches from being added.

            return(batchInfoResultList);
        }
Ejemplo n.º 2
0
        private async Task <IEnumerable <entities.UserTimeClock> > BatchAndSubmit_UserTimeClocks(Salesforce.Common.Models.Xml.JobInfoResult jobInfo, IEnumerable <UserTimeClock> userTimeClocks)
        {
            const int batchSize           = 100;
            var       utcBatchList        = Batch_UserTimeClocks(userTimeClocks, batchSize);
            var       batchInfoResultList = new List <BatchInfoResult>();
            var       startDate           = userTimeClocks.FirstOrDefault().StartDate;

            foreach (var utcBatch in utcBatchList)
            {
                // map bhive usertimeclock records to sales force time sheet activity records
                var tsaBatch = new SObjectList <SObject>();
                foreach (var utc in utcBatch)
                {
                    tsaBatch.Add(MapToSalesForceTimeSheetActivityRecord(utc));
                }

                // submit batch here
                var batchInfoResult = await _forceClient.CreateJobBatchAsync(jobInfo, tsaBatch);

                batchInfoResultList.Add(batchInfoResult);

                // Set SalesForce job id and batch id in user time clock record
                foreach (var utc in utcBatch)
                {
                    utc.JobId   = jobInfo.Id;
                    utc.BatchId = batchInfoResult.Id;
                }

                SaveSubmittedBatchToBlobStorage(startDate, batchInfoResult.Id, tsaBatch);
            }

            // Closing job prevents any more batches from being added and
            // allows faster reading of submission results.
            await _forceClient.CloseJobAsync(jobInfo);

            var batchResultsList = await Get_BatchSubmissionResults(batchInfoResultList);

            userTimeClocks = setTimeSheetActivityIdsFromSalesForce(batchResultsList, userTimeClocks);

            return(userTimeClocks);
        }
        public async Task UpsertAccountTests()
        {
            var dtContactsBatch1 = new SObjectList <SObject>
            {
                new SObject {
                    { "Name", "Upsert 1" }
                },
                new SObject {
                    { "Name", "Upsert 2" }
                }
            };

            var resultsUpsert1 = await _client.RunJobAndPollAsync("Campaign", "Name",
                                                                  BulkConstants.OperationType.Upsert, new List <SObjectList <SObject> > {
                dtContactsBatch1
            });

            Assert.IsTrue(resultsUpsert1 != null);
            Assert.AreEqual(resultsUpsert1.Count, 1);
            Assert.AreEqual(resultsUpsert1[0].Items.Count, 2);
            // TODO: This fails on successive runs against an org; update so that we delete these after running the test
            // Assert.IsTrue(resultsUpsert1[0].Items[0].Created);
            Assert.IsTrue(resultsUpsert1[0].Items[0].Success);
            // Assert.IsTrue(resultsUpsert1[0].Items[1].Created);
            Assert.IsTrue(resultsUpsert1[0].Items[1].Success);

            var dtContactsBatch2 = new SObjectList <SObject>
            {
                new SObject {
                    { "Name", "Upsert 2" }, { "Description", "Updated via Upsert" }
                },
                new SObject {
                    { "Name", "Upsert 3" }
                }
            };

            var resultsUpsert2 = await _client.RunJobAndPollAsync("Campaign", "Name",
                                                                  BulkConstants.OperationType.Upsert, new List <SObjectList <SObject> > {
                dtContactsBatch2
            });

            Assert.IsTrue(resultsUpsert2 != null);
            Assert.AreEqual(resultsUpsert2.Count, 1);
            Assert.AreEqual(resultsUpsert2[0].Items.Count, 2);
            // Assert.IsFalse(resultsUpsert2[0].Items[0].Created);
            Assert.IsTrue(resultsUpsert2[0].Items[0].Success);
            // Assert.IsTrue(resultsUpsert2[0].Items[1].Created);
            Assert.IsTrue(resultsUpsert2[0].Items[1].Success);

            // create an Id list for the original strongly typed accounts created
            var idBatch = new SObjectList <SObject>();

            idBatch.AddRange(resultsUpsert1[0].Items.Select(result => new SObject {
                { "Id", result.Id }
            }));
            idBatch.Add(new SObject {
                { "Id", resultsUpsert2[0].Items[1].Id }
            });

            var resultsDelete = await _client.RunJobAndPollAsync("Account", BulkConstants.OperationType.Delete,
                                                                 new List <SObjectList <SObject> > {
                idBatch
            });

            Assert.IsTrue(resultsDelete != null, "[results4] empty result object");
            Assert.AreEqual(resultsDelete.Count, 1, "[results4] wrong number of results");
            Assert.AreEqual(resultsDelete[0].Items.Count, 3, "[results4] wrong number of result records");
        }
        public async Task UpsertTests()
        {
            // Requires a new field on Contact "Unique_Email__c" with External Id set.
            if (_testUpsert)
            {
                var dtContactsBatch1 = new SObjectList <SObject>
                {
                    new SObject {
                        { "FirstName", "TestDtContact1" }, { "LastName", "TestDtContact1" }, { "MailingCity", "London" }, { "Email", "*****@*****.**" }, { "Unique_Email__c", "*****@*****.**" }
                    },
                    new SObject {
                        { "FirstName", "TestDtContact2" }, { "LastName", "TestDtContact2" }, { "MailingCity", "London" }, { "Email", "*****@*****.**" }, { "Unique_Email__c", "*****@*****.**" }
                    }
                };

                var resultsUpsert1 = await _client.RunJobAndPollAsync("Contact", "Unique_Email__c",
                                                                      BulkConstants.OperationType.Upsert, new List <SObjectList <SObject> > {
                    dtContactsBatch1
                });

                Assert.IsTrue(resultsUpsert1 != null);
                Assert.AreEqual(resultsUpsert1.Count, 1);
                Assert.AreEqual(resultsUpsert1[0].Items.Count, 2);
                Assert.IsTrue(resultsUpsert1[0].Items[0].Created);
                Assert.IsTrue(resultsUpsert1[0].Items[0].Success);
                Assert.IsTrue(resultsUpsert1[0].Items[1].Created);
                Assert.IsTrue(resultsUpsert1[0].Items[1].Success);

                var dtContactsBatch2 = new SObjectList <SObject>
                {
                    new SObject {
                        { "FirstName", "TestDtContact2" }, { "LastName", "TestDtContact2" }, { "MailingCity", "York" }, { "Email", "*****@*****.**" }, { "Unique_Email__c", "*****@*****.**" }
                    },
                    new SObject {
                        { "FirstName", "TestDtContact3" }, { "LastName", "TestDtContact3" }, { "MailingCity", "York" }, { "Email", "*****@*****.**" }, { "Unique_Email__c", "*****@*****.**" }
                    }
                };

                var resultsUpsert2 = await _client.RunJobAndPollAsync("Contact", "Unique_Email__c",
                                                                      BulkConstants.OperationType.Upsert, new List <SObjectList <SObject> > {
                    dtContactsBatch2
                });

                Assert.IsTrue(resultsUpsert2 != null);
                Assert.AreEqual(resultsUpsert2.Count, 1);
                Assert.AreEqual(resultsUpsert2[0].Items.Count, 2);
                Assert.IsFalse(resultsUpsert2[0].Items[0].Created);
                Assert.IsTrue(resultsUpsert2[0].Items[0].Success);
                Assert.IsTrue(resultsUpsert2[0].Items[1].Created);
                Assert.IsTrue(resultsUpsert2[0].Items[1].Success);

                // create an Id list for the original strongly typed accounts created
                var idBatch = new SObjectList <SObject>();
                idBatch.AddRange(resultsUpsert1[0].Items.Select(result => new SObject {
                    { "Id", result.Id }
                }));
                idBatch.Add(new SObject {
                    { "Id", resultsUpsert2[0].Items[1].Id }
                });

                var resultsDelete = await _client.RunJobAndPollAsync("Contact", BulkConstants.OperationType.Delete,
                                                                     new List <SObjectList <SObject> > {
                    idBatch
                });

                Assert.IsTrue(resultsDelete != null, "[results4] empty result object");
                Assert.AreEqual(resultsDelete.Count, 1, "[results4] wrong number of results");
                Assert.AreEqual(resultsDelete[0].Items.Count, 3, "[results4] wrong number of result records");
            }
            else
            {
                Assert.Inconclusive("Upsert Tests Skipped.");
            }
        }