Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum

JavaScript to lock fields based on owner and active stage

Posted on by 140

Hey guys so anyone know a good java script to lock and unlock all the fields on a stage based on owner and active stage  

Thanks 

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: JavaScript to lock fields based on owner and active stage

    Hi ,

    Try with this - 

    function processflow() 
    {
        Xrm.Page.data.process.addOnStageChange(setReadOnlyFields);
    }
    
    function setReadOnlyFields() {
        // You can also check with selected stage name 
        //  var SelectedStageName = Xrm.Page.data.process.getSelectedStage().getName().toString().toLowerCase(); 
        var activeStage = Xrm.Page.data.process.getActiveStage().toLowerCase();
        var name = activeStage.getName();
        if (name == "pending review") 
        {
            // lock BPF field
            var field1 = Xrm.Page.getControl("header_process_fieldname"); 
            if (field1!= null) {
                field1.setDisabled(true);
            }
            // lock form field
            Xrm.Page.getAttribute("fieldname").setDisabled(true);
        }
    
    }


  • LeoAlt Profile Picture
    LeoAlt 16,329 on at
    RE: JavaScript to lock fields based on owner and active stage

    Hi msboy,

    1.You need to add the business rule in the main form entity.

    2.Could you share your code so that we could do test or debug for you.

    Best Regards,

    Leo

  • Suggested answer
    Kokulan Profile Picture
    Kokulan 18,048 on at
    RE: JavaScript to lock fields based on owner and active stage

    Business Rule should be on your main entity, you do not have to create that on BPF entity. The fields that you want to lock and unlock are on the main entity.

    Please see below as to how you can createt he Business Rule to lock and unlock

    ScreenClip-_5B00_267_5D00_.png

    You need to rules, one to check Stage and other is to check the owner

    To check the BPF stage via the category field,

    ScreenClip-_5B00_265_5D00_.png

    And to check the Owner 

    ScreenClip-_5B00_266_5D00_.png

    Both of these rules are for the main condition, the Text view should look like the following

    ScreenClip-_5B00_267_5D00_.png

  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    Alex Fun Wei Jie 33,626 on at
    RE: JavaScript to lock fields based on owner and active stage

    Hi,

    you also need to do the checking during form onload.

  • msboy Profile Picture
    msboy 140 on at
    RE: JavaScript to lock fields based on owner and active stage

    hey so I tried to use bussines rule to lock fields, but its not working

    and should the bussines rule be in the bpf entity or the main form enity

    also I used a similar javascript, but when the user refresh the page the fields just unlocks

  • Suggested answer
    LeoAlt Profile Picture
    LeoAlt 16,329 on at
    RE: JavaScript to lock fields based on owner and active stage

    Hi partner,

    1.You could use js to lock entire form.

    function ReadOnly ()
    
    {
    
       var controls = Xrm.Page.ui.controls.get();
    
       for (var i in controls)
    
       {
    
           var control = controls[i];
    
           if (!control.getDisabled())
    
           {
    
               control.setDisabled(true);
    
           }
    
       }
    
    }


    2.Since you said that you want to do this action based on owner or stage, I think using Business Roles is the best way for you.

    community245.png

    community245.png

    Here is a blog for you to refer to.

    https://community.dynamics.com/crm/b/passiondynamics/archive/2018/01/06/lock-all-fields-on-a-form-using-javascript-microsoft-dynamics-crm

    Hope it helps,

    Best Regards,

    Leo

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans