4D v14.3Calling 4D Tables and Methods |
||||||||||
|
4D v14.3
Calling 4D Tables and Methods
|
4D scope | Wakanda object |
table | datastore class |
current selection | entity collection |
current record | entity |
Note: 4D methods can also be called on the client side using datasources (see below); in this case all the methods are available, and the datasource applies them automatically to the current collection or the current entity depending on the context.
For example, if you perform a query using the query method (see previous section), Wakanda returns an entity collection. You can execute any 4D project method whose scope is declared as "current selection" on this collection.
There are three ways for 4D methods to be called by JavaScript code:
var vTot = ds.Emp.raiseSalary(param)) //raiseSalary is a datastore class property //the catalog is merged with the active model var vTot2 = my4DStore.Company.first().capital(param)) //capital is an entity property since first() returns an entity //using the my4DStore dedicated model
sources.employee.raiseSalary(param, {onSuccess: function(event) { ... //code to execute when method has finished} }))
ds.Employee.raiseSalary(param, // syntax resembles a SSJS call {onSuccess: function(event) // but it’s client-side code so you must // manage the callback method of the asynchronous call { ... //code to execute when 4D method has finished} }))
The choice of location (server or client) and API depends on the needs of the application and is described in the Wakanda documentation.
As with standard methods, you can pass parameters during the call, which are received in order in the parameters $1, $2, and so on. Similarly, the method can return a result in the $0 variable.
Example: You want to give a 5% raise to employees whose salary is less than 1500.
C_REAL($1)
READ WRITE([Employees])
FIRST RECORD([Employees])
While(Not(End selection([Employees])))
[Employees]salary:=[Employees]salary*$1
SAVE RECORD([Employees])
NEXT RECORD([Employees])
End while
UNLOAD RECORD([Employees])
var emp = ds.Employees.query("salary < :1",1500); // emp contains the collection of employees whose salary is <1500 emp.IncreaseSalary(1.05); //execute the IncreaseSalary method on the collection //You could also write: //"ds.Employees.query("salary < :1",1500).IncreaseSalary(1.05);
You can also return a 4D selection directly as a Wakanda collection using the command. For example:
When calling a 4D method by means of the Wakanda link:
Note that after executing a method through 4D Mobile, the 4D context is reset:
You must make sure the scope of the 4D method corresponds to the type of Wakanda object that is calling it, otherwise a "TypeError: 'undefined' is not a function" error is returned by Wakanda.
For example, given the 4D "getcursel" method containing the following code:
$0:=Records in selection([Table_1])
Given the run method on the Wakanda side:
var tt = ds.Table_1.query("Field_2 = 'a*'").getcursel();
The query( ) method returns a collection. If the scope of the getcursel method was set as "Current record", Wakanda returns the following error:
TypeError: 'undefined' is not a function (evaluating 'ds.Table_1.query("Field_2 = 'a*'").getcursel()')".
Product: 4D
Theme: Calling 4D Tables and Methods
4D Mobile ( 4D v14 R3)
4D Mobile ( 4D v14.3)
4D Mobile ( 4D v14 R4)
Inherited from : Calling 4D Tables and Methods ( 4D v14 R2)