acumaticaacumatica-kb

Bulk Edit UserRoles for Acumatica Users


I need to bulk update about 2000+ users in Acumatica (ie: Guest access users). I need to assign a new role to all of these user to support a new custom integration.

I am looking for a solution using either the Rest API or some sort of Excel bulk Import/Update that assigns the roles.

I have not had much luck working with UserRoles via the Rest API. I have only been able to download the users > user roles via a generic inquiry and ODATA. So far I have not found any documentation around this. Any guidance would be appreciated!


Solution

  • REST Api is not really the best tool for this job. It doesn't contain an endpoint for Users (needs to be extended) and is a bit hard to use for complex logic. There isn't an Excel import mechanism either (outside import scenarios).

    I would suggest to create a Customization Plugin for this task instead. They can be created in Acumatica customization project editor with a new CODE file: enter image description here

    A customization plugin is a script that is executed when you publish the customization. I tested the following code which assign Portal User role to all guest users as an example.

    using System;
    using PX.Data;
    using Customization;
    using PX.SM;
    using PX.EP;
    using System.Linq;
    
    namespace UpdateUserRoles
    {
      public class UpdateUserRoles : CustomizationPlugin
      {
        public override void UpdateDatabase()
        {
          const string roleNameToAssign = "Portal User";
    
          AccessUsers accessUsers = PXGraph.CreateInstance<AccessUsers>();
    
          // For each users in the system
          foreach (Users user in accessUsers.UserList.Select())
          {
            // Modify only guest users
            if (user.Guest != true)
              continue;
    
            try
            {
              // Set current user
              accessUsers.UserList.Current = user;
    
              // Assign role
              EPLoginTypeAllowsRole role = (EPLoginTypeAllowsRole)accessUsers.AllowedRoles.Select().RowCast<EPLoginTypeAllowsRole>()
                .Where(x => x.Rolename.Contains(roleNameToAssign)).FirstOrDefault();
    
              role.Selected = true;
              accessUsers.AllowedRoles.Update(role);
              WriteLog("User " + user.Username + " updated.");
            }
            catch (Exception ex)
            {
              WriteLog("Error: " + ex.Message);
            }
          }
    
          accessUsers.Save.Press();
        }
      }
    }