2

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
  • 758
  • 5
  • 21
  • 43
  • Have you tried this? I think CRM will do it for you and prevent the save, displaying the standard error about required fields not having been completed. – glosrob Apr 03 '12 at 12:05
  • Yes it will give a warning message but wont terminate the execution. We can manually terminate the execution if we will get to know the form has not saved. – Charan Raju C R Apr 03 '12 at 12:15
  • I am surprised that is ignores the required fields and saves anyway - wouldn't this would break the db integrity? Are they definitely 'required' and not 'recommended'? – glosrob Apr 03 '12 at 13:01

3 Answers3

3

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.

Philip Rich
  • 637
  • 1
  • 9
  • 20
  • This is great for creating new record. Whats the solution for update..? – Charan Raju C R Apr 03 '12 at 14:00
  • 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. – Philip Rich Apr 03 '12 at 14:15
  • 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...? – Charan Raju C R Apr 03 '12 at 15:04
  • 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"; } }); – Philip Rich Apr 03 '12 at 20:36
0

You could try this way:

var entitySaved;

function OnLoad(){
   entitySaved=false;
 }
function OnSave(){
   entitySaved=true;
 }
function myFunction(){
  if(entitySaved){
   //do your logic here
  }
}

Of course, you will have to add the form events from your CRM solution, by clicking in form properties.

Manuel Roldan
  • 487
  • 3
  • 12
0

You could add your own OnSave method to validate the fields and return a value based on whether they are valid or not.

e.g.

Xrm.Page.data.entity.addOnSave(function() {
    var isValid = VerifyOnSave();
    if (isValid) {
        //continue
    }
    else {
        //show errors, cancel save
    }
);

function VerifyOnSave()
{ 
    //<insert validation logic here>
    return true;
}

That doesn't explicitly tell you the form saved, but lets you know whether the form is valid, which may or may not be close enough.

glosrob
  • 6,631
  • 4
  • 43
  • 73