Helper Functions

Home » Other Functions » Javascript » Helper Functions
Javascript No Comments

ScriptingService provides a set of helper functions which can be called thus:

ScriptingService.functionName(params, if, any)

findFirstInCache (schema, search_json)

Retrieves an item of a specific schema from the cache (schema must be cached) based on search criteria.

  • schema: the schema to search
  • search_json: specific search criteria – JSON format {field:value}

For Example to get a AD_Post_Codes document with the same id as the current document’s Post_Code field:

var someDoc = ScriptingService.findFirstInCache (‘AD_Post_Codes’, {_id: model.Post_Code});

formatDecimals()

Formats any number fields in the model to use their correct number of decimal places

getNumber (model, field)

Gets field value as a number (float). If field is null or not a number return 0. This may need to be used in situations where a simple mathematical function (see paragraph 3) does not return the expected result. The cause of this is often empty strings or null values in the operand fields.

For example; if a statement such as model.Value=model.Quantity*model.Rate; returns odd results it may be necessary to use the Scripting Service as follows;

model.Value = ScriptingService.getNumber(model.Quantity) *

ScriptingService.getNumber(model.Rate);

addBusDaysToDateInMillis(dateInMillis, daysToAdd)

Adds a number of days to a date value specified in milliseconds

addBusDays(date, dd)

Adds a number of days to a date value 

setTime (source, sourceField, destination, destinationField)

This helper function sets both the time field and the hidden ___temp fields.

  • source source object.
  • sourceField name of source field in source object.
  • destination source object.
  • destinationField name of destination field in destination object.

setDate (source, sourceField, destination, destinationField)

Set a date field and the hidden __temp field

  • source source object.
  • sourceField name of source field in source object.
  • destination source object.
  • destinationField name of destination field in destination object.

getParentFieldFromSpecificLevel (model, fieldName, level)

Get field from parent (or ancestry) of specified model by going down to a specific level.

  • model: pass in the current model
  • fieldName: the field to retrieve from parent(s)
  • level: which level of parent to retrieve field from

For example if level is set to 3 then the field will be retrieved from exactly model.parentmodel.parentmodel.parentmodel (and not before even if found).

Following sample retrieves field called ‘Customer’ from model.parentmodel.parentmodel.parentmodel:

ScriptingService.getParentFieldFromSpecificLevel(model,’Customer’, 3)

getParentField (model, fieldName)

Get field from parent of specified model. If field not found in parent then recursively check parent’s ancestry.

  • model: pass in the current model
  • fieldName: the field to retrieve from parent(s)

The following example retrieves field called ‘Customer’ from parent or its ancestry:

ScriptingService.getParentField(model,’Customer’)

setDefaultDate (model, fieldName, offset, overwrite)

Sets a default date in specified field.

  • model: pass in the current model
  • fieldName: the date field to set
  • offset: number of days to adjust date from today (positive or negative)
  • overwrite: if true always overwrite any value in fieldName

The following example sets field Booking_Date to today but does not overwrite it if it is already defined:

ScriptingService.setDefaultDate(model,’Booking_Date’,0, false);

AjaxService.getDocument (schema, document)

Get a document from the system – useful if you need to look into a linked document as part of a script, parameters are:

  • schema: the schema the document is in – a string
  • document: the _id of the document required

The document is returned as a JSON document.

The following example gets a client document linked to from the current document and then sets a value based on a value in the client document:

script4