Skip to content

Michael Heydasch's picture

Beware the Third Parameter of MODIFYALL

The C/AL keyword MODIFYALL is tempting to use when a certain value must be changed for every record matching the current filtered recordset.   One may or may not be aware of the third optional parameter for this keyword:  TRUE/FALSE. FALSE is the default. If set to TRUE, the OnModify() trigger is executed when the record is modified.

Consider the following snippet of code:

SalesLine.SETRANGE("Document Type",SalesHeader."Document Type");

SalesLine.SETRANGE("Document No.",SalesHeader."No.");

SalesLine.SETRANGE(Type,SalesLine.Type::Item);

SalesLine.SETFILTER(Quantity,'<>0');

SalesLine.MODIFYALL("Qty. to Ship",0,TRUE);

Using the above snippet of code, the "Qty. to Ship" value will be changed for every sales line matching the filtered recordset. The third parameter, TRUE, indicates the system should execute the OnModify() trigger for every record when changing the value. What the command does NOT do is execute the OnValidate() trigger of the Qty. to Ship field.

The OnValidate() trigger of the Qty. to Ship field of the Sales Line table contains, at minimum, critical code affecting the "Qty. to Ship (Base)" field, without which data errors will be introduced into the database. If code is introduced after the above snippet to release the document and post, Codeunit 80 skips past this particular issue and posts the document. Strange data errors then result when any further action is taken on this document.

The code below is a far safer block of code to execute:

SalesLine.SETRANGE("Document Type",SalesHeader."Document Type");

SalesLine.SETRANGE("Document No.",SalesHeader."No.");

SalesLine.SETRANGE(Type,SalesLine.Type::Item);

SalesLine.SETFILTER(Quantity,'<>0');

IF SalesLine.FINDSET(TRUE) THEN

  REPEAT

    SalesLine.VALIDATE("Qty. to Ship",0);  

      // safe!  code executed to set Qty. to Ship (Base) also

    SalesLine.MODIFY(TRUE);                

      // execute the OnModify() trigger

  UNTIL SalesLine.NEXT = 0;

The key is knowing whether there is any code in the OnValidate() trigger of the field. If there is, it's better to be safe than sorry.

Blog Tags: 
Read ArcherPoint's Blog Follow us on Twitter Follow us on Facebook Follow us on LinkedIn Link to our RSS feed Join us on Google+ Watch us on YouTube
Get Help Now