Skip to main content
Solved

Linking a User account to an employee record by programming (API or C#)

  • Oct 5, 2022 8:37 PM UTC
  • 3 replies
  • 609 views
  • Translate All

szur
Freshman I

Is there a way to link an user account to an employee record?

I understand that I would need to set the UserID field on the employee's Contact record to the user's PKID value. 

From Slashdot this was one of the answers:


“In addition there is also Employee.UserID that you would want to set equal to the Users.PKID value. 
The virtual field is populated using that Contact.UserID value but in the RowUpdating handler Acumatica is updating both the Contact and Employee records when Users.ContactID is changed.”

 

Is there any more clarification on this? 

Best answer by Brian Stevens

Yes.  In C#, just set Employee.UserID = to the Users.PKID, just as you stated.

In the following code, request an object for a custom DAC containing the AcctCD value on the EPEmployee record for which I want to link the User account.  Username contains the existing user (by login name) that I want to link to the employee.  This is significantly reduced to relevant code for your question, but I actually took this from a set of code where I create a user, the employee, the contact records for the employee, and link it all together.  However, this little bit should make the link you requested if your User and Employee exist.  Just replace the “request” references to your own variables/values.

EmployeeMaint graph = PXGraph.CreateInstance<EmployeeMaint>();
EPEmployee employee = graph.Employee.Search<EPEmployee.acctCD>(request.EPAcctCD);

AccessUsers graphUser = PXGraph.CreateInstance<AccessUsers>();
Users user = graphUser.UserList.Search<Users.username>(request.Username);

employee.UserID = user?.PKID;
graph.CurrentEmployee.Update(employee);

graph.Save.Press();

 

 

View original

3 replies

Forum|alt.badge.img+5

Yes.  In C#, just set Employee.UserID = to the Users.PKID, just as you stated.

In the following code, request an object for a custom DAC containing the AcctCD value on the EPEmployee record for which I want to link the User account.  Username contains the existing user (by login name) that I want to link to the employee.  This is significantly reduced to relevant code for your question, but I actually took this from a set of code where I create a user, the employee, the contact records for the employee, and link it all together.  However, this little bit should make the link you requested if your User and Employee exist.  Just replace the “request” references to your own variables/values.

EmployeeMaint graph = PXGraph.CreateInstance<EmployeeMaint>();
EPEmployee employee = graph.Employee.Search<EPEmployee.acctCD>(request.EPAcctCD);

AccessUsers graphUser = PXGraph.CreateInstance<AccessUsers>();
Users user = graphUser.UserList.Search<Users.username>(request.Username);

employee.UserID = user?.PKID;
graph.CurrentEmployee.Update(employee);

graph.Save.Press();

 

 

Translate

Forum|alt.badge.img
  • Varsity I
  • Jul 5, 2024 4:48 PM UTC

I was able to link Employee to user by calling API Employee

{

  "EmployeeID": {"value": "EP001"},

  "custom": {

        "CurrentEmployee": {

            "UserID":  {"value": "USER01"}

        }    

  }

}

 

The user shows in Employees screen, but the Users screen Linked Entity field is still blank, doesn’t link back to Employee.

Translate

Forum|alt.badge.img
  • Freshman I
  • Nov 12, 2026 9:52 PM UTC

​@junmao01 I tried something like this and had the same result where the users screen doesn’t link back to the employee. However, the link does work in both directions if the user is updated with the contact ID. It took some to figure this out, so for you or others that may be trying to accomplish this through the REST API, here’s an approach that seems to work better:

Obtain the employee’s Contact ID through the API by using OData; for example:

apiClient.GetOData(ODataVersion.DACBasedOData, "EPEmployee", tenant: tenantId, filter: filter);

That result can be converted to a more convenient form by using the ToObject() method from Newtonsoft.Json.

The DefContactID value is then used when creating or updating the user account through the API by using a web service endpoint extension for the User entity with a field named ContactID (for example) where the "Mapped Field" selection is set to "Linked Entity" (data field ContactID).

Translate