Description:
BISP is committed to provide BEST learning material to the beginners and advance
learners. In the same series, we have prepared a complete end-to end Hands-on
Beginners Guide for Microsoft Dynamics CRM. The document focuses on various
error handling techniques in Microsoft Dynamics CRM. Join our professional training
program and learn from experts.
History:
Version
0.1
0.1
Description Change
Initial Draft
Review#1
www.bispsolutions.com
Author
Chandra Prakash Sharma
Sumit Goyal
www.bisptrainigs.com
Publish Date
10th Aug 2014
10th Aug 2014
www.hyperionguru.com
Page 1
Contents
Handling unexpected user input ......................................................................................................................... 3
Handling unexpected processing ........................................................................................................................ 6
How to work Exception handling :............................................................................................................... 6
Blocking events .................................................................................................................................................. 11
Handling UI events ............................................................................................................................................ 12
Advanced error handling ................................................................................................................................... 13
www.bispsolutions.com
www.bisptrainigs.com
www.hyperionguru.com
Page 2
www.bispsolutions.com
www.bisptrainigs.com
www.hyperionguru.com
Page 3
www.bispsolutions.com
www.bisptrainigs.com
www.hyperionguru.com
Page 4
function CheckMobile()
{
var _userInput = Xrm.Page.getAttribute("bispdata_mobnumber").getValue();
var _isValid = false;
if(_userInput != null && _userInput != "")
{
if(_userInput.match(/^[789]\d{9}$/))
{
_isValid = true;
}}
if(_isValid == false)
{
// clear the field
_userInput = "";
Xrm.Page.getAttribute("bispdata_mobnumber").setValue(_userInput);
// alert
alert("Please Enter Write Mobile number...");
// set focus
Xrm.Page.getControl("bispdata_mobnumber").setFocus(true);
}
}
After enter correct mobile number, see result :
www.bispsolutions.com
www.bisptrainigs.com
www.hyperionguru.com
Page 5
www.bispsolutions.com
www.bisptrainigs.com
www.hyperionguru.com
Page 6
drag and drop this field on you form. then click on save and publish form.
www.bispsolutions.com
www.bisptrainigs.com
www.hyperionguru.com
Page 7
Step 2 : Then write JavaScript file for handle this exception and in web resource. and add this JavaScript in
form. then save and Publish after then click on Save and Close.
This JavaScript code :
function ErrorHandler()
{
var _error = Xrm.Page.getAttribute("bispdata_errors").getSelectedOption().text;
// alert("Selected option is: " + _error);
switch(_error)
{
case "URIError":
try
{
decodeURIComponent("%");
}
catch(err)
{
alert(err.name + " || " + err.message);
}
break;
case "RangeError":
var _age = 120;
try{
if(_age > 100){
throw new RangeError("Age cannot be over 100");
}}
catch(err)
{
alert(err.name + " || " + err.message);
}
break;
case "ReferenceError":
try
{ // use an undeclared variable
trying.thisone;
// TypeError due to browser
}
catch(err){
alert(err.name + " || " + err.message);
}
break;
case "EvalError":
// not used in recent versions, but supported
www.bispsolutions.com
www.bisptrainigs.com
www.hyperionguru.com
Page 8
try
{
var y = new eval();
// TypeError due to browser
}
catch(err){
alert(err.name + " || " + err.message);
}
break;
case "TypeError":
try{
var _obj = {};
// call undefined method
_obj.execute();
}
catch(err)
{
alert(err.name + " || " + err.message);
}
break;
case "SyntaxError":
try{
var _x = "some string";
var _y = 10;
var _total = eval(_x + _y);
}
catch(err){
alert(err.name + " || " + err.message);
}
break;
case "CustomError":
try{
throw new Error("Custom Error message");
}
catch(err){
alert(err.name + " || " + err.message);
}
break;
default:
// do nothing
}
}
www.bispsolutions.com
www.bisptrainigs.com
www.hyperionguru.com
Page 9
Step 3 : After then when you select error list any value you got the error message in popup window.
www.bispsolutions.com
www.bisptrainigs.com
www.hyperionguru.com
Page 10
Blocking events
One of the most common requirements that you will eventually encounter is to block a form from getting
saved if a certain condition is not met.
How to work Blocking events :
Step 1 : Go to Navigation bar and select your entity here go to form and add new field type "Two Options".
then drag and drop in your form then save and publish.
www.bispsolutions.com
www.bisptrainigs.com
www.hyperionguru.com
Page 11
Handling UI events
For the instances where you need to block the form from getting saved due to a user input error I have
mentioned briefly, it's always a good idea to return a message to the user and notify them of the reason for
such a decision.
How to work Handling UI events :
Step 1 : open your entity and create a new custom field type "Two Options" and create a new single line of
text field, drag and drop in form.
Step 2 : Add below JavaScript on Web resource and Set function " contactLoad " OnLoad and function "
message " set on FormSave event.
See below JavaScript file :
1. Contact.js
function contactLoad()
{
var _placeholder = document.getElementById("bispdata_placeholder");
_placeholder.style.display = "none";
}
2. ShowMessage.JS
function ShowMessage(){
var _placeholder = document.getElementById("bispdata_placeholder");
if(_placeholder != null){
var _newDiv = document.createElement("div");
//style='overflow-y:auto; height:80px; border:1px #6699cc solid; background-color:#ffffff;' />");
_newDiv.id = 'divMessage';
_newDiv.innerHTML = "<label style='font-family:arial;color:red;font-size:20px'>Field Message must be set to
No to be able to save the form!</label>";
_placeholder.style.display = "none";
var _previous = _placeholder.firstChild;
if(_previous == null){
if(_placeholder.childNodes.length == 0){
_placeholder.parentNode.appendChild(_newDiv);
}
Else {
_placeholder.insertBefore(_newDiv, _previous);
}
}
Else {
_placeholder.replaceChild(_newDiv, _previous);
}
}}
www.bispsolutions.com
www.bisptrainigs.com
www.hyperionguru.com
Page 12
3. Message.js
function message(context)
{
var _isMessage = Xrm.Page.getAttribute("bispdata_message").getValue();
if(_isMessage)
{
ShowMessage(); //call swos message function
}
}
After then Save and Publish and Save and Close.
www.bispsolutions.com
www.bisptrainigs.com
www.hyperionguru.com
Page 13
www.bispsolutions.com
www.bisptrainigs.com
www.hyperionguru.com
Page 14