Dynamic Surveys in Microsoft Dynamics, Part III: The ViewModel

JE

Josh Elster

11/11/2015

This is part III in a series of posts about using the InRule BRMS along with Microsoft Dynamics CRM to create a dynamic, guided survey experience. This post assumes a working familiarity with JavaScript, the KnockoutJS MVVM framework, and a little bit of Dynamics CRM. Just tuning in? Check out the other posts in this series for more background:

Part I: The business use case for dynamic surveys

Part II: Managing the client-side dependencies for the survey

Part III: Structuring the view model (this post)

There’s so much material to cover that I can’t possibly get into the amount of detail that I’d like to without writing a hefty book’s worth of content. As a result, I’ve tried to focus on the aspects that I’ve found the most challenging or obscure as I went through the development process. Your feedback is appreciated!

Defining a ViewModel (for context)

A ViewModel is typically declared as a JavaScript Constructor function. In my script resource, I begin by declaring and creating a function. The name of the function is upper-case because in JavaScript the convention is for constructor functions to begin with an upper-case letter; all other functions should be lower-cased. The self alias variable is used to provide a consistent reference to the ViewModel, regardless of what the current definition of this is.

Notice how properties are declared not just for the data elements that we want rendered, but also for metadata that describes various aspects of the UI itself. For example, the value of isLoading is used to determine whether or not an indeterminate progress spinner should be displayed to indicate a long-running operation, while the questionIndex property stores the position of the currently active question within the greater list of questions.

There’s a lot of interesting things going on in the ViewModel, but in the interest of brevity I’d like to highlight the lastRuleResponse property. This observable field holds whatever response was last received from the rule engine (as expressed via a custom action and plugin – all part of the InRule ROAD team’s CRM Integration Framework.This allows me to set up a subscription to that property that notifies me when rules have been applied and new information may be available from the server.

The code above creates a subscription that extracts any notifications, validations, or warnings from the response. These are passed to other components for specific handling. The penultimate line extracts and sets the survey’s ID (because rules may need to be ran initially to assign/create the survey instance), while the last line kicks off a request to retrieve a list of questions from the remote server.

The getNotificationsFromRules and related functions don’t have their code listed here because they are simple mapping functions that exist to safely extract and shape data – nothing very interesting going on in those!

Something that *is* interesting is this line: Mscrm.Utilities.setReturnValue(v); this call passes data back to the survey launcher which allows the hosting form to react to changes in the survey – see The Launcher section from the previous post in this series for more on how that works.

Loading Questions

The survey code uses the Dynamics JavaScript SDK+SOAP endpoint to query the server for question data, but it could just as easily use the OData or the new WebAPI endpoints that are available in newer versions of Dynamics CRM. All the code needs to do is issue a query that returns any question associated with a given survey instance. Not having to write a whole bunch of data access code is part of what gives the dynamic survey such flexibility. The loadQuestionsFromCrm method looks a bit funky because it defines success and error callbacks at the top of the method rather than inline or at the bottom of the function, so it may be easier to see what’s going on by starting with the actual AJAX request to the server.

The loadSurveyQuestionsUsingLinks method is what I call a “garbage pail method” because it hides a lot of smelly, ugliness under a nice clean lid. The smelliness has to do with the fact that the shape of the data coming from Dynamics doesn’t match the shape needed by the ViewModel. Rather than forcing the ViewModel to understand how Dynamics shapes its data, this function serves the purpose of insulating the rest of the components from having that knowledge. Using a single function as a gatekeeper/translator simplifies the rest of my code immensely. To help accomplish this, I’m using Underscore (http://underscorejs.org), a JavaScript utility framework well worth checking out.

After the “garbage pail” of the loadSurveyQuestionsUsingLinks function, the rest of the question loading process should be a breeze! The success callback is fired and the list of questions is iterated through and used to instantiate new IrSurveyQuestionViewModel instances as part of the map expression. The resulting array is then sorted to arrange questions oldest to newest, existing questions are cleared, and new ones pushed onto the questionList. The last thing that the success handler does is tell the ViewModel to set the current question to be the first question which does not have a value for its answer property. From there, Knockout manages the rest of the process of updating the proper DOM elements and notifying other subscribers of the new data.

Conclusion

I hope that this post has provided you with a basic idea of the challenges and concerns surrounding the implementation and design of the Survey’s object model. I’ve tried to focus on the things that I think can trip up the unsuspecting developer, but doubtless I’ve left some gaps. Is there too much detail? Not enough detail? Let me know in the comments. I’m currently indecisive between wanting to describe how the HTML of the UI gets rendered and going over how the Rule Application is constructed, so if there’s something you’d like to see definitely chime in!

InRule for JavaScript | inrule said: "[…] couple of weeks ago we announced availability of InRule® for JavaScript through an early adopter program. To our knowledge this is […]".
Dynamic Surveys in Dynamics CRM Part II: Managing Dependencies | inrule said: "[…] this post makes extensive references to my preceding post, Surveys in Dynamics CRM Part I: Don’t Be a Monkey With Your Survey. If you are the type of person who likes to get the full story, you should go back and read the […]".
Josh Elster said: "In the time since this post was published, I've added (via a PR) full Windows Containers example code, DOCKERFILEs and scripts. They share common ancestry with the GH Gists I posted earlier, but are more up-to-date and tested. Here's a direct link to that sub-folder: https://github.com/InRule/Samples/tree/master/Developer%20Samples/WindowsContainers Now that our Samples repository is public, feel free to file Issues if you have problems or features you'd like to see implemented. If you have something you'd like to contribute, then by all means do so! The contributor guidelines and process are listed in the root of the repository. Enjoy! I'll update the body of the post as well with this link.".