Anda di halaman 1dari 14

Getting Started with MS Dynamics CRM

Error Handling in Dynamics CRM Online

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

Handling unexpected user input


In this recipe we will be looking at how to handle unexpected user input. While this is already in place for
some specific field types, we can easily enhance the system functionality through very simple validation
scripts and user feedback messages.
Below see Example :
Ex : Name first letter must be Capital.

Below code .js file :


function CheckUserInput() {
var _userInput = Xrm.Page.getAttribute("bispdata_name").
getValue();
var _isValid = false;
if(_userInput != null && _userInput != ""){
if(_userInput.match(/^[A-Z][a-z]+$/)){
_isValid = true;
}
}
if(_isValid == false)
{
// clear the field
_userInput = "";
Xrm.Page.getAttribute("bispdata_name").
setValue(_userInput);
// alert
alert("First Later should be Capital");
// set focus
Xrm.Page.getControl("bispdata_name").setFocus(true);
}
}

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page 3

EX : Mobile Number must be 10 digit.

Below mobile validation code:


function CheckMobile() {
var _userInput = Xrm.Page.getAttribute("bispdata_mobnumber").getValue();
var _isValid = false;
if(_userInput != null && _userInput != ""){
if(_userInput.match(/^\d{10}$/)){
_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 insert correct value see the result :

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page 4

Ex: Mobile number should be numeric and start with 7,8,9.

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

Handling unexpected processing


While this recipe will most likely not introduce you to anything specific to Dynamics CRM, the error-handling
procedures described here are specific to the JavaScript language and can easily be applied in the context of
Dynamics CRM.
Exception handling is the process of responding to the occurrence, during computation, of exceptions anomalous or exceptional conditions requiring special processing - often changing the normal flow of
program execution. It is provided by specialized programming language constructs or computer hardware
mechanisms.
JavaScript defines six standard and one custom error type to allow you to throw your own customized
exceptions :
Error
Details
URIError
This error occurs while encoding or decoding an URI. It is not a common occurrence in
Dynamics CRM.
RangeError
This error occurs when the number is out of range. It has a quite common occurrence
when performing calculations, especially when overriding the standard taxation in
Dynamics CRM.
ReferenceError This error is caused by an illegal reference; this is returned in Internet Explorer as a
TypeError.
EvalError
This error can be generated while using the eval() function. This is also returned in
Internet Explorer as a TypeError.
TypeError
This error is not as common anymore when defining most of your variables as var, but you
will encounter this in the case of incorrect casting.
SyntaxError
Working in conjunction with EvalError, this error is thrown when there is a syntax error
within the eval() function. This does not catch the standard syntax errors.
you can use Try Catch for Handling unexpected processing below you can see syntax.
try {
//your code here..
}
catch(Exception er){
//print here exception
}

How to work Exception handling :


Step 1 : Open any entity and add new field .
this field type is option set and add some value see below.
after then click on save and close button.

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.

Step 2 : Add a new web resource named JavaScript Event Blocking.


function BlockSave(context)
{
var _canSave = Xrm.Page.getAttribute("bispdata_save").getSelectedOption().text;
if(_canSave == "No")
{
Xrm.Page.context.getEventArgs().preventDefault();
}
}
Open a contact or create a new one. Set the Can Save field to No, and click on Save or Save and Close. Your
form will not get saved anymore.
Return the Can Save to Yes, and click on Save or Save and Close. Now your form gets saved as expected.

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.

Advanced error handling


MS Dynamics CRM Using JavaScript your options revolve around using the try and catch block in a creative
way. In standard catch block has some additional features you should be taking advantage of when needed.
How to work advance error handling :
Step 1 : Go to navigation bar and open your custom object. and add here new field, field type is Two
Options, then add in your form, then save and publish the form.

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page 13

Step 2 : Create a new JScript and add in web resource.


then call this JScript OnChange event on Error Handling field.
if filed=="No", this Jscript work and show error message.

www.bispsolutions.com

www.bisptrainigs.com

www.hyperionguru.com

Page 14

Anda mungkin juga menyukai