/// <summary> /// Synchronizes a set of projects using a connected IntacctClient /// </summary> /// <param name="client">The Client to sync to</param> /// <param name="orgprojects">Project Data to Send</param> private async Task SyncOrgProjects(OnlineClient client, IEnumerable <IntacctProject> orgprojects, PerformContext context) { IDictionary <string, string> projectmap = await GetProjectIds(client, context); // Filter Existing Out if (SyncOnlyNew) { context.WriteLine("Filtering out Existing Projects"); orgprojects = orgprojects.Where(c => !projectmap.ContainsKey(c.PROJECTID)).ToArray(); } // Send in batches of Projects int sent = 0; int total = orgprojects.Count(); while (sent < total) { // What's in this batch var batchData = orgprojects.Skip(sent).Take(100).ToList(); context.WriteLine("Preparing Batch of 100 ({0} - {1} of {2})", sent, sent + batchData.Count, total); sent += batchData.Count; // Create the Batch for Intacct List <IFunction> batchFunctions = new List <IFunction>(); foreach (var project in batchData) { if (!projectmap.ContainsKey(project.PROJECTID)) { // Create the Project ProjectCreate create = new ProjectCreate { CustomerId = project.CUSTOMERID, ProjectId = project.PROJECTID, ProjectName = project.PROJECTNAME, ProjectCategory = project.PROJECTCATEGORY, ProjectStatus = project.PROJECTSTATUS, Active = project.PROJECTACTIVE == "active", DepartmentId = project.DEPARTMENTID, LocationId = project.LOCATIONID, ProjectManagerEmployeeId = project.PROJECTMANAGERID }; if (!String.IsNullOrWhiteSpace(project.PROJECTPARENTID)) { create.ParentProjectId = project.PROJECTPARENTID; } if (!String.IsNullOrWhiteSpace(project.PE_JOB_CODE)) { create.CustomFields.Add("PE_JOB_CODE", project.PE_JOB_CODE); } if (!String.IsNullOrWhiteSpace(project.USER_RESTRICTIONS)) { create.CustomFields.Add("USER_RESTRICTIONS", project.USER_RESTRICTIONS); } batchFunctions.Add(create); } else { // Update the Project ProjectUpdate update = new ProjectUpdate { CustomerId = project.CUSTOMERID, ProjectId = project.PROJECTID, ProjectName = project.PROJECTNAME, ProjectCategory = project.PROJECTCATEGORY, ProjectStatus = project.PROJECTSTATUS, Active = project.PROJECTACTIVE == "active", DepartmentId = project.DEPARTMENTID, LocationId = project.LOCATIONID, ProjectManagerEmployeeId = project.PROJECTMANAGERID }; if (!String.IsNullOrWhiteSpace(project.PROJECTPARENTID)) { update.ParentProjectId = project.PROJECTPARENTID; } if (!String.IsNullOrWhiteSpace(project.PE_JOB_CODE)) { update.CustomFields.Add("PE_JOB_CODE", project.PE_JOB_CODE); } if (!String.IsNullOrWhiteSpace(project.USER_RESTRICTIONS)) { update.CustomFields.Add("USER_RESTRICTIONS", project.USER_RESTRICTIONS); } batchFunctions.Add(update); } } // Send the Batch to Intacct context.WriteLine("Sending Batch to Intacct"); var response = await client.ExecuteBatch(batchFunctions); context.WriteLine("Inspecting Response from Intacct"); foreach (var result in response.Results) { if (result.Errors != null) { context.SetTextColor(ConsoleTextColor.Red); context.WriteLine("=================================="); foreach (var err in result.Errors) { context.WriteLine(err); } context.WriteLine("=================================="); context.WriteLine(); Console.ResetColor(); } } } }
/// <summary> /// Synchronizes a set of customers using a connected IntacctClient /// </summary> /// <param name="client">The Client to sync to</param> /// <param name="orgcustomers">Customer Data to Send</param> private async Task SyncOrgCustomers(OnlineClient client, IEnumerable <IntacctCustomer> orgcustomers, PerformContext context) { IList <string> types = await GetCustTypes(client, context); IDictionary <string, string> customermap = await GetCustomerIds(client, context); // Filter Existing Out if (SyncOnlyNew) { context.WriteLine("Filtering out Existing Customers"); orgcustomers = orgcustomers.Where(c => !customermap.ContainsKey(c.CUSTOMERID)).ToArray(); } // Send in batches of Customers int sent = 0; int total = orgcustomers.Count(); while (sent < total) { // What's in this batch var batchData = orgcustomers.Skip(sent).Take(100).ToList(); context.WriteLine("Preparing Batch of 100 ({0} - {1} of {2})", sent, sent + batchData.Count, total); sent += batchData.Count; // Create the Batch for Intacct List <IFunction> batchFunctions = new List <IFunction>(); foreach (var customer in batchData) { var hasValidCustType = types.Contains(customer.CUSTTYPENAME); if (customermap.ContainsKey(customer.CUSTOMERID)) { // Update the Customer CustomerUpdate update = new CustomerUpdate { CustomerId = customer.CUSTOMERID, CustomerName = IntacctCleanString(customer.CUSTNAME), PrintAs = customer.CONTACTNAME, FirstName = customer.FIRSTNAME, LastName = customer.LASTNAME, PrimaryEmailAddress = customer.EMAIL1, Active = customer.STATUS == "active", AddressLine1 = customer.ADDRESS1, City = customer.CITY, StateProvince = customer.STATE, Country = "United States", ZipPostalCode = customer.ZIP, ParentCustomerId = customer.PARENTID }; if (!String.IsNullOrWhiteSpace(customer.FIRSTNAME)) { update.FirstName = customer.FIRSTNAME; } if (!String.IsNullOrWhiteSpace(customer.LASTNAME)) { update.LastName = customer.LASTNAME; } update.CustomFields.Add("RECORDNO", customermap[customer.CUSTOMERID]); if (hasValidCustType) { update.CustomerTypeId = customer.CUSTTYPENAME; } batchFunctions.Add(update); } else { // Create the Customer CustomerCreate create = new CustomerCreate { CustomerId = customer.CUSTOMERID, CustomerName = IntacctCleanString(customer.CUSTNAME), PrintAs = customer.CONTACTNAME, FirstName = customer.FIRSTNAME, LastName = customer.LASTNAME, PrimaryEmailAddress = customer.EMAIL1, Active = customer.STATUS == "active", AddressLine1 = customer.ADDRESS1, City = customer.CITY, StateProvince = customer.STATE, Country = "United States", ZipPostalCode = customer.ZIP, ParentCustomerId = customer.PARENTID }; if (hasValidCustType) { create.CustomerTypeId = customer.CUSTTYPENAME; } batchFunctions.Add(create); } } // Send the Batch to Intacct context.WriteLine("Sending Batch to Intacct"); var response = await client.ExecuteBatch(batchFunctions); context.WriteLine("Inspecting Response from Intacct"); foreach (var result in response.Results) { if (result.Errors != null) { context.SetTextColor(ConsoleTextColor.Red); context.WriteLine("=================================="); foreach (var err in result.Errors) { context.WriteLine(err); } context.WriteLine("=================================="); context.WriteLine(); Console.ResetColor(); } } } }
/// <summary> /// Synchronizes a set of employees using a connected IntacctClient /// </summary> /// <param name="client">The Client to sync to</param> /// <param name="orgemployees">Customer Data to Send</param> private async Task SyncOrgEmployees(OnlineClient client, IEnumerable <IntacctEmployee> orgemployees, PerformContext context) { IDictionary <string, string> employeemap = await GetEmployeeIds(client, context); IList <string> contactNames = await GetContacts(client, context); // Filter Existing Out if (SyncOnlyNew) { context.WriteLine("Filtering out Existing Employees"); orgemployees = orgemployees.Where(c => !contactNames.Contains(IntacctCleanString(c.EMPLOYEENAME))).ToArray(); } // Send in batches of Employees int sent = 0; int total = orgemployees.Count(); while (sent < total) { // What's in this batch var batchData = orgemployees.Skip(sent).Take(50).ToList(); context.WriteLine("Preparing Batch of 50 ({0} - {1} of {2})", sent, sent + batchData.Count, total); sent += batchData.Count; // Create the Batch for Intacct List <IFunction> batchFunctions = new List <IFunction>(); foreach (var employee in batchData) { // Process the Contact First if (contactNames.Contains(IntacctCleanString(employee.EMPLOYEENAME))) { // Update the Contact ContactUpdate update = new ContactUpdate { PrintAs = employee.EMPLOYEENAME, ContactName = IntacctCleanString(employee.EMPLOYEENAME), FirstName = employee.FIRSTNAME, LastName = employee.LASTNAME, Active = employee.EMPLOYEEACTIVE == "active", PrimaryPhoneNo = employee.PHONE, PrimaryEmailAddress = employee.EMAIL }; batchFunctions.Add(update); } else { // Create the Contact ContactCreate create = new ContactCreate { PrintAs = employee.EMPLOYEENAME, ContactName = IntacctCleanString(employee.EMPLOYEENAME), FirstName = employee.FIRSTNAME, LastName = employee.LASTNAME, Active = employee.EMPLOYEEACTIVE == "active", PrimaryPhoneNo = employee.PHONE, PrimaryEmailAddress = employee.EMAIL }; batchFunctions.Add(create); // Add to our List, so we don't update duplicates contactNames.Add(employee.EMPLOYEENAME); } // Process the Employee Now if (employeemap.ContainsKey(employee.EMPLOYEEID)) { // Update the Employee EmployeeUpdate update = new EmployeeUpdate { EmployeeId = employee.EMPLOYEEID, ContactName = IntacctCleanString(employee.EMPLOYEENAME), DepartmentId = employee.DEPARTMENTID, LocationId = employee.LOCATIONID, Active = employee.EMPLOYEEACTIVE == "active", StartDate = employee.EMPLOYEESTART, EndDate = employee.EMPLOYEETERMINATION.ToIntacctDate() }; if (!String.IsNullOrWhiteSpace(employee.PE_STAFF_CODE)) { update.CustomFields.Add("PE_STAFF_CODE", employee.PE_STAFF_CODE); } update.CustomFields.Add("RECORDNO", employeemap[employee.EMPLOYEEID]); batchFunctions.Add(update); } else { // Create the Employee EmployeeCreate create = new EmployeeCreate { EmployeeId = employee.EMPLOYEEID, ContactName = IntacctCleanString(employee.EMPLOYEENAME), DepartmentId = employee.DEPARTMENTID, LocationId = employee.LOCATIONID, Active = employee.EMPLOYEEACTIVE == "active", StartDate = employee.EMPLOYEESTART, EndDate = employee.EMPLOYEETERMINATION.ToIntacctDate() }; if (!String.IsNullOrWhiteSpace(employee.PE_STAFF_CODE)) { create.CustomFields.Add("PE_STAFF_CODE", employee.PE_STAFF_CODE); } batchFunctions.Add(create); } } // Send the Batch to Intacct context.WriteLine("Sending Batch to Intacct"); var response = await client.ExecuteBatch(batchFunctions); context.WriteLine("Inspecting Response from Intacct"); foreach (var result in response.Results) { if (result.Errors != null) { context.SetTextColor(ConsoleTextColor.Red); context.WriteLine("=================================="); foreach (var err in result.Errors) { context.WriteLine(err); } context.WriteLine("=================================="); context.WriteLine(); Console.ResetColor(); } } } }
public async Task ExecuteBatchTransactionResultExceptionTest() { string xml = @"<?xml version=""1.0"" encoding=""utf-8""?> <response> <control> <status>success</status> <senderid>testsenderid</senderid> <controlid>requestUnitTest</controlid> <uniqueid>false</uniqueid> <dtdversion>3.0</dtdversion> </control> <operation> <authentication> <status>success</status> <userid>testuser</userid> <companyid>testcompany</companyid> <locationid></locationid> <sessiontimestamp>2015-12-06T15:57:08-08:00</sessiontimestamp> </authentication> <result> <status>aborted</status> <function>getAPISession</function> <controlid>func1UnitTest</controlid> <errormessage> <error> <errorno>XL03000009</errorno> <description></description> <description2>The entire transaction in this operation has been rolled back due to an error.</description2> <correction></correction> </error> </errormessage> </result> <result> <status>failure</status> <function>getAPISession</function> <controlid>func2UnitTest</controlid> <errormessage> <error> <errorno>Get API Session Failed</errorno> <description></description> <description2>Something went wrong</description2> <correction></correction> </error> <error> <errorno>XL03000009</errorno> <description></description> <description2>The entire transaction in this operation has been rolled back due to an error.</description2> <correction></correction> </error> </errormessage> </result> </operation> </response>"; HttpResponseMessage mockResponse1 = new HttpResponseMessage() { StatusCode = System.Net.HttpStatusCode.OK, Content = new StringContent(xml) }; List <HttpResponseMessage> mockResponses = new List <HttpResponseMessage> { mockResponse1, }; MockHandler mockHandler = new MockHandler(mockResponses); ClientConfig clientConfig = new ClientConfig { SenderId = "testsender", SenderPassword = "******", SessionId = "testsession..", MockHandler = mockHandler, }; RequestConfig requestConfig = new RequestConfig() { Transaction = true, }; List <IFunction> content = new List <IFunction> { new ApiSessionCreate("func1UnitTest"), new ApiSessionCreate("func2UnitTest") }; OnlineClient client = new OnlineClient(clientConfig); var ex = await Record.ExceptionAsync(() => client.ExecuteBatch(content, requestConfig)); Assert.IsType <ResultException>(ex); Assert.Equal("Result status: failure for Control ID: func2UnitTest - Get API Session Failed Something went wrong - XL03000009 The entire transaction in this operation has been rolled back due to an error.", ex.Message); }