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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Disable field based on UserId using Javascript

(0) ShareShare
ReportReport
Posted on by 85

Hi community

I'm wanting to disable some fields based on a userId. I've tried the below script without success.

Any assistance is greatly appreciated.

function sample() {
	// Obtain the User ID from context of the page
	var UserId = Xrm.Page.context.getUserId();
	var UserName = Xrm.Page.context.getUserName();
	// Check for User Id match
	if (UserId == "A45413E2-2B07-EB11-A812-000D3A0C2AB4") {          
	// Set field to Disabled
	Xrm.Page.getControl("fieldName").setDisabled(true);
	}
	else {
	// Leave as is
	}
}

I have the same question (0)
  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    Try below code.

    function sample() {

    // Obtain the User ID from context of the page

    var UserId = Xrm.Page.context.getUserId();

    var UserName = Xrm.Page.context.getUserName();

           UserId=UserId.replace('{','').replace('}','');

    // Check for User Id match

    if (UserId == "A45413E2-2B07-EB11-A812-000D3A0C2AB4") {          

    // Set field to Disabled

    Xrm.Page.getControl("fieldName").setDisabled(true);

    }

    else {

    // Leave as is

    }

    }

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Dynamics_DR Profile Picture
    85 on at

    Than you Bipin for the response

    This did not work. There is no error....it just doesn't validate on the UserId.

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    Try to add alert in your code to see what is happening. Please see below code -

    function sample() {

    // Obtain the User ID from context of the page

    var UserId = Xrm.Page.context.getUserId();

    alert("Original User ID"+UserId );

    var UserName = Xrm.Page.context.getUserName();

          UserId=UserId.replace('{','').replace('}','');

    alert("After Replace User ID"+UserId );

    // Check for User Id match

    if (UserId == "A45413E2-2B07-EB11-A812-000D3A0C2AB4") {          

    // Set field to Disabled

    alert("Condition satisfied");

    Xrm.Page.getControl("fieldName").setDisabled(true);

    }

    else {

    alert("Condition Not satisfied");

    // Leave as is

    }

    }

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Dynamics_DR Profile Picture
    85 on at

    Thanks Bipin

    I applied the alerts which worked as below image. The field still isn't 'disabled'.

    pastedimage1645195056860v1.png

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    I hope you are replacing fieldName with actual field schema name something like new_name

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    I see you are using Xrm.Page which is now deprecated. I have updated your code and verified on my personal tenant.

    function sample(executionContext) {

    // Obtain the User ID from context of the page

    var formContext=executionContext.getFormContext();

    var userSettings = Xrm.Utility.getGlobalContext().userSettings;

    var UserId = userSettings.userId;

    alert("Before"+UserId);

    var UserName = userSettings.userName;

    UserId=UserId.replace('{','').replace('}','');

    alert("After"+UserId);

    // Check for User Id match

    if (UserId == "A45413E2-2B07-EB11-A812-000D3A0C2AB4") {          

    // Set field to Disabled

    alert("condition satisfied");

    formContext.getControl("msft_employeenumber").setDisabled(true);

    }

    else {

    alert("condition not satisfied")

    // Leave as is

    }

    }

    3566.CR22.PNG

    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-utility/getglobalcontext/usersettings#userid

    docs.microsoft.com/.../getformcontext

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Dynamics_DR Profile Picture
    85 on at

    Hi Bipin

    Thanks again for the feedback.

    I am using the correct fieldName and the web resource file is loaded and enabled correctly. It is working for the alerts but not disabling the field even with the change to formContext.getControl.

    pastedimage1645338256129v1.png 

    I'm not sure what else to try. Here is my script.

    **************************

    function sample(executionContext) {
    // Obtain the User ID from context of the page
    var formContext=executionContext.getFormContext();
    var userSettings = Xrm.Utility.getGlobalContext().userSettings;
    var UserId = userSettings.userId;
    alert("Before"+UserId);
    var UserName = userSettings.userName;
    UserId=UserId.replace('{','').replace('}','');
    alert("After"+UserId);
    // Check for User Id match
    if (UserId == "A45413E2-2B07-EB11-A812-000D3A0C2AB4") {
    // Set field to Disabled
    alert("condition satisfied");
    formContext.getControl("cr91f_productsignedoff").setDisabled(true);
    }
    else {
    alert("condition not satisfied")
    // Leave as is
    }
    }

    ****************************

    The UserId I'm using is mine. I'm an administrator so would this have any impact? 

  • necsa Profile Picture
    3,455 on at

    Hi,

    Is there any Workflow or Plugin depending on this field? Please check it. Maybe these prevent to disabled mode.

  • Dynamics_DR Profile Picture
    85 on at

    Hi Necsa

    Yes the field is included in the Opportunity Sales Process (workflow) on the opportunity. What is needed to solve this?


    pastedimage1645414705466v1.png

  • Suggested answer
    shivaram Profile Picture
    3,315 on at

    Hi,

    Seems this field is in Business Process Flow and you are trying to disable which is in stage? If yes, please add header_process as prefix.

    formContext.getControl("header_process_cr91f_productsignedoff").setDisabled(true);

    Thank you,

    Shivaram.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 33 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans