Hi I have a field in a table that has a tablerelation to another table. Is there a way to override or swith off this relation with c/al code ? I want to temporarily disable this relation at runtime. Thanks
You cannot just override a tablerelation. You can however make it conditional. If you make it conditional to a new boolean field in your table. You can swich this boolean on/off and this way change the tablerelation. An example of conditional tablerelations is in table 37 and 39. Look at the field “No.”
I want to temporarily disable this relation at runtime.
Originally posted by gazpacho - 2005 Oct 03 : 15:11:27
What do you mean by this? I guess when you are not running it, then the relationship is not tested anyway?[?]
You can’t override or turn off a table relationship, it would be a bad thing for any database management system if that were possible. It is, however, possible to enter invalid values into fields with relationships. Usually, you would VALIDATE a value into a field that has a relationship, to make sure that the value is a valid value. You can, however, enter an invalid value through code, by simply assigning it the value. Then, by calling the MODIFY method either without a parameter or with the parameter FALSE (so that would be MODIFY(FALSE)), you save the record with the invalid value to the database. For instance, let’s say you have a sales line in a variable, you can do the following: SalesLine.“No.” := ‘BADITEMNUMBER’; SalesLine.MODIFY(FALSE); This will save the line with the bad item number. Note that the posting routine will catch this value though, and it will not post. It is always better to VALIDATE the value and catch wrong data as soon as you can, like this: SalesLine.VALIDATE(“No.”,‘BADITEMNUMBER’); This will cause an error (assuming there is no Item called ‘BADITEMNUMBER’), as it should. The best way to go around the error is to do something like this: IF Item.GET(‘SOMEITEMNUMBER’) THEN SalesLine.VALIDATE(“No.”,Item.“No.”); and then you can decide what to do when the Item doesn’t exist. For the record, so I don’t get flamed… You need to know that I do not approve of this, but I think every Navision developer should know how to do it, so they can recognize it in code.
Hi, This can be achieved through C/AL code by setting the Property ValidateTableRelation of the field to No. In the OnValidate trigger of the field you then put code like this
IF NOT SkipValidateRelation THEN IF NOT MyRelatedTable.GET(MyCode) THEN ERROR( %1 %2 %3 does not exist.', MyRelatedTable.TABLECAPTION, FIELDCAPTION(MyCode), MyCode);and you can then switch off the validation of the relation by setting the boolean variable SkipValidateRelation to TRUE. This is definititely not recommended practice, but in some in cases it’s acceptable. For instance - in standard Navision - the “Post Code” field in the Customer Table and the “Vendor No.” field in the Item Table has the property ValidateTableRelation set to No. BTW
Usually, you would VALIDATE a value into a field that has a relationship, to make sure that the value is a valid value. You can, however, enter an invalid value through code, by simply assigning it the value. Then, by calling the MODIFY method either without a parameter or with the parameter FALSE (so that would be MODIFY(FALSE)), you save the record with the invalid value to the database.
An assignment will also be accepted by MODIFY(TRUE) (unless the modify trigger calls the validate trigger of the field).
Can you give an example under what circumstances you want to switch off the table relation ? Do you want it to be switched of for all records in that table or just for specific ?