Question

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');
    }
}
Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top