10,730
edits
(→OnSave) |
|||
Line 500: | Line 500: | ||
Another equivalent of a Woodford rule in JavaScript is the OnSave. Similar to Woodford’s OnSave rule, the JavaScript Bridge’s onSave callback is executed when the form is being saved. However, due to the asynchronous nature of many calls, you might want to call from the onSave handler (like Fetch for getting the records for validation). We have introduced a special mechanism for the onSave callback which enables control over the save validation execution. | Another equivalent of a Woodford rule in JavaScript is the OnSave. Similar to Woodford’s OnSave rule, the JavaScript Bridge’s onSave callback is executed when the form is being saved. However, due to the asynchronous nature of many calls, you might want to call from the onSave handler (like Fetch for getting the records for validation). We have introduced a special mechanism for the onSave callback which enables control over the save validation execution. | ||
In the example below, we have created a validation function | In the example below, we have created a validation function that checks if the email matches the regular expression during the Save process. Again, please note that if you use event handler like OnSave or OnChange, it is necessary to disable '''Delay load''', so the handlers are registered immediately when the form is opened, and not only when the user switches the tab to the iframe. | ||
You also need to call your function while loading the offline HTML page, so put window.onload = name_of_function(); in your script. | |||
Once you have the onSave handler in place, you need to understand the mechanism behind the save validation execution. As soon as the onSave callback returns (true or false) the save execution continues and the record is saved (or not). However, imagine a scenario, where you need to use fetch to get some data to perform the validation in onSave. This would include a call for FetchXml Execute and this function requires another callback. But the code in onSave will not wait until the callback is executed and the data is available. It will inevitably run into the return and end the execution! | |||
For this problem – when you want to call another asynchronous method in the onSave callback, we have introduced the following mechanism. You can call suspendSave before the async method of your choice to stop the save execution. This will wait until you call resumeSave from any other callback or method. The example below should clarify this better: | |||
<syntaxhighlight lang='js'> | |||
function SavingValidation() | |||
{ | |||
MobileCRM.UI.EntityForm.onSave( | |||
function (entityForm) | |||
{ | |||
///<param name="entityForm" type="MobileCRM.UI.EntityForm"></param> | |||
var emailDetail = entityForm.getDetailView("General"); | |||
var emailItem = emailDetail.getItemByName("emailaddress1"); | |||
if (!emailItem.value || emailItem.value.length == 0) | |||
entityForm.cancelValidation("Email Is empty."); | |||
else if(validateEmail(emailItem.value) == null) | |||
entityForm.cancelValidation("Email is in incorrect format \n\n Correct format : e.g. anything@whatever.info"); | |||
else | |||
{ | |||
// Return true to apply all changes | |||
return true; | |||
} | |||
// Return false to ignore all changes | |||
//return false | |||
}, | |||
true, null); | |||
} | |||
function validateEmail(email) | |||
{ | |||
var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; | |||
return email.match(mailformat); | |||
} | |||
</syntaxhighlight> | |||
In the first step, we check if the field with email address contains any character and if it exists. If not, we use the property method cancel validation to stop onSave validation and display an error message. | |||
<syntaxhighlight lang='js'> | <syntaxhighlight lang='js'> | ||
if (!emailItem.value || emailItem.value.length == 0) | |||
entityForm.cancelValidation("Email Is empty."); | |||
</syntaxhighlight> | </syntaxhighlight> | ||
We use this method also when our email doesn’t match the regular expression to display the message in a correct email format. In the situation when email matches to regular expression, we return true, to apply all changes and close the form. | |||
=== Form commands === | |||
So far, we have examined how to access the form and react to the change on an item. Now, let’s take a look at another aspect of the form – the commands (actions). In this section, we show you how to access commands from JavaScript, but also how to create a command in Woodford, while this is a necessary part of the process. The example in the next section shows how to create the commands from JavaScript directly. | |||
Commands can be very useful and can enable certain actions throughout the whole form, on all detail tabs. The main principle is to create a command in Woodford but to define and bind the logic in JavaScript. | |||
<syntaxhighlight lang='js'> | <syntaxhighlight lang='js'> |