acumatica Modifications to Contact and Address Info through Code Specify Contact and Address information for a new Employee


Example

To specify Contact and Address info for an Employee, you should always invoke Select() method on the Contact and Address data views prior to assigning any field values. It is also recommended to assign the result of Select() method to the Contact and Address data views' Current property to guarantee that your code modifies the current record in Contact and Address PXCache respectively.

EmployeeMaint employeeMaintGraph = PXGraph.CreateInstance<EmployeeMaint>();
EPEmployee epEmployeeRow = new EPEmployee();
epEmployeeRow.AcctCD = "EMPLOYEE1";
epEmployeeRow = employeeMaintGraph.Employee.Insert(epEmployeeRow);

Contact contactRow = employeeMaintGraph.Contact.Current = employeeMaintGraph.Contact.Select();
contactRow.FirstName = "John";
contactRow.LastName = "Green";
employeeMaintGraph.Contact.Update(contactRow);

Address addressRow = employeeMaintGraph.Address.Current = employeeMaintGraph.Address.Select();
addressRow.CountryID = "US";
addressRow = employeeMaintGraph.Address.Update(addressRow);
addressRow.State = "DC";
employeeMaintGraph.Address.Update(addressRow);

epEmployeeRow.VendorClassID = "EMPSTAND";
epEmployeeRow.DepartmentID = "FINANCE";
employeeMaintGraph.Employee.Update(epEmployeeRow);

employeeMaintGraph.Actions.PressSave();

When inserting a new Employee, employeeMaintGraph.Contact.Current will always return the main contact record as the contact record gets automatically inserted into the cache and therefore becomes available via the Current property of PXCache/Data View. The use of Select() method is a little more generic since it will work in all possible scenarios, whether you need to insert new Employee or update an existing one.