Best Practices - Controls on Updated Properties

The purpose of this document is to provide best practices for controls.

The reason for this rule is that you cannot assess in which order the properties have been entered and controlled. If you want to perform such controls immediately after data entry in order to enhance the user experience, you can add code to the UI layer attached to the representation. However:

Example: If you want to make sure that no duplicate values exist on the row of a grid, you can specify it in the global control code for the main class. One option would be to use the following code:

If uni(this.COLLECTION(1..maxtab(this.COLLECTION)).MYKEY)<>0# A duplicate key exists for the rowARET= Fmet this.ASETERROR .... # Place here the right error messageEnd ARETEndif

However, this code is flawed because the deletion of lines in a collection is managed logically by setting a value equal to [V]CST_ADEL or [V]CST_ANEWDEL in the ASTALIN property of the collection. This means that trying to insert a new key corresponding to a previously deleted value will also display the error.

The following code should be used instead:

Local Char KEY(20)(1..)Local Integer LINES, CURLIN, L_INDEX, I, J# Let's make a list of the existing codesFor CURLIN=1 to maxtab(this.COLLECTION)If this.COLLECTION(CURLIN)<>Null and find(this.COLLECTION(CURLIN).ASTALIN,[V]CST_ADEL,[V]CST_ANEWDEL)=0LINES+=1KEY(LINES)=this.COLLECTION(CURLIN).MYKEYL_INDEX(LINES)=CURLINEndifNext CURLIN# Now we can test the unicity of the codesJ=uni(KEY(1..LINES))If J<>0# A duplicate key exists for the rowI=find(KEY(J),KEY(1..J-1))# L_INDEX(I) is the first duplicate key position, and L_INDEX(J) the second positionARET= Fmet this.ASETERROR .... # Place here the right error messageEnd ARETEndif

You can write the following code in the script associated with the collection:

Local Char KEY(20)(1..)Local Integer LINES, CURLIN# Let's make a list of the existing codesFor CURLIN=1 to maxtab(this.APARENT.COLLECTION)If this.APARENT.COLLECTION(CURLIN)<>Null and find(this.APARENT.COLLECTION(CURLIN).ASTALIN,[V]CST_ADEL,[V]CST_ANEWDEL)=0LINES+=1KEY(LINES)=this.APARENT.COLLECTION(CURLIN).MYKEYEndifNext CURLINIf uni(KEY(1..LINES))<>0# A duplicate key exists for the rowARET= Fmet this.ASETERROR .... # Place here the right error messageEnd ARETEndif

However, this would be risky from a performance point of view because it would be repeated on every line in service mode.