Login    Sites MenuBlueStep

BlueStep Platform Support

RelateScript
Outline full outline 
Overview 
Data Types 
Operators 
Statements 
Functions 
Working with Relate Data 
Field Data 
Accessing Forms and Fields 
Accessing Fields 
Single Entry Forms 
Multi-Entry Forms 
Queries and Reports 
Searching 
Form Permissions 
Special Data and Functions 
How Formulas Work 
Related Topics 

In all types of formulas except field formulas, data from the records of a query or report may be accessed.  This is done by adding a formula id for the query or report within the formula, and optionally defining a formula id for one or more forms from the records of the query or report.   The query or report will be run as if it were being displayed in the unit of the current record being processed by the formula.  The data element which represents the query or report is a custom object with the following properties:

Property/Function Description
next() A function which receives no inputs other than the query/report object.  It retrieves the record from query or report immediately following the prior record retrieved.  If no record has yet been retrieved it returns the first record.  If called after the last record has been retrieved, the next function returns an empty record object which contains only null data and ignores any changes attempted.  If any of the searching or sorting functions are called, causing a change in the result of the query/report, then the query/report is reset and next() will return the first record.
hasNext() A function which receives no inputs other than the query/report object.  It results in a boolean value indicating whether a subsequent call to next() will result in a valid, non-empty record object being returned.
reset() A function which receives no inputs other than the query/report object.  It causes the next() and hasNext() functions to begin again at the start of the list of records.  It also resets the index to null.
index A read-only integer value containing the index of the last retrieved record.  The value of index will be null if no records have been retrieved, or if the last entry retrieved was an empty or invalid record object.  If any of the searching or sorting functions are called, causing a change in the result of the query/report, then the query/report is reset and index will be null.
count A read-only integer value containing the number of records returned by the query.
get(index) A function which receives an integer value indicating the index of the record to retrieve.  It retrieves a specific record from the query or report.  If the index is greater than or equal to 0 and less than the value of count, then a record from the query is returned.  An index greater than or equal to the value of count is out-of-range.  If the index is a negative number a previously created new record may be returned.  If two new records have been created then an index of -1 will retrieve the first created, and an index of -2 will return the second.  Negative numbers with a magnitude greater than newCount are out-of-range.  If the index specified is null or out-of-range then the function returns an empty record object which contains only null data and ignores any changes attempted.  Also, passing null or an out-of-range index also has the same effect as using the reset() function.
addSearch(formId, fieldId, operator, value)

Adds a search to the current list of records.  The formId and fieldId are String values containing the formula ids of the form and field, not their names or labels.  For additional information on the operators and values that are appropriate for different types of fields, see "Searching."  Search criteria are in addition to, not a replacement of, any search criteria specified in a query or report.  If multiple search criteria are added, only entries meeting the requirements of ALL the search criteria will be returned in the results.  The result of the addSearch function is a String value which will be null if a valid search criteria is defined by the function call.  If the String value is not null, it will contain a message indicating what is wrong.

addSort(formId, fieldId) Adds a sort to the current list of records.  The formId and fieldId are String values containing the formula ids of the form and field, not their names or labels. Sort criteria override any sort settings specified in the query or report, but they do not override any grouping settings, which also have the effect of sorting the records.   In other words, it will sort within the groups.  If multiple sorts are added it will search first by the first sort specified, and next by subsequent sort criteria.  A maximum of three levels of sorting are allowed, including sorting caused by grouping.  The result of the addSort function is a String value which will be null if a valid sort is defined by the function call.  If the String value is not null, it will contain a message indicating what is wrong.
anchorTimestampSearch(timestamp) For queries and reports with search criteria based on the current date/time, causes the query/report to run as though the current date/time was timestamp.  This can be used to view the results of the query/report as they would have looked in the past or as they will look in the future, assuming today's data.  The result of this function is the prior value set, if any.
clearSearchAndSort() Clears any search criteria, sorts and timestamp anchor which may have been previously set.  The result of this function is true if search or sort parameters were actually cleared by the function.  The result is false if the function did not have any settings to clear.
getNewRecord() Results in a new Relate record object matching the record type and "Must Have" record category settings of the query/report.  The new record will be in the unit where the query/report runs.  If the query/report includes records from multiple record types, an empty object will be returned and no new record will be created.  The same result will occur if there are no writable forms available to populate data into the new record.
newCount A read-only integer value indicating how many times the getNewRecord() function has been successfully used in the current formula.  Note that newCount may be used after getNewRecord() much the same way that the index value may be used immediately after calling next().  Namely, the newly created record may be retrieved again by passing the negative of newCount to the get(...) function.
getById(id) Retrieves a record, given the id of one of the entries within the record.  This function will not return a record which is not a part of the query/report result (before any custom searching).  It is useful when you have an id of something from another source, such as a params field, the 'current' record or from another query and you want to find the same record in a different query with possibly different forms available.
setCurrentUnit(id) Sets the unit in which the query will run.  This function receives the id of an entry from any record in the unit.  Note, that this can also effect the location where new records are created.  If rememberSearchAndSort() is used, the current unit will also be remembered.  However, using clearSearchAndSort() does not reset the unit. Reset of the unit can be done by passing null to the setCurrentUnit() function.
setMaxRows(int) Sets the maximum number of rows that will be returned from the query. Will over-write what is currently set in the query settings. Passing "-1" into the function will return all rows.

A record object, returned by the next() function, the get(...) function or the getNewRecord() function has properties corresponding to the formula ids defined for forms from the records returned by the query or report.  A record object, in addition to having properties corresponding to the formula ids specified in the formula configuration, also has two additional functions or methods:

Function Description
getNewRecord() Results in a new Relate record object matching the record type and record categories of the current record.  The new record will also be in the unit where the current record resides.  The function will not create a new record and will result in an empty object if there are no writable forms available to populate data into the new record.  Note that this function is similar, but not identical, to the getNewRecord() function of the query/report object.  Specifically, there are four important differences.  1. The new record may have additional categories.  2.  The new record may be in a sub unit.  3. The new record cannot be retrieved again via the get(...) method of the query/report object.  4. A new record will be created even if the query/report uses multiple record types.
deleteRecord() Deletes the current record.  A safer technique is to mark the record as ready for delete, then send yourself an email so you can go look at the record before you delete it.  WARNING: This really does delete the record.  That means it will delete all of the entries from all of the forms belonging to this record, not just the forms the formula is accessing.  This action is pretty much irreversable.  Don't call this function unless you are 100% certain you are deleting the correct record and that deleting the record is the right thing to do.  If in doubt, don't use this method.
isNull() Returns a Boolean indicating whether the record is null or not. Especially useful when combined with getById.
id() Returns a Strings representing the Record (Entity) ID.
shortId() Returns a Strings representing the Record (Entity) Short ID (seqnum).
fqId() Returns a Strings representing the Record (Entity) Fully Qualified ID (ID with U).
topId() Returns a Strings representing the Record (Entity) Top ID. Top ID does a number of Alt Id lookups and falls back on the normal id results. The order goes, FID, SID, GID, results of id.

Example: If you add a formula id, "clients", for the query All Clients, and you add, under that query, a formula id, "notes", for the Call Notes multi-entry form, and assuming the Call Notes form has a date field with a formula id of "date" you would access the date of the first note of the first client like so:

clients.get(0).notes[0].date;

Example: Normally you would want to loop through each record and perform some action.  This is best accomplished using a while loop:

while(clients.hasNext()) {
    client = clients.next();
    // some actions performed on client done here
}