With the arrival of Dynamic Scripting in DRM 11.1.2.3, JavaScript is a valid scripting option within DRM. This is pretty neat because a script provides a more robust alternative to traditional DRM formulas. I also like the fact that you can now put comments inside your code. I thought it would be nice to do a simple use case of this technique. In this case, I thought we would create a simple validation with Dynamic Scripting logic.
But first, I suppose we need some suitable dynamic scripting music.
The validation I have in mind is a simple one…check whether the number of characters in a description exceeds 80. On a side note, I was wondering why there is usually an 80 character limit on coding width standards? Apparently this hearkens back to the IBM punch card days.
I guess I could create a simple one like this without the use of dynamic scripting:
Well that’s efficient enough…but what if I wanted to extend this to also validate whether the user forgot to add a description? Now, we either need another validation or a property or a query that would check whether:
- The description is empty?
- The length of the description exceeds 80 characters?
Enter dynamic scripting.
Firstly, I’ll create a validation with the class, Script. Now, let’s declare a couple of variables (Notice how existing properties can be called from within variables?):
1 2 3 4 |
// Variable to store the value of the error var error; // Variable to store the DRM description var d = node.PropValue("Core.Descr") |
Now, we add some logic to check whether the description is greater than 80 characters:
1 2 3 |
if(d.length > 80) error="Description should be less than 80 characters…and no references to the Sound of Music, please"; print (error); |
The “print” function is useful because it allows you to do some troubleshooting within the scope of the validation. Now we at least know that our code is working.
And if we need to check another condition? Very simple and very similar to any other scripting solution you’ve use in the past.
1 2 3 |
// Notice that you don’t need to check if value is null else if (!d) errm="Description cannot be empty"; |
Finally, we’ll add one last snippet of code.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// Return error values if(!error) // Validation was successful. return true; else print (error); return{ // Validation was unsuccessful. success:false, // Return error messages to the screen. parameters:[error] } |
And a quick check whether our syntax is accurate:
Great, so now that all of that works, there is one last piece to the puzzle before we add this validation to a hierarchy. How do we set up a failure message so that we can get the parameterized messages to the console? That’s pretty simple too. All you need to do is put the following code in the Failure Message box.
Now, we’ll attach the validation to a hierarchy and run the validation.
Let’s check the messages that popped up.
So as you can see, dynamic scripting is a powerful new addition to the DRM stack. There are so many uses one can think of for it. There will be other posts devoted to dynamic scripting in the future, so keep checking in from time to time.
Thank you very much Vijay, the customised message for validation failure was causing me headaches.
Nice post Vijay.. The good thing is you are sharing interesting things.. eagerly waiting for your future posts regarding java script.