web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Disable A Field on Form based on CurUserID

(0) ShareShare
ReportReport
Posted on by 384

Dear experts,

i am trying to disable a field on Hcmworker form, the field is in a customized table called "XWorkerX" and the field is WorkerCNSS

what i am trying to achieve is the below:

whenever the logged in user (curiserid) equals to a worker on hcmworker i want that field to be enabled, otherwise the field will be disabled,

the purpose is that the employees has access to the worker form, i have no problem with that, but since they have access they can change WorkerCNSS field,

i tried the below code on Activated Datasource method but it didnt work, the field is always disabled

FormDataSource XworkerXDS = sender.formrun().dataSource("XworkerX");
select DirPersonUser where DirPersonUser.User == curUserId();
while select hcmworker join AllUsers where AllUsers.PersonParty == hcmworker.Person
{
  if(AllUsers.user != curUserId())
  XworkerXDS.object(fieldNum(XworkerX,WorkerCnss)).enabled(false);
  else
  XworkerXDS.object(fieldNum(XworkerX,WorkerCnss)).enabled(True);
       

}

I have the same question (0)
  • Martin Dráb Profile Picture
    239,874 Most Valuable Professional on at

    What's the point of iterating all workers? Why don't you simply compare the active worker with the current user? Unfortunately I don't know your tables, therefore I can't tell how exactly they're related.

    By the way, you select DirPersonUser but never use it for anything.

  • Suggested answer
    Gunjan Bhattachayya Profile Picture
    35,429 on at

    Hi Sylvester,

    You should do this in the OnIntialized method rather than OnActivated.

    You can use something like this -

    FormDataSource XworkerXDS = sender.formrun().dataSource("XworkerX");
    hcmWorker =  HcmWorker::findByPerson(DirPersonuser::find(curUserId()).PersonParty);
    boolean isEnabled = (hcmWorker.RecId == 0) ? false : true;
    XworkerXDS.object(fieldNum(XworkerX,WorkerCnss)).enabled(isEnabled);
    

  • Suggested answer
    nmaenpaa Profile Picture
    101,172 Moderator on at

    Once you get your code working, not even administrator can change other worker's information regarding this field. Is this what you want?

    If not, you could use eXtensible Data Security to limit access to this field.

  • sylvesterPowerBi Profile Picture
    384 on at

    yes Nikolaos, thats what i want, since it's considered like a password field, no one can set it but the user itself

  • sylvesterPowerBi Profile Picture
    384 on at

    Hi Martin,

    how can  i get the active worker  ? and not the active user

  • Suggested answer
    nmaenpaa Profile Picture
    101,172 Moderator on at

    Quick web search helps with "how to get the active worker": community.dynamics.com/.../d365o-how-to-get-current-worker-through-x

    I don't recommend having fields that administrators can't update. That could cause serious issues in the future. You should use the security framework for this.

  • sylvesterPowerBi Profile Picture
    384 on at

    Hi Gunjan,

    i tried your code, but the field is always enabled.

  • Gunjan Bhattachayya Profile Picture
    35,429 on at

    Hi Sylvester,

    Understood your requirement now. If you still want to do this using code, you can try the code below.

    You may also want to look at XDS as Nikolaos suggested.

    FormDataSource XworkerXDS = sender.formrun().dataSource("XworkerX");
    Hcmworker hcmWorkerCurrent =  HcmWorker::findByPerson(DirPersonuser::find(curUserId()).PersonParty);
    boolean isEnabled = (hcmWorker == hcmWorkerCurrent) ? true : false;
    XworkerXDS.object(fieldNum(XworkerX,WorkerCnss)).enabled(isEnabled);

  • Verified answer
    André Arnaud de Calavon Profile Picture
    305,865 Super User 2026 Season 1 on at

    Hi Sylvester,

    XDS can't be used on individual fields, only whole records. As you have a new table for one field, you can indeed implement XDS. Note that the field will be open for editing using this framework; only during saving the record, it will then evaluate if the user is allowed to make changes or not. To directly lock the field, you need coding.

    The Human Resources do have an option to secure private contact details. Part of this solution is the option to set contact details to private or remove this tick box. This can be done by users part of roles which are setup on a parameter form, but also it is always open for the own worker record. This last part is also done with x++ coding. You can have a look at my blog where I explain this feature. Then you can use the technical implementation of this as example for your field. dynamicspedia.com/.../

  • sylvesterPowerBi Profile Picture
    384 on at

    Thank you André

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 461 Super User 2026 Season 1

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 428 Super User 2026 Season 1

#2
Subra Profile Picture

Subra 428

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans