locked
CRM 2011 - How to propagate the plug-in internal message as JavaScript alert? RRS feed

  • Question

  • Hello Everyone,

    I have registered a plug-in on post event of custom entity. On a ribbon button click i am updating one of the attributes value to trigger this plug-in. Inside plug-in code in the catch block i have written some InvalidPluginExcecutionException messages. So, whenever there is any failure inside plug-in code it should alert that message to the user. As i am initiating the plug-in from JavaScript i am unable to propagate the plug-in message as JavaScript alert to the user.

    Does anybody know how to implement this?


    Thanks, Ankit Shah


    Inkey Solutions, India.
    Microsoft Certified Business Management Solutions Professionals
    http://www.inkeysolutions.com/MicrosoftDynamicsCRM.html

    Monday, October 15, 2012 1:08 PM

Answers

  • Hello Ankit,

    maybe I misunderstood, but aren't you using JS to update a field of the entity using the ribbon button? Something like this?

     $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: serverUrl + ODATA_ENDPOINT + myQuery,
            beforeSend: function(XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
            },
            success: function(data, textStatus, XmlHttpRequest) {
                successCallBack(data.d, textStatus, XmlHttpRequest);
            },
            error: function(XmlHttpRequest, textStatus, errorThrown) {
                errorHandler(XmlHttpRequest, textStatus, errorThrown);
            }
        });

    if yes, then you can do this in your error callback to show your message:

    function errorHandler(xmlHttpRequest, textStatus, errorThrown) {
        alert(JSON.parse(xmlHttpRequest.responseText).error.message.value);
    }

    This message will contain the message from the plugin exception.

    Hope I understood your requirement right.

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.
    View my latest gallery contribution here.
    Visit my blog here.


    Monday, October 15, 2012 2:03 PM

All replies

  • Hello,

    From my point of view the best way is to create some fake attribute and store message that should appear inside this attribute. Once record is saved and plugin is triggered you can retrieve this value using Rest EndPoint.


    Freelance Developer for Dynamics CRM 4.0/2011

    Monday, October 15, 2012 1:13 PM
    Moderator
  • Hi,

    when issuing a request to OData to trigger the plugin, you can view the error in Fiddler. I tried this out and this is what I got in the error:

    {
    "error": {
    "code": "-2147220891", "message": {
    "lang": "de-DE", "value": "test"
    }
    }
    }

    In my plugin I did this:

    throw new InvalidPluginExecutionException("test");

    So you can see the error message in the value property of the error in the response as shown above.

    Greetings,
    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.
    View my latest gallery contribution here.
    Visit my blog here.

    Monday, October 15, 2012 1:15 PM
  • Hi Andrii,

    Thank you for the quick reply.

    But suppose, i having multiple InvalidPluginExcecutionException messages in my plug-in then how could i identify the specific one in JavaScript code?


    Thanks, Ankit Shah


    Inkey Solutions, India.
    Microsoft Certified Business Management Solutions Professionals
    http://www.inkeysolutions.com/MicrosoftDynamicsCRM.html

    Monday, October 15, 2012 1:20 PM
  • Hi Andrii,

    Thank you for the quick reply.

    But suppose, i having multiple InvalidPluginExcecutionException messages in my plug-in then how could i identify the specific one in JavaScript code?


    Thanks, Ankit Shah


    Inkey Solutions, India.
    Microsoft Certified Business Management Solutions Professionals
    http://www.inkeysolutions.com/MicrosoftDynamicsCRM.html

    Pseudocode:

    instead of

    throw new InvalidPluginException("test exception");

    you can use

    Update Entity.Field = "test exception";
    return;
    So one exception equals to one message. Not sure how you wanted to use multiple exceptions. Could you in-light me how it could be used?


    Freelance Developer for Dynamics CRM 4.0/2011

    Monday, October 15, 2012 1:26 PM
    Moderator
  • @ Andrii - suppose i am performing two actions in my plug-in 1. retrieve accounts 2. update contacts. Now i have two try catch blocks for both these actions. How can i identify to show "accouts retreival failure" or "update contacts failure" alert?

    @ Pavlos - yes you are right, even i could also see the same but that just help us to resolve plug-in errors. My concern is to show that error as an alert message via JavaScript.


    Thanks, Ankit Shah


    Inkey Solutions, India.
    Microsoft Certified Business Management Solutions Professionals
    http://www.inkeysolutions.com/MicrosoftDynamicsCRM.html


    Monday, October 15, 2012 1:40 PM
  • Ok. Again pseudocode because I don't to write real code for this easy task...

    try
    {
    	//Retreive accounts code
    }
    catch(Exception e)
    {
    	Update entity field to "1. error during retrieval of accounts" + e.Message;
    	return;
    }
    
    try
    {
    	//update contacts code
    }
    catch(Exception e)
    {
    	Update entity field to "2. error during update of contacts" + e.Message;
    	return;
    }
    

    In JS I believe you can get first char of string. In case it would be 1 - Exception occurred during retrieval of accounts. Otherwise in case it would be 2 (you would not guess) - Exception occurred during update of contacts.

    Freelance Developer for Dynamics CRM 4.0/2011

    Monday, October 15, 2012 1:50 PM
    Moderator
  • Hello Andrii

    I completely understood you while you specified to update the entity field from plug-in. Actually the concern was JavaScript code

    Fine, let me see inside JavaScript code how would i can manage & identify the plug-in message to propopagate.

    thanks again for sharing workaround.


    Thanks, Ankit Shah


    Inkey Solutions, India.
    Microsoft Certified Business Management Solutions Professionals
    http://www.inkeysolutions.com/MicrosoftDynamicsCRM.html


    Monday, October 15, 2012 1:57 PM
  • Hello Ankit,

    maybe I misunderstood, but aren't you using JS to update a field of the entity using the ribbon button? Something like this?

     $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: serverUrl + ODATA_ENDPOINT + myQuery,
            beforeSend: function(XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
            },
            success: function(data, textStatus, XmlHttpRequest) {
                successCallBack(data.d, textStatus, XmlHttpRequest);
            },
            error: function(XmlHttpRequest, textStatus, errorThrown) {
                errorHandler(XmlHttpRequest, textStatus, errorThrown);
            }
        });

    if yes, then you can do this in your error callback to show your message:

    function errorHandler(xmlHttpRequest, textStatus, errorThrown) {
        alert(JSON.parse(xmlHttpRequest.responseText).error.message.value);
    }

    This message will contain the message from the plugin exception.

    Hope I understood your requirement right.

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.
    View my latest gallery contribution here.
    Visit my blog here.


    Monday, October 15, 2012 2:03 PM
  • Hello Pavlos,

    This is what exactly i am looking for.

    Thank you for sharing the details. will validate the same tomorrow morning... & let you know my feedback

    thanks again for sharing the code.


    Thanks, Ankit Shah


    Inkey Solutions, India.
    Microsoft Certified Business Management Solutions Professionals
    http://www.inkeysolutions.com/MicrosoftDynamicsCRM.html


    Monday, October 15, 2012 2:06 PM