Code Data Provider

Code Data Provider

General Info

This Code Data Provider enables you to write javascript code in the very same way you would execute a native Javascript function: after calculating the new value, you should use a “return” statement in order to specify the final value to be set to the control/property. 

The "form" special variable is available in order to provide access to the parent form control/object, its methods and all included controls. 

In case your code contains asynchronous calls (web services, ajax posts, http requests, timeouts) and you need the return operation to take place after the async method's execution, then the Async Code Data Provider should be used.

Examples

1) The following example returns a concatenation of the values of two Textboxes (“FirstName” and “LastName”). This code provider can be used as a value to a third textbox for displaying the full name:

var firstName = form.GetControl("FirstName").GetValue();

var lastName = form.GetControl("LastName").GetValue();
return firstName + " " + lastName;

2) In the following example, a Code Provider can be used to feed the Lookup Values in a Combobox control with Person objects. Combobox Lookup Values property typically accept an array of items. 


var person1 = {ID:1Name"John Black"Position"Operator"};

var person2 = {ID:2Name"George Hitman"Position"Developer"};
var person3 = {ID:3Name"Jack White"Position"Team Leader"};

var persons = [];

persons.push(person1);
persons.push(person2);
persons.push(person3);

// loop through all person records and create a calculated field

// for containing both name and position 
for (var i=0;i < persons.length;i++) {
    persons[i].NameAndPosition = persons[i].Name + " (" + persons[i].Position + ")";
}
return persons;


The above code creates a calculated column for combining the Name and Position attributes. Assuming you have set “NameAndPosition” property as a Lookup Display Field in the Combobox, the final result will look like the following:

See Also

For more info about the concept of Data Providers, their usage and their common properties (Name, OnDataLoaded, Condition, etc) see Basic Concepts: Intro to Data Providers 
For more info about Code and Scripting customization capabilities in Openous, see Introduction to Openous Events and Scripting