Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

JS for locking form

(0) ShareShare
ReportReport
Posted on by 17

Hi all,

I don’t have much idea on JS. My requirement is that when a record is created in an entity after 24 hrs all the fields needs to be locked.

After 24hrs(or)1 day after creating a record the fields must lock on the form

can anyone help me with the script? Please

  • Guys5 Profile Picture
    Guys5 17 on at
    RE: JS for locking form

    Hi

    yes your script worked perfect but I can’t see the YES option above your response for marking

    Anyways thanks for the help

  • Verified answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: JS for locking form

    Hello,

    If my reply (or replies) answers your question take a minute and verify the answer. You can do it by clicking "Yes" at the top of the reply (or replies) under the label "Does this answer your question?"

  • Guys5 Profile Picture
    Guys5 17 on at
    RE: JS for locking form

    Hi @Andrew Butenko and @Leah Ju

    I’ve added 

    var FormType syntax and update the if condition to

    ”if(diff hours > 24 && FormType == 2)

    and is working perfect

    Thanks for the help

  • Guys5 Profile Picture
    Guys5 17 on at
    RE: JS for locking form

    Thx a33ik

    Is there a way such that the new form doesn’t get locked?

    Because when I click on new the form still gets locked but it should be supposed to lock fields only if the records is created after 24hrs and not for the new form

    Please help and thanks

  • Guys5 Profile Picture
    Guys5 17 on at
    RE: JS for locking form

    Thx for the help   
    It’s working perfect but the issue is when I select a new form it still locking all the fields because we triggered it on OnLoad

    Is there a way that it should only lock for records which are older than 24hrs and not for new forms?

    Thanks in advance

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JS for locking form

    Hi Partner,

    1.Adding the created on field to the form firstly:

    pastedimage1664775840568v1.png

    2.Adding the following code to the form onload:

    function lockForm(executionContext) {
        var formContext = executionContext.getFormContext();

        //You should get the Datetime field by CRM SDK
        var createdOn = formContext.getAttribute("createdon").getValue();
        var currentTime = new Date();

        //calculate the difference
        dateTimeDiff = currentTime - createdOn;
        var diffhours = (dateTimeDiff / 1000) / 3600;
        alert(diffhours);

        //lock form
        if (diffhours > 24) {
            formContext.data.entity.attributes.forEach(function (attribute, index) {
                let control = formContext.getControl(attribute.getName());
                if (control) {
                    control.setDisabled(true)
                }
            });
        };
    }

    3.Test Result:

    pastedimage1664775471493v1.png

  • Oleta V. Reed Profile Picture
    Oleta V. Reed 5 on at
    RE: JS for locking form

    Hey guys so I am trying to unlock and lock all the field on a form based some condition

    I have a field shows the current user so I am saying if  sup == userCurrent lock all the field

    and if officer == userCurret  unlock all the field field

    so I did not get any script error, but code does not lock the field when the lock condition is met

    it unlock all the field regardless of condition

    if anyone can help and feedback is appreciated Thanks

    function Role(){

    var activeStage = Xrm.Page.data.process.getActiveStage();

       var name = activeStage.getName();

    var PSRstatus = Xrm.Page.getAttribute("psr_psrstatus").getText();

    var Lksup = Xrm.Page.getAttribute("psr_reportedtoid").getValue();

       var Lkofficer = Xrm.Page.getAttribute("psr_reportingofficer").getValue();

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

    var userCurrent = Xrm.Page.data.entity.attributes.get("psr_currentuser").getValue();

    if ((Lkofficer != null) && (Lksup != null)){

    var officer = Lkofficer[0].name;

    var sup = Lksup[0].name;

    if ((PSRstatus == "Return (1st)" || PSRstatus == "Return (2nd)") &&

    (sup == userCurrent) && (name == "In-Progress"))

    {

             setReadOnlyFields();

    }else if ((PSRstatus == "Return (1st)" || PSRstatus == "Return (2nd)") &&

    (officer == userCurrent) && (name == "In-Progress"))

    {

    unlockFormFields();

    }

    }

    function setReadOnlyFields() {

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

          attribute.controls.get().forEach(function (control) {

               if (control) {

                   control.setDisabled(true);

               }

           });

       });

    }

    function unlockFormFields()

    {

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

          attribute.controls.get().forEach(function (control) {

               if (control) {

                   control.setDisabled(false);

               }

           });

       });

    }

    }

    It help you to unlock all field on Javascript it is working i also use this here apkpouch.com/.../

  • Verified answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: JS for locking form

    Hello,

    Check this script out:

    function FormOnLoad(executionContext) {
    	var formContext = executionContext.getFormContext();
    	var createdon = formContext.getAttribute("createdon").getValue();
    	if (!createdon) {
    		return;
    	}
    
    	var dateDiff = (new Date()) - createdon;
    	var diffInHours = dateDiff/1000/60/60;
    
    	if (diffInHours < 24) {
    		return;
    	}
    
    	var allControls = formContext.ui.controls;
    	allControls.forEach(function(control){
    		if (control.setDisabled) {
    			control.setDisabled(true);
    		}
    	});
    }

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

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,414 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans