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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

AutoSave a form based on value

(0) ShareShare
ReportReport
Posted on by 238

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

I have the same question (0)
  • Abby Kong Profile Picture
    6 on at

    Hello JoeO,

    Don't think you can trigger AutoSave inside Business Rules.

    To AutoSave a form based on a field value, assume the field is a text field, you can register a function to field OnChange.

    function new_field1_onchange() {
        if (Xrm.Page.getAttribute("new_field1").getValue() == 'Trigger') {
            Xrm.Page.data.save();
        }
        else if (Xrm.Page.getAttribute("new_field1").getValue() == 'ShowError') {
            //Xrm.Page.ui.setFormNotification("Field 1 Info.", "INFORMATION")
            Xrm.Page.ui.setFormNotification("Field 1 Error", "ERROR")
        }
    }
    
  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    You can add the following:

    function onSave(eventArgs) {

       var saveType = eventArgs.getEventArgs().getSaveMode();

       if (saveType == 70)

       {

           //AutoSave

           var holding = Xrm.Page.getAttribute("new_holding").getValue();

           if (holding == "Walked")

           {

              eventArgs.getEventArgs().preventDefault();

           }        

       }

    }

    First, if you want to check if the save action is AutoSave by getting the SaveMode and comparing it to AutoSave. In case it is get the value of the holding attribute, and check if the value is "Walked". If it is call the preventDefault on the eventArgs.

    The below link might help you out:

    blogs.msdn.microsoft.com/.../how-to-manage-auto-save-for-individual-forms-of-entity

    Hope this helps.

  • JO-30091535-0 Profile Picture
    238 on at

    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();
           }        
    
       }
    
    }
  • Suggested answer
    Community Member Profile Picture
    on at

    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

  • JO-30091535-0 Profile Picture
    238 on at

    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
    gdas Profile Picture
    50,091 Moderator on at

    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.

  • JO-30091535-0 Profile Picture
    238 on at

    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
    50,091 Moderator on at

    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 .

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

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

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

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    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.

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans