locked
User feedback from prevalidate RRS feed

  • Question

  • If I have a prevalidate plugin which, amongst other things, will ensure that a newly created object is valid how can I get feedback from that plugin to the user if it isn't valid?

    So for example if I have "widget" entity and whether users can create widgets depends on some function in the plugin can I get a message back from the plugin to say, for example, insufficient "sprockets in stock to create widgets" or "Sprockets can not be created in requested date range". These are errors which are trivial to handle in a plugin but would be a real pain in the proverbials to handle in Javascript. Now in a web development environment I'd do that through a simple AJAX callback but how do I do that in CRM?

    Wednesday, August 14, 2013 2:14 PM

All replies

  • You aren't going to like the answer.  CRM does not give any way for plugins to communicate with the user beyond unhandled exceptions.  Sooooo, you can either show your user a message through a nasty exception window, or do your validation through javascript.  

    On a side note, could you possibly write a web service that can be called through SOAP?  That way you can write the web service in .NET and then use Javascript to call it and get the result.  That should make it less of a pain the proverbials ;)  You would have to host the web service elsewhere, though.

    Just a thought.  Good luck!

    Ray


    Wednesday, August 14, 2013 4:09 PM
  • Hi,

    Through Prevalidation Plugin you can throw an error message to the user after validating some condition in Plugin. You can handle it through JavaScript as well. Through JavaScript you can show alert to user and abort the save operation. But if widget will be created through some background process then that will not be validated through JavaScript. In such a case go with Plugin.

    Hope this helps!

    Thanks!

    Kalim Khan

    If this post helps you, please click "Mark As Answer" on the post and "Vote as Helpful"

    Wednesday, August 14, 2013 4:53 PM
  • Hi J N Brand, 

    we can validate it through Javascript which need to be fire on "On Save" event.

    Wednesday, August 14, 2013 6:14 PM
  • Hi,

    Through Prevalidation Plugin you can throw an error message to the user after validating some condition in Plugin. You can handle it through JavaScript as well. Through JavaScript you can show alert to user and abort the save operation. But if widget will be created through some background process then that will not be validated through JavaScript. In such a case go with Plugin.

    Hope this helps!

    Thanks!

    Kalim Khan

    If this post helps you, please click "Mark As Answer" on the post and "Vote as Helpful"

    That sounds like a possible solution. So how does one throw such an error in the plugin and how does it get handled in the client side javascript?

    Wednesday, August 14, 2013 7:14 PM
  • Hi,

    In Plugin you can throw Exception like given below. check your business logic as given below.

    if(Check your condition){

    throw new InvalidPluginExecutionException("Sprockets can not be created in requested date range");

    }

    If you are not familiar with Plugin then follow MSDN Step by step guide.

    http://msdn.microsoft.com/en-us/library/gg594416.aspx

    Same thing can be achieved through JavaScript code as given below. In this blog step has been provided. in Alert you have to pass your message.

    http://www.infinite-x.net/2011/05/06/crm-2011-canceling-the-save-operation/

    Hope this helps!

    If this post helps you, please click "Mark As Answer" on the post and "Vote as Helpful"


    • Edited by Kalim Khan Wednesday, August 14, 2013 7:48 PM
    • Proposed as answer by B sreenivasulu Thursday, August 15, 2013 2:25 PM
    Wednesday, August 14, 2013 7:33 PM
  • Hmm! well it sort of works.

    If I create a prevalidation stage plugin and throw the exception then it does come back to the user and appear as a meaningful error message. Unfortunately it appears as a raw http 500 error response in a popup which is crude, ugly and frankly so far from user friendly that it's positively user hostile. Of course the irony is that despite that it's better than anything that CRM itself provides!

    It also only works from prevalidate. So If I want the action to occur in post create, which is often the case, I have to split the error handling logic into a separate plug in from the non error handling case which is klunky to say the least.

    • Edited by J N Brand Wednesday, August 14, 2013 10:13 PM
    Wednesday, August 14, 2013 10:04 PM
  • Yup, that's the "nasty exception window" I was talking about.  Lovely isn't it?  Unfortunately, Microsoft expects us to be able to do all of our validation through javascript.  While usually possible, it's not always the ideal solution, as in your case.  What is your plugin doing that would be difficult in JavaScript?  Database checking or something like that?
    Wednesday, August 14, 2013 10:41 PM
  • You can throw the error at any point in a plugin (even in post-state plugins running on create) - when CRM encounters an exception it will roll back the entire transaction.

    Paul


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism

    Thursday, August 15, 2013 6:33 AM
  • Hi,

    Plugin's are server side and don't have a UI. if you don't like Error window from plugin and If you want to validate client side input by user and give feedback to the user then go with JavaScript as suggested by me.

    Through JavaScript we can not perform more complex work, In such a case plugin is the solution. in case of error Plugin will rollback the transaction even on post operation. If you are writing plugin for validation then go with Pre operation.

    Hope this helps!

    Thanks!

    If this post helps you, please click "Mark As Answer" on the post and "Vote as Helpful"
    Thursday, August 15, 2013 6:49 AM
  • Yup, that's the "nasty exception window" I was talking about.  Lovely isn't it?  Unfortunately, Microsoft expects us to be able to do all of our validation through javascript.  While usually possible, it's not always the ideal solution, as in your case.  What is your plugin doing that would be difficult in JavaScript?  Database checking or something like that?

    Well in this case it checks that an action performed by a user is unique within both the teams of which they are a member and a date range. At the server that's relatively trivial to test with a LINQ based query but to get all that data back to the client and then make the client understand what is going on is a lot of overhead not to mention that it duplicates code that will have to happen in a post create plugin anyway. So it's cumbersome and error prone.

    In a normal web development environment I would just make that an ajax call to some php, the php would validate the calling parameters and any other more complex conditions, if it passes it would run the code to make any database changes and then it would return a block of XML or JSON encoded data including something like "<ok>ok</ok>" or "<error>This is why it broke</error>". All handled in single code block. The client side jQuery would then popup a friendly style of message to tell the user that or highlight what they need to fix. The advantage is that a single chunk of server side php handles both error checking and database operations without needing to duplicate anything and without needing to send chunks of the database information to the client.

    I know raising an error, at any point in the plugin, will roll back the transaction. The issue is how to tell the user whether what they did worked or not.



    • Edited by J N Brand Thursday, August 15, 2013 11:00 AM
    Thursday, August 15, 2013 9:14 AM
  • In a normal web development environment I would just make that an ajax call to some php, the php would validate the calling parameters and any other more complex conditions, if it passes it would run the code to make any database changes and then it would return a block of XML or JSON encoded data including something like "<ok>ok</ok>" or "<error>This is why it broke</error>". All handled in single code block. The client side jQuery would then popup a friendly style of message to tell the user that or highlight what they need to fix. The advantage is that a single chunk of server side php handles both error checking and database operations without needing to duplicate anything and without needing to send chunks of the database information to the client.

    All of the above is still possible with CRM, so long as you have a PHP server to do the checking on (or you can host an ASP.NET page on the CRM server, which would be my recommendation).  AJAX works fine, as do JSON and JQuery.  I think that's the route you'll need to take to get the type of user friendly message you want.

    Good luck!

    Ray

    Thursday, August 15, 2013 2:13 PM
  • The Business Process Error that MSCRM displays when you throw new InvalidPluginExecutionException() is exactly how CRM displays error messages; and so it's providing the same user experience. You can pass in the message you want to show the user, so they know exactly what or why they're seeing the error. And if you need to provide more information about the error, or what needs to be done to get past the validation you can trace it so they can download the log for more information.


    What is so bad about this that makes you want to go against the MS way of doing it?

    Paul


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism

    Thursday, August 15, 2013 8:35 PM
  • Personally, I wouldn't prevent the record from being saved via a plugin as this could cause problems down the line (e.g. workflows or other dependencies). The approach I would take would be to have a Widget Status field.

    Then if the plugin says that the Widget can be created, the Status is set to true

    If the Widget can't be saved, then set the status to some error code. To increase the user experience you could try either of the following:

    - set up a workflow that emails people when a widget with an invalid status is created

    - customise the widget form so that you add a crm notification to the form if the widget is invalid

    As you've stated, the plugin exception route is really not user friendly and I think the above approach makes it a nicer user experience


    Friday, August 16, 2013 8:12 AM
  • That is not what our users see. Our users see a popup which is a raw http 500 error message. No log file, no OK button. If it did work then that would be marginally better but the standard MS error message is still one step from useless. Apart from any error message that I create it offers an XML log file. How many end users would understand or care about a log file in that format? In a real web development environment the response would be trapped in the client which would then highlight what the user should fix. Popups are a lazy solution but I would usually attach an error box to each input element and on getting an error response from ajax I would display and populate those error boxes. So in that example the input would show the error message would appear at the top of the page and the date fields would get an appropriate error message next to them.

    Just because microsoft can create a popup and dump XML files to the end user does not mean that is a good solution. It is barely a solution at all.

    Friday, August 16, 2013 9:22 AM