Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

form JavaScript

(0) ShareShare
ReportReport
Posted on by 1,942

Hi all,

I was trying to lock & unlock the form fields using the below code and it doesn't seem to be working.


Did i miss anything here?

function editpkey(){
var cform = Xrm.Page.ui.getFormType();
if (cform == 1){
Xrm.Page.getControl("name").setDisabled(false);
}

*This post is locked for comments

  • Verified answer
    Vighnesh Profile Picture
    Vighnesh 1,942 on at
    RE: form JavaScript

    Hello All,

    After hours of analysis and troubleshooting with debugger i finally got it working after making some changes to the syntax. Please find the updated code.

    function editpkey(){
    debugger;
    var cform = Xrm.Page.ui.getFormType();
    var userName = Xrm.Page.context.getUserName();
    if (cform == 1){
    Xrm.Page.getControl("name").setDisabled(false);
    }
    else if (cform == 2){
    if (userName == "Admin C"){
    Xrm.Page.getControl("name").setDisabled(false);
    }
    else if (cform == 2){
    if(userName != "Admin C"){
    Xrm.Page.getControl("name").setDisabled(true);
    Xrm.Page.getAttribute("name").setSubmitMode("always");
    }
    }
    }
    }

  • Vighnesh Profile Picture
    Vighnesh 1,942 on at
    RE: form JavaScript

    i receive the following error.

    account_5F00_error.jpg

  • Vighnesh Profile Picture
    Vighnesh 1,942 on at
    RE: form JavaScript

    Sorry for the typo mistake, i have corrected the code

  • RE: form JavaScript

    Xrm.Page.getControl("control").setDisabled(false);

    Is there a control field on form?

  • Vighnesh Profile Picture
    Vighnesh 1,942 on at
    RE: form JavaScript

    In my situation i have to check using user name.

  • Suggested answer
    RE: form JavaScript

    By Looking at your script it looks like you have assumed a user will have name Administrator. I think Security role will be Administrator not the user name. Please use below code line to get user security role:

    Xrm.Page.context.getUserRoles();

    Refer link:

    https://community.dynamics.com/crm/b/microsoftdynamicscrmxrm/archive/2014/05/15/ms-dynamics-crm-login-user-39-s-security-role-using-javascript

  • Vighnesh Profile Picture
    Vighnesh 1,942 on at
    RE: form JavaScript

    I was trying to lock the field 'name' on the account form when the form type is update, in case of create i am allowing the user to enter value into it (only for admin).

    I don't know if i am using the correct library to lock or unlock the field here, below is my complete code.

    function editpkey(){

    debugger;

    var cform = Xrm.Page.ui.getFormType();

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

    if (cform == 1){

    Xrm.Page.getControl("name").setDisabled(false);

    break;

    }

    if (cform == 2 && userName =! "Administrator"){

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

    Xrm.Page.getAttribute("name").setSubmitMode("always")

    break;

    }

    if (cform == 2 && userName == "Administrator"){

    Xrm.Page.getControl("name").setDisabled(false);

    break;

    }

    }

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,076 on at
    RE: form JavaScript

    Hi Vighnesh,

    I think the field "name" is a composite field. Locking or unlocking composite fields is a different scenario.

    Please refer the link below.

    www.inogic.com/.../lockunlock-composite-fields-using-java-script-in-dynamics-crm

  • RE: form JavaScript

    code syntax is correct. Is there a field "name" on the form?

    What does the error say?

  • Vighnesh Profile Picture
    Vighnesh 1,942 on at
    RE: form JavaScript

    Hi Purva,

    When i run the debugger i am getting an error at this line: Xrm.Page.getControl("name").setDisabled(false);

    Is this the correct library to use to lock & unlock the field on the form?

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans