setValue()

Sets the value of the specified field.

FieldObject.setValue(Value)

Arguments

Value—new value for the field.

Exceptions

The following exceptions may be thrown:

Description

Note:  The Uniface JavaScript API does not handle data type and formatting conversion. You must ensure that the specified Value matches the format defined for the field, before it is submitted to the server.

In the browser, field values are treated as strings, regardless of the data type defined for the field in Uniface. JavaScript converts values into a type suitable for the current context. For example, using a mathematical operand on a variable automatically converts it to a number, thus if the variable vFld1 contains a string "1234.78", the following Javascript code converts it to a number:

vResult = vFld1*4

However, before the value of vResult can be assigned to a field, it must be converted back to a string. The setValue operation handles this automatically, but it does not format the string.

The format of the string must match the expected format in the field definition, otherwise a syntax or format exception may be reported the next time the data is submitted to the server. For example, if the field interface is defined as N10.2 (numeric, length 10, 2 decimal places), the string "12345.678923" will result in an error when the data is saved.

The setValue operation can change the values of fields that have the field syntax set to NED, such as StaticText fields. However, such values will not be accepted when the data is submitted to the server, because they cannot pass validation checks. In such cases, you should use the updatable version of the widget, in this case the StaticText_updatable widget.

If setValue is used assign an image to a Picture (or Picture_updatable) widget, the data type of the widget must be Image(url). If it is set to any other data type, such as String or Image (any source), the image cannot be resolved because the field value refers to a server resource, which is not accessible from the client.

Using setValue

The following example sets the value of the GREETING.ENT1 field to "Hello".

this.getEntity("ENT1").getOccurrence(0).getField("GREETING").setValue("Hello");

Getting and Setting Field Values

The following operation calculates the total of an order line by multiplying the QUANTITY by the UNIT_PRICE fields.

webtrigger OnChange                          
 javascript             
   var fld1, fld2, total, occ; 
   // ;'this' is always the current object--here, it is field QUANTITY
   // ;use methods of the object get field value and parent occurrence object
   fld1 = this.getValue();
   occ = this.getParent();

   //;using the occurrence object, get the value of another field in the occurrence
   fld2 = occ.getField("UNIT_PRICE").getValue();

   //;calculate the line total
   total = Number(fld1) * Number(fld2);  // ;explicitly treat field values as numbers
   total = Round(total,2);               // ;round to 2 decimal points, 
                                         // ;in case of rounding errors resulting in wrong format

   //;set the value of the LINE_TOTAL field, implicitly converting to String
   occ.getField("LINE_TOTAL").setValue(total); 
  
 endjavascript
end; onchange

Related Topics