Component Field Find & Replace

Sometimes you just want to find a field and replace its value no matter where it is in the Component.
class General;
   
   method ComponentFieldFindReplace(&rs As Rowset, &fieldName As string, &find As string, &replace As string)
end-class;



/* Searches *entire* Component Buffer for a given Field Name, and optional value, 
   and replaces with the given value. 

   &rs:        The RowSet that the search will begin at, and cascade down from. Generally use GetLevel0().
   &fieldName: The name of the Field whose value you want to replace.
   &find:      (optional) The value to match
   &replace:   The value to update the Field.Value with.

   Example: &clsGPUtilities.ComponentFieldFindReplace(GetLevel0(), "YOUR_FIELD", "", "X");
*/
method ComponentFieldFindReplace
   /+ &rs as Rowset, +/
   /+ &fieldName as String, +/
   /+ &find as String, +/
   /+ &replace as String +/
   
   Local integer &i, &r, &f, &c;
   
   For &i = 1 To &rs.RowCount
      
      For &r = 1 To &rs(&i).RecordCount
         For &f = 1 To &rs(&i).GetRecord(&r).FieldCount
            If &rs(&i).GetRecord(&r).GetField(&f).Name = &fieldName Then
               If &find = "" Or
                     &find = &rs(&i).GetRecord(&r).GetField(&f).Value Then
                  &rs(&i).GetRecord(&r).GetField(&f).Value = &replace;
               End-If;
            End-If;
         End-For;
      End-For;
      
      /* Do same for each child RowSet of this Row. */
      For &c = 1 To &rs(&i).ChildCount
         %This.ComponentFieldFindReplace(&rs(&i).GetRowset(&c), &fieldName, &find, &replace);
      End-For;
      
   End-For;
   
end-method;

0 comments: