How to Enable and Disable User Account by Epicor Function

I am using below code to Enable or Disable the User account. It is working in script editor but while using the similar code in Epicor function maintenance then it is not working.

Script Editor

// Declare and Initialize EpiDataView Variables
// Declare and create an instance of the Adapter.
UserFileAdapter adapterUserFile = new UserFileAdapter(this.oTrans);
adapterUserFile.BOConnect();
bool objds = adapterUserFile.GetByID(“Test”);
//var user = ds.UserFile.FirstOrDefault();
if(objds)
{
adapterUserFile.UserFileData.Tables[“UserFile”].Rows[0][“UserDisabled”] = “false”;
adapterUserFile.Update();
}
adapterUserFile.Dispose();

Epicor Function Maintenance

CallService<Ice.Contracts.UserFileSvcContract>(svcUF =>
{
Ice.Tablesets.UserFileTableset userFileDataSet = new Ice.Tablesets.UserFileTableset();
userFileDataSet = svcUF.GetByID(this.UserID);
var userFile = userFileDataSet.UserFile.FirstOrDefault();
if(userFile != null)
{
userFile.UserDisabled = false;
svcUF.Update(ref userFileDataSet);
DebugMsg = userFile.Name;
}
});

Thanks in advance.

try to add
userFile.RowMod=“U”;
before Update

2 Likes

It is not working.

I never succeeded. I used a workaround.

Try looking at this post i was able to do it. I just used updateext

1 Like

@Devin.Draeger , I have tried but it is not working below is my code

try
{
    this.CallService<Ice.Contracts.UserFileSvcContract>(uf =>
    {
      var dsEnableUf = new Ice.Tablesets.UpdExtUserFileTableset();
      var dsUf = uf.GetByID(UserID);
      bool errorsOut = false; 
      var ufr = new UserFileRow();
      ufr.UserID = dsUf.UserFile[0].UserID;
      ufr.ClearPassword = true;
      ufr.PasswordBlank = true;
      ufr.PasswordEmail = "";
      ufr.PwdExpiresDays = 0;
      ufr.PwdExpires = DateTime.Today;
      ufr.PwdGrace = 3;
      ufr.UserDisabled = false;
      ufr.RowMod = "U";
      dsEnableUf.UserFile.Add(ufr);
      uf.UpdateExt(ref dsEnableUf,false,true, out errorsOut);

    }) ;   
    Success = true;    
}
catch (Exception ex)
{
    Success = false;
    ErrorMsg = ex.Message;
}

are you getting any error messages in the server event logs?

1 Like

I also remember having difficulty with Update and found that UpdateExt did the trick

I am not getting any error but user is not enabled.

Hold on

1 Like

I’ve got something like this running in production as part of our offboarding process. I used UpdateExt to bypass some annoying errors. It required a service account with admin privileges to actually work, since admin rights are required to modify users.

1 Like

Got it, un momento

BeforeImage Strikes Again… @josecgomez :rofl:
This’ll do it.

try
{
    this.CallService<Ice.Contracts.UserFileSvcContract>(uf =>
    {
        //Get the dataset
        var dsUserFile = uf.GetByID(UserID);
        
        //UnModified Record -> BeforeImage...
        var userFile = dsUserFile.UserFile.FirstOrDefault();
        
        //Get a new Record, this will be our updated Row
        Ice.Tablesets.UserFileRow updatedUserFileRow = (Ice.Tablesets.UserFileRow)dsUserFile.UserFile.NewRow();
        
        //Copy the unmodified row to the new row
        BufferCopy.Copy(userFile, updatedUserFileRow);
        
        //Modify the new row
        updatedUserFileRow.UserDisabled = !enable;
        updatedUserFileRow.RowMod = "U";
        
        //Add it to the dataset
        dsUserFile.UserFile.Add(updatedUserFileRow);
        
        //Update
        uf.Update(ref dsUserFile);

    });
    
    Success = true;    
}
catch (Exception ex)
{
    Success = false;
    ErrorMessage = ex.Message;
}
5 Likes

Getting below mentioned error

{
“Success”: false,
“ErrorMsg”: “You are not allowed to modify other users unless you are a security manager.”,
“DebugMsg”: null
}
But I have already access of Security Manager.

But is your function running in the context of a security manager?

Here is the library, run my exact code through Swagger (Rest API help page)

ScratchTests.efxj.txt (2.5 KB) ← Remove .txt

Could you please attach the correct format of Library?

That is the correct format, just remove .txt from the end, and when you chose import, change the type to efxj json in the lower right corner.

I have tried but it is again showing text file.

ScratchTests.efxb (2.3 KB)

2 Likes