DQ Issues powered by formscripting

I had an interesting discussion with a customer lately, which triggered a discussion on dataquality with a friend of mine:

How do you ensure dataquality? Are you aware of some subtle issues on dataquality when using formscripting?

What is the challenge? The customer had a request, that an entity should carry an account, something like a budget and sometimes a person who could help influencing the decisionmakers. This person is not required below a certain amount of budget, but required, when the budget is above.
So, for demopurposes, i created an entity with an account, a budget (attributename clz_budget) and an influencer (clz_decisiondriverid), which is a reference to a contact. Additionally, i added a changeevent to the budgetattribute on the form: when teh budget is above 1000, the influencer is mandantory, else use the formdefault for the requiredlevel.

So, the form looks like this:

and the formcode is something like that:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

functionform_onload(){

varREQUIREDLEVEL={// just a shortcut to the requiredlevels...

NONE:"none"

,RECOMMENDED:"recommended"

,REQUIRED:"required"

};

varBUDGETNAME="clz_budget";// the attribute holding the budget

// the attribute referncing the possible decisiondriver

varDECISIONDRIVERNAME="clz_decisiondriverid";

// the transitionvalue, from where a deciondriver is required

varVALUEFROMWHEREDECISIONDRIVERISNEEDED=1000;

// get a shortcut to the attributes...

varXPDEA=Xrm.Page.data.entity.attributes;

// get the budgetattribute

varbudget=XPDEA.get(BUDGETNAME);

// get the decisiondriverattribute

vardecisiondriver=XPDEA.get(DECISIONDRIVERNAME);

if(budget&amp;&amp;decisiondriver){// make sure, that both are present

// get the default depending on the customization

vardefaultrequiredlevel=decisiondriver.getRequiredLevel();

// code the logic for the requiredlevel for the decisiondriver

// depending on the budget

budget.addOnChange(

// this closures somehow some values from outside

// (especially budget and decisiondriver and defaultrequiredlevel)

function(){

varrequiredlevel=defaultrequiredlevel;

varthisbudget=budget.getValue();

// look for the transition

if(thisbudget&gt;VALUEFROMWHEREDECISIONDRIVERISNEEDED){

requiredlevel=REQUIREDLEVEL.REQUIRED;

}

decisiondriver.setRequiredLevel(requiredlevel);

}

);

// initial fill of the requiredlevel dpending on the budgetvalue...

budget.fireOnChange();

}

}

Everything is fine on the form, as you can see. If the budgetvalue is above 1000, the decisioninfluencer is mandantory. Now the problem: if you leave the budgetvalue below 1000, save the data and let the budgetvalue be set by a workflow to 2000, the rule of the mandantory decisioninfluencer will be disobeyed and you loose dataquality.
see here before and after workflow:

Even worse: if you open the record with the budgetvalue of 2000, the decisioninfluencer is marked as mandantory and you are able to close the form without a hint (because no data was changed…)
So, where does the problem reside: the problem is caused by programatically setting the requiredlevel. You could introduce a plugin, which prevents that situation, but the message from the plugin will will rise on a strange moment while saving the form or nearly nothing will happen in case of the execution of the workflow. And what happens, if you think you have to change the threshold from 1000 to 500 by changing the script?

This is definitely a scenario, where you should carefully thing about when designing your solution. I will keep it in mind and think about new strategies to deal with that kind of problems.