Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

AutoSave a form based on value

Posted on by 230

Hi,

I have the JS below which i've enabled on fields of a form to autosave when the field is dirty (unsaved changes). Can someone please help me add a condition to indicate "if field value = WTVR or != WTVR". If you could give me the complete JS as a whole, i would appreciate it. What i would like to accomplish is to AutoSave only if the field contains, or does not contain, a specific value, or null. Lets assume the field i'd like to check is called "Holding" and the filed value is "Walked". If Holding Value = Walked then dont AutoSave.

Also, I have a business rule to show an error msg on a field based on a condition. When i try to use this AutoSave on that form it doesn't work, ever after the error condition is cleared. It sounds like the JS is running prior to the error being cleared? What i see happening is that when i clear the error msg condition (condition in the business rule), i see the error msg next to the Save icon which is of course the AutoSave trying to save, but at this time, i can of course Save the form manually because the error msg is gone. 

function SaveForm() {

if (Xrm.Page.data.entity.getIsDirty()) {

Xrm.Page.data.save();

}

}

*This post is locked for comments

  • JoeO Profile Picture
    JoeO 230 on at
    RE: AutoSave a form based on value

    Abby, I tried your script as well, but its just not doing anything. I can confirm the script loads (debugger)

    function AutoSave() {
        if (Xrm.Page.getAttribute("new_casestatus").getValue() != 191,170,000) {
            Xrm.Page.data.save();
        }
        else if (Xrm.Page.getAttribute("new_casestatus").getValue() == 191,170,000) {
            //Xrm.Page.ui.setFormNotification("Field 1 Info.", "INFORMATION")
            Xrm.Page.ui.setFormNotification("Test Error", "ERROR")
        }
    }

    JS-AutoSave.png

  • JoeO Profile Picture
    JoeO 230 on at
    RE: AutoSave a form based on value

    Hi Goutam,

    Thanks so much for the response, and sorry about the delayed response. I tried what you suggested, but its not working for me. Its basically not doing anything. Just to point out - and i think i missed this point previously - when the form loads, the OptionSet field in question is empty. It just want that when the user selects a value in the OptionSet, the form should AutoSave, except if the selection is a specific OptionSet value. In my case, the option set value is "191,170,000". So the form loads, the OptionSet field is blank. The user then selects a OptionSet value while in the form, then AutoSave the form. The user selects an OptionValue which is matching "191,170,000", dont AutoSave the form

    var  CaseStatus = Xrm.Page.getAttribute("new_casestatus").getValue(); 
    
    function OnLoadFieldDirtyCheckAndSave()
    {
        
        var intervalId = window.setInterval(
         function () {
             window.clearInterval(intervalId);
        
             if(Xrm.Page.data.entity.getIsDirty())
             {
                 var CaseStatus =Xrm.Page.getAttribute("new_casestatus").getValue();
                 if( CaseStatus != 191,170,000)
                     Xrm.Page.data.save();
             }
    
         }, 2000);
    }


  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: AutoSave a form based on value

    Hi,

    For optionset, you need to check the value. Example-

    var optionsetValue= Xrm.Page.getAttribute("<schema name of your field>").getValue();

    if(optionsetValue === 100,000,00) // Get the actual value from the optionset field

    {

    //True do this

    }

    else

    {

    // false, do this

    }

    Hope this helps.

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: AutoSave a form based on value

    You can also write below line inside the function in the first line.

    var HoldingVaue = Xrm.Page.getAttribute("fieldname").getValue();

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: AutoSave a form based on value

    Hi ,

    In the javascript web resource you can declare a global variable which will holds the current value  and then inside the method every time you will retrieve the value and check. If changes then save the form.

    var HoldingVaue = Xrm.Page.getAttribute("fieldname").getValue(); //Golbal Variable will assign at the time of javascript file load.
    
    function OnLoadFieldDirtyCheckAndSave()
    {
        
        var intervalId = window.setInterval(
         function () {
             window.clearInterval(intervalId);
        
             if(Xrm.Page.data.entity.getIsDirty())
             {
                 var changeValue =Xrm.Page.getAttribute("fieldname").getValue();
                 if( HoldingVaue != changeValue)
                     Xrm.Page.data.save();
             }
    
         }, 2000);
    }


    Hope this will match your requirement .

  • JoeO Profile Picture
    JoeO 230 on at
    RE: AutoSave a form based on value

    Thanks. This is very helpful. However, it doesnt address my need. Im looking to enable AutoSave on a field IF a certain OptionSet field does not contain a certain value.

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: AutoSave a form based on value

    Hi ,

    You can try with below code, register below function in on-load event of the form, It will check every 2 seconds and if its found dirty then save the forms. However you can reduce and increase the time as per your choice in milliseconds.

    function OnLoadFieldDirtyCheckAndSave()
    {
        var intervalId = window.setInterval(
         function () {
             window.clearInterval(intervalId);
        
             if(Xrm.Page.data.entity.getIsDirty())
             {
                 Xrm.Page.data.save();
             }
    
         }, 2000);
    }


    Hope this helps.

  • JoeO Profile Picture
    JoeO 230 on at
    RE: AutoSave a form based on value

    This is the other way around. It enables AutoSave globally, and and then i can disable on individual forms (hours of work). This wouldn't make any sense when i wish to enable AutoSave ONLY on one form and on one field.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: AutoSave a form based on value

    Hi,

    Follow this article, it may answer your question.

    you can prevent autosave using any business logic you need.

    there is also a complete sample how to do it.

    technet.microsoft.com/.../dn531073.aspx

    Goodluck.

    Ayman

  • JoeO Profile Picture
    JoeO 230 on at
    RE: AutoSave a form based on value

    Hi,

    Thanks for the response. It doesn't work for me, but i've also failed to mention that the field is an OptionSet field. I tried the JS below (AutoSave IF the Holding OptionSet field does not = Walked), it works, but it doesnt read the IF condition.

    function SaveForm() {
    var holding = Xrm.Page.getAttribute("new_holding").getValue();
    if (holding != "Walked")
    {
    if (Xrm.Page.data.entity.getIsDirty()) 
    {
    Xrm.Page.data.save();
           }        
    
       }
    
    }

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans