Accessing Data

Home » Other Functions » Javascript » Accessing Data
Javascript No Comments

Accessing Data

If a script is operating on a document (basically always) the document model can be accessed as a variable called model. Say the document is an address details and the script is going to set the zone:

model.Zone = “some zone”

or setting a value based on other values: Value = Quantity * Rate…

model.Value=model.Quantity*model.Rate;

or setting a string to have contents based upon text and a field

model.Details=”Support Job – “+model.Job_Number

Accessing Data in From an Inline

The simplest form of accessing a field within an inline document is using “dot notation”:

// Set Total based on Rate from inline Item

model.Total = model.Item.Rate * model.Quantity

If you are running a script based upon the inline then you are working from the inline. In this case the parent fields must be referred to as such. The example below sets a value in the parent view based upon a quantity in the parent and a rate obtained from the inline.

model.parentmodel.Value = model.Rate * model.parentmodel.Quantity

This example would be used if the script was set against the inline field in the parent view. If the script is set against a parent field which is not the inline field the format changes to recognising the name of the inline field. If for example the Rate field is located in an inline called Items the format of the above script would change to;

model.Value = model.Item.Rate * model.Quantity

Accessing Data in From a List

If a model contains a list (of inlines, strings or linked) the items in the list can be accessed by index using [n] notation, for example:

model.TruckName = model.AllTrucks[0].Name;

Sets the top level TruckName in the model to be the same as the Name of the first item in the AllTrucks list – in this case a list of inlines.

Accessing Data in From a Linked Schema

If a model contains a field that links to another schema and you wish to copy data from this schema to the top level (parent) schema. This can be done as follows.

script2

In this example the Item.Cost_Price field will be set to the Hourly_Rate recorded in the Consultant schema.

script3

Accessing Data From The User’s Record

This is useful to enable values to be set based upon the user.

model.User = ScriptingService.getUser()._id
model.Depot = ScriptingService.getUser().Depot;