Permission to enable certain fields depends on CurrentUserID Epicor ERP10

556 Views Asked by At

I need help. I need to enable certain fields depends on CurrentUserID. There is one field which is UltraCombo contains of Employee's name. When the Employee's Name is selected, the other fields should be enabled if the CurrentUserID is matched with the selected Employee's name. Otherwise, the other fields should be locked. I tried to use the CanView method in the code but I don't know how to call in SQL command. Plese help me T-T

    private bool CanView(string field)
{
    bool result = true;
    EpiDataView edv = oTrans.EpiDataViews["CallContextClientData"] as EpiDataView;
    string CurrentUser = edv.dataView[edv.Row]["CurrentUserId"].ToString();
    string ConnectionString = "Data Source=RWNAERP;Initial Catalog=ERP10TESTRWNA;Persist Security Info=True;User ID=sa;Password=Epicor10";
    string CompanyId = ((Ice.Core.Session)(oTrans.Session)).CompanyID;
    string UserID = ((Ice.Core.Session)(oTrans.Session)).UserID;
    using (SqlConnection connection1 = new SqlConnection(ConnectionString)) 
    {
        DataTable dt = new DataTable();
        connection1.Open();
        SqlCommand cmd = new SqlCommand("SELECT DcdUserID FROM dbo.UserFile WHERE Name=@Name AND EmpID=@EmpID", connection1);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.Add("DcdUserID", SqlDbType.NVarChar).Value = UserID;
        SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);
        sqlDa.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            result = false;
        }
        if (CurrentUser != "")
        {
            result = true;
        }
        connection1.Close();
        connection1.Dispose();
    }
1

There are 1 best solutions below

0
On

What you are trying to do is on the client, but the client only connects to the AppServer and never to the SQL Database. Only the AppServer should connect to the database.

Assuming you are adding this code as a customisation script, it's much easier to get the current user info from the Session variable i.e.

        var session = (Ice.Core.Session)oTrans.Session;
        var userId = session.UserID;
        var userName = session.UserName;
        var userEmail = session.UserEmail;

In the Epicor.exe client disabling fields is best done with a RowRule, i.e.

        var callContextClientData = oTrans.Factory("CallContextClientData");
        var disableFieldsForUser = new RowRule("CurrentUserId", RuleCondition.Equals, ((Ice.Core.Session)trans.Session).UserID);
        disableFieldsForUser.AddAction(RuleAction.AddControlSettings(stockDtlEpiDataView, "CallContextClientData.ShortChar01", SettingStyle.Disabled));
        callContextClientData.AddRowRule(disableFieldsForUser);

It's not clear which fields you are matching or which you want to disable, but hopefully this should get you started.