Clearing dependent property(s) values

Clearing dependent property(s) values

Old forum URL: forums.lhotka.net/forums/t/10903.aspx


DancesWithBamboo posted on Monday, November 21, 2011

I've started using 4.2 and am not real familiar yet with the new business rules system.  I have a need to "erase" the values in 2 properties when another property changes because they no longer are valid.  Is this something that can/should be handled cleanly as a business rule?  

Does anyone have an example of setting a dependent property's value in a rule like this?

StefanCop replied on Monday, November 21, 2011

You cannot clear a property's value, such that FieldManager.FieldExists(p) returns false again. You only can set to a "null" value.  The post "Is there a way to undefine a field in the FieldManager" explains it.

I found this http://forums.lhotka.net/forums/p/10843/50598.aspx  a nice rule example.  The important thing is to add input and output properties in the constructor to the lists:

                InputProperties.Add(primaryProperty);
                InputProperties.Add(secondaryProperty);
                AffectedProperties.Add(outputProperty);          

and the in the Execute:

               var value1 = (decimal)context.InputPropertyValues[primaryProperty];
               var value2 = (string)context.InputPropertyValues[secondaryProperty];
               if (....)
                   context.AddOutValue(outputProperty, null );   // raise property changed

or

               LoadProperty(outputProperty, context.Target, null);  // load silently

Here (http://forums.lhotka.net/forums/p/10308/48295.aspx#48295) is are some facts of AddOutValue, AffectedProperties, and LoadProperty.

HTH

 

JonnyBee replied on Monday, November 21, 2011

You should also make sure to set

                CanRunAsAffectedProperty = false;
                CanRunInCheckRules = false;
                CanRunOnServer = false;

So that your rule will only run when either code or user sets the field value.

You cannot clear the entire field value so FieldManager.FieldExistes will return false again (as this would corrupt the Undo / CancelEdit function).

I also recommend the use of AddOutValue as this will notify the UI of changes to the data field.

 

Copyright (c) Marimer LLC