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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Javascript to lock specific fields

(0) ShareShare
ReportReport
Posted on by

I need a way to lock an array of named fields based on a record's status.  I have thoughtfully considered the "Active" vs. "Inactive" states, but these don't meet my needs because they lock all the fields and I still need to be able to update the Status Reason.  

The requirement is that at a certain point in a record's lifecycle, it should become locked so user's can't change the values, but it still needs to pass through various other states to reflect it's processing.

I'm thinking the solution is a JS function to lock an array of fields.  Would someone be willing to share what that would look like?  I really want to learn how to expand my solution's capabilities through the use of JS.

Thank you!

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Saroj Das Profile Picture
    3,355 on at
    RE: Javascript to lock specific fields

    Hi Debra,

    Please find below code snippet for getting all the field and Disabled them.

    Xrm.Page.data.entity.attributes.forEach(function (attribute, index) {    

       var fields = Xrm.Page.getControl(attribute.getName());

       if (fields) { // you can check the field name here also like if(fields=="new_myfield")

           fields.setDisabled(true)

       }

    });

    Please mark my answer verified if it helps you.

    Thanks,

    Saroj

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: Javascript to lock specific fields

    Here is the line that you have to add in the JS

    Xrm.Page.ui.controls.get("your field name").setDisabled (true); //Field readonly  you can't write any thing on field

    if you change true as false;

    Xrm.Page.ui.controls.get("your field name").setDisabled (false); //Anyone can put value on this field

  • Community Member Profile Picture
    on at
    RE: Javascript to lock specific fields

    Sorry that is just the name you can call in statusReasonChange and then add your library and script call to your form and field. 

    You can take the field names in a couple of ways. The easiest is to just have lots of the genericLock calls, 1 for each field you want to lock. The other would be to use the parameters in the web resource declaration itself and add a list of string field names. You would need to handle this code in the 2nd script using a string array etc.

    For the child records, you could have an inactive status and then use a workflow the a custom workflow to change these to an inactive/locked status. The other way if again there are some fields to be edited would be to have a separate jscript library that checks the status reason of the parent record and if of a certain stage then the fields get locked.

    Ideally it would be the former of these 2 and there is a 3rd party component (free) that you can use.

    https://crm2011distributewf.codeplex.com/

    Hope this helps.

  • Cyclefitness Profile Picture
    on at
    RE: Javascript to lock specific fields

    Thank you, Andy!  Not sure I'm following the Status Reason lock.  I'm thinking on Status Reason change.  Can you show me how to make the second function take an array of field names?

    I also need to lock the related child entities.  In other words; this is a Purchase Order and it has many related line items.  Each of those need to be locked as well.

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: Javascript to lock specific fields

    You can write a generic function that takes field name as a parameter, then if you progress through a series of stages (tied to your status reasons) then update of the status reason if is your status or after then you call another function. This 2nd function has a load of fields that you want to lock down on the form.

    Something like.

    function genericLock(fieldname, lock){

         var fieldObj = Xrm.Page.getAttribute(fieldname);

         if(fieldObj != null){

            fieldObj.setDisabled(lock);

         }

    }

    function onStatusReasonLock(){

      genericLock('crm_myfield', true);

    )

  • Suggested answer
    Sean K Profile Picture
    1,537 on at
    RE: Javascript to lock specific fields

    Hi Debra,

    One way you could probably achieve this is by using a Business Rule (Admittedly this wouldn't be JS!).

    Regards,

    Sean

  • Cyclefitness Profile Picture
    on at
    RE: Javascript to lock specific fields

    It would probably be easier to lock everything, then unlock Status and Status Reason.

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…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Aric Levin - MVP Profile Picture

Aric Levin - MVP 2 Moderator

#2
MA-04060624-0 Profile Picture

MA-04060624-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans