Error Handling in DynamError Handling in Dynamics CRM Onlineics CRM Online

download Error Handling in DynamError Handling in Dynamics CRM Onlineics CRM Online

of 14

Transcript of Error Handling in DynamError Handling in Dynamics CRM Onlineics CRM Online

  • 7/26/2019 Error Handling in DynamError Handling in Dynamics CRM Onlineics CRM Online

    1/14

    www.bispsolutions.com www.bisptrainigs.com www.hyperionguru.com Page 1

    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 advancelearners. 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 Description Change Author Publish Date0.1 Initial Draft Chandra Prakash Sharma 10th Aug 20140.1 Review#1 Sumit Goyal 10th Aug 2014

    http://www.bispsolutions.com/http://www.bispsolutions.com/http://www.bisptrainigs.com/http://www.bisptrainigs.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.bisptrainigs.com/http://www.bispsolutions.com/
  • 7/26/2019 Error Handling in DynamError Handling in Dynamics CRM Onlineics CRM Online

    2/14

    www.bispsolutions.com www.bisptrainigs.com www.hyperionguru.com Page 2

    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

    http://www.bispsolutions.com/http://www.bispsolutions.com/http://www.bisptrainigs.com/http://www.bisptrainigs.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.bisptrainigs.com/http://www.bispsolutions.com/
  • 7/26/2019 Error Handling in DynamError Handling in Dynamics CRM Onlineics CRM Online

    3/14

    www.bispsolutions.com www.bisptrainigs.com www.hyperionguru.com Page 3

    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);

    }

    }

    http://www.bispsolutions.com/http://www.bispsolutions.com/http://www.bisptrainigs.com/http://www.bisptrainigs.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.bisptrainigs.com/http://www.bispsolutions.com/
  • 7/26/2019 Error Handling in DynamError Handling in Dynamics CRM Onlineics CRM Online

    4/14

    www.bispsolutions.com www.bisptrainigs.com www.hyperionguru.com Page 4

    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 focusXrm.Page.getControl("bispdata_mobnumber").setFocus(true);

    } }

    After insert correct value see the result :

    http://www.bispsolutions.com/http://www.bispsolutions.com/http://www.bisptrainigs.com/http://www.bisptrainigs.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.bisptrainigs.com/http://www.bispsolutions.com/
  • 7/26/2019 Error Handling in DynamError Handling in Dynamics CRM Onlineics CRM Online

    5/14

    www.bispsolutions.com www.bisptrainigs.com www.hyperionguru.com Page 5

    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 focusXrm.Page.getControl("bispdata_mobnumber").setFocus(true);

    }

    }

    After enter correct mobile number, see result :

    http://www.bispsolutions.com/http://www.bispsolutions.com/http://www.bisptrainigs.com/http://www.bisptrainigs.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.bisptrainigs.com/http://www.bispsolutions.com/
  • 7/26/2019 Error Handling in DynamError Handling in Dynamics CRM Onlineics CRM Online

    6/14

    www.bispsolutions.com www.bisptrainigs.com www.hyperionguru.com Page 6

    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 inDynamics 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.

    http://www.bispsolutions.com/http://www.bispsolutions.com/http://www.bisptrainigs.com/http://www.bisptrainigs.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.bisptrainigs.com/http://www.bispsolutions.com/
  • 7/26/2019 Error Handling in DynamError Handling in Dynamics CRM Onlineics CRM Online

    7/14

    www.bispsolutions.com www.bisptrainigs.com www.hyperionguru.com Page 7

    drag and dropthis field on you form. then click on save and publish form.

    http://www.bispsolutions.com/http://www.bispsolutions.com/http://www.bisptrainigs.com/http://www.bisptrainigs.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.bisptrainigs.com/http://www.bispsolutions.com/
  • 7/26/2019 Error Handling in DynamError Handling in Dynamics CRM Onlineics CRM Online

    8/14

    www.bispsolutions.com www.bisptrainigs.com www.hyperionguru.com Page 8

    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 newRangeError("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

    http://www.bispsolutions.com/http://www.bispsolutions.com/http://www.bisptrainigs.com/http://www.bisptrainigs.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.bisptrainigs.com/http://www.bispsolutions.com/
  • 7/26/2019 Error Handling in DynamError Handling in Dynamics CRM Onlineics CRM Online

    9/14

    www.bispsolutions.com www.bisptrainigs.com www.hyperionguru.com Page 9

    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

    }

    }

    http://www.bispsolutions.com/http://www.bispsolutions.com/http://www.bisptrainigs.com/http://www.bisptrainigs.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.bisptrainigs.com/http://www.bispsolutions.com/
  • 7/26/2019 Error Handling in DynamError Handling in Dynamics CRM Onlineics CRM Online

    10/14

    www.bispsolutions.com www.bisptrainigs.com www.hyperionguru.com Page 10

    Step 3 :After then when you select error list any value you got the error message in popup window.

    http://www.bispsolutions.com/http://www.bispsolutions.com/http://www.bisptrainigs.com/http://www.bisptrainigs.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.bisptrainigs.com/http://www.bispsolutions.com/
  • 7/26/2019 Error Handling in DynamError Handling in Dynamics CRM Onlineics CRM Online

    11/14

    www.bispsolutions.com www.bisptrainigs.com www.hyperionguru.com Page 11

    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.

    http://www.bispsolutions.com/http://www.bispsolutions.com/http://www.bisptrainigs.com/http://www.bisptrainigs.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.bisptrainigs.com/http://www.bispsolutions.com/
  • 7/26/2019 Error Handling in DynamError Handling in Dynamics CRM Onlineics CRM Online

    12/14

    www.bispsolutions.com www.bisptrainigs.com www.hyperionguru.com Page 12

    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 = "Field Message must be set to

    No to be able to save the form!";

    _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);

    }

    } }

    http://www.bispsolutions.com/http://www.bispsolutions.com/http://www.bisptrainigs.com/http://www.bisptrainigs.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.bisptrainigs.com/http://www.bispsolutions.com/
  • 7/26/2019 Error Handling in DynamError Handling in Dynamics CRM Onlineics CRM Online

    13/14

    www.bispsolutions.com www.bisptrainigs.com www.hyperionguru.com Page 13

    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.

    http://www.bispsolutions.com/http://www.bispsolutions.com/http://www.bisptrainigs.com/http://www.bisptrainigs.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.bisptrainigs.com/http://www.bispsolutions.com/
  • 7/26/2019 Error Handling in DynamError Handling in Dynamics CRM Onlineics CRM Online

    14/14

    www.bispsolutions.com www.bisptrainigs.com www.hyperionguru.com Page 14

    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.

    http://www.bispsolutions.com/http://www.bispsolutions.com/http://www.bisptrainigs.com/http://www.bisptrainigs.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.hyperionguru.com/http://www.bisptrainigs.com/http://www.bispsolutions.com/