Check the form has saved or not in CRM 2011 Javascript

14,159

When you say 'form has been saved' do you mean for the first time? If so you can query the form type:-

Xrm.Page.ui.getFormType();

(Is it in Create or Update for example). If the form is already in Update mode then you can check if the form is dirty as you say. If you want to know which mandatory fields have not been completed you can also potentially loop over the attributes on the form and query whether they are Business Required or not:-

Xrm.Page.data.entity.attributes.get("myAttribute").getRequiredLevel();

and add this to a warning message to the user.

Share:
14,159
Charan Raju C R
Author by

Charan Raju C R

Updated on August 04, 2022

Comments

  • Charan Raju C R
    Charan Raju C R almost 2 years

    I'm writing a Javascript to call external link on click of custom ribbon button in CRM 2011 entity form. In javascript I'm checking the form is dirty or not. If the form is dirty,(means some fields are modified by user) then JScript will save the form forcefully using Xrm.Page.data.entity.save(). But, when the mandatory fields have not filled, force save will not be happened and I have to show some custom message to fill those fields, terminate the flow of control and should not open the external link. How to get whether the form has saved or not..?

    Piece of code as below:

    function buttonOnClick() {
        if (Xrm.Page.data.entity.getIsDirty()) 
        {
            Xrm.Page.data.entity.save();
        }
        else 
        {
            window.open('http://www.google.com', 'name', 'width=900,height=800');
        }
    }
    
  • Charan Raju C R
    Charan Raju C R about 12 years
    This is great for creating new record. Whats the solution for update..?
  • Philip Rich
    Philip Rich about 12 years
    If the form is already in Update mode then you can check if the form is dirty -> That way you know if any fields have been changed. Then you can look for all fields which are mandatory using the code above and check those fields have a value. If they don't you could add the field name to a warning message telling the user than this would need to be completed? As the other user commented though it is essentially reinventing the OOTB functionality, but it can be done if like you say the standard event has been prevented.
  • Charan Raju C R
    Charan Raju C R about 12 years
    Then we should check each and every fields' required level and its contains value or not..? If its the case I have many fields and many forms.. Can we have any property to get all the fields on the form...?
  • Philip Rich
    Philip Rich about 12 years
    You can iterate over the attributes collection and only select those which meet your criteria. Xrm.Page.data.entity.attributes.forEach(function (attribute, index) { if (attribute.getRequiredLevel() == "required") { message += attribute.getName() + "\n"; } });