BusinessObjectBase

BusinessObjectBase TypeScript Class Reference

Overview

BusinessObjectBase class is a base class for all business objects. The developer defines the business objects within the app in JSON files (Business Object Definition) under the Business Objects folder in the app's project. The Dynamics Mobile framework will generate corresponding TypeScript implementations, where each of the Business Objects JSON definitions will be represented by a corresponding TypeScript class. Each of these TypeScript classes extends BusinessObjectBase class.

Don't create BusinessObjectBase class directly - use the automatically generated ancestors.

Properties
Methods
Constructor
Platforms
App Types
Properties

Name

Type

DMS_ROWID

string

members

string

appCode

string

boName

string

boSyncName

string

boTableName

string

dontSendSyncLog

boolean

state

number

Methods

Name

query

livelinkQuery

add

update

delete

Constructor

Do not create BusinessObjectBase instances directly. It is supposed that you have to create instances of the BusinessObjectBase ancestor classes, which are automatically generated during the build process in result of existing Business Object JSON definition files in your app's project.

import { Customer } from '@dms-bo';
...
var customer = new Customer();
Platforms

📳 Devices: All Clouds: All

App Types

📳 Mobile 🌐 Backend

static appCode: string

Description
Example
Description

Property 'appCode' contains the unique of the application where the business object was generated.

Example

import { Customer } from '@dms-bo';
...
console.log(`current app code is ${Customer.appCode}`)

static boName: string

Description
Example
Description

Property 'boName' contains the name of the business object

Example

import { Customer } from '@dms-bo';
...
console.log(`current business object name is ${Customer.boName}`)
//prints out: Customer

static boSyncName: string

Description
Example
Description

Property 'boSyncName' contains the synchronization tag of the business object as specified in the Business Object's JSON definition in the SyncTag property.

Example

import { Customer } from '@dms-bo';
...
console.log(`current business object sync name is ${Customer.boSyncName}`)
//prints out: Customer

static boTableName: string

Description
Example
Description

Property 'boTableName' contains the name of the physical database table , where the records for the business object will be stored. Can be used in row level queries executed via SqlQueryService class.

Example

import { Customer } from '@dms-bo';
import { SqlQueryService } from '@dms';
...
let sqlStatement = `select * from ${Customer.boTableName}`;
let records = await this.SqlQueryService.execute();

static members: Array<BusinessObjectMember>

Description
Example
BusinessObjectMember
Description

the property contains metadata for the properties of the business object as they are defined in the business object JSON definition.

Example
import { Customer } from '@dms-bo';
import * as uuid from 'uuid'
...
var customer1 = new Customer();
//set all string properties with same value
customer1.forEach(function(member){
if(member.dataType=='string'){
customer1[member.name] = 'my_test_string_value';
}
});
BusinessObjectMember

{
name: string; //name of the property
dataType: string; // data type: string, int32, int64, datetime, date, boolean, decimal
length:number; //max number character for string dataType
required: boolean; //if value is required during add() and update()
isPK: boolean; //indicates if the property is primary key - true for property with name DMS_ROWID
}

DMS_ROWID: string

Description
Example
Description

DMS_ROWID property is the unique identifier of the business object instance and is used as a primary key field in the corresponding record in the mobile database. DMS_ROWID is a string, which contains unique UUID( GUID) value. The value an be either automatically generated by the framework when the BusinessObjectBase.add method is called or manually provided by the developer by using the uuid npm or similar uuid generation algorithm.

Example

import { Customer } from '@dms-bo';
import * as uuid from 'uuid'
...
var customer1 = new Customer();
await customer1.add();
console.log(customer1.DMS_ROWID); // will print the automatically generated primary key
var customer2 = new Customer();
customer2.DMS_ROWID = uuid.uuidv4(); //provide manually generated primary key
await customer2.add();
console.log(customer2.DMS_ROWID); // will print the manually generated primary key
//select business object by DMS_ROWID
var customer3 = await customer2.query().filter(`DMS_ROWID='${customer2.DMS_ROWID}'`).executeSelect();
//customer3 properties will be the same as customer2

dontPostToSyncLog: boolean

Description
Example
Description

Property 'dontPostToSyncLog' is false by default and specifies if the sending of the business object instance must be skipped during changes of the instance. Dynamics Mobile framework will automatically generate and send synchronization packet for each business instance when the business object instance add, update and delete methods are called. if property dontPostToSyncLog is set to true, the framework will not send synchronization packet to the backend, which will make so that the change of the business object will not be reflected on the backend.

Example

import { Customer } from '@dms-bo';
...
var customer1 = new Customer();
...
await customer1.add(); // this will persist the entity in the local on-device database and will send packet to the backend
...
var customer2 = new Customer();
...
customer2.dontPostToSyncLog = true;
await customer2.add(); // this will persist the entity localy in the on-device database, but will NOT send sychronization packet to the backend

state: 0 | 1 | 2 | 3

Description
Example
Description

Property 'state' represent the state of the business object.

  • 0 - not modified

  • 1 - created (inserted)

  • 2 - updated

  • 3 - deleted

Call to the methods add, update, delete will change the state property accordingly.

Example

import { Customer } from '@dms-bo';
...
var customer1 = new Customer();
console.log(customer1.state); // will print 0
...
await customer1.add();
console.log(customer1.state); // will print 1
await customer1.update();
console.log(customer1.state); // will print 2
await customer1.delete();
console.log(customer1.state); // will print 3

static query(): DbQuery<T>

Description
Parameters
Returns
Example
Description

The query method will return generic DbQuery instance bound to the current business object instance. This allows the developer to query the local database.

See DbQuery for more information how to use DbQuery

Parameters

the method has no parameters

Returns

Returns generic instance of DbQuery where the type T is equal to the type of the actual BusinessObjectBase ancestor

Example

import { Customer } from '@dms-bo';
...
var customer = await Customer.query().filter(`no=1`).executeSelect();
if(customer && customer.length>0)
console.log(customer[0].no);

static livelinkQuery(): LiveLinkQuery<T>

Description
Parameters
Returns
Example
Description

The query method will return generic LiveLinkQuery instance bound to the current business object instance. This allows the developer to query the local database.

See LiveLinkQuery for more information how to use it

Parameters

Returns

Returns generic instance of LiveLinkQuery where the type T is equal to the type of the actual BusinessObjectBase ancestor

Example

import { Customer } from '@dms-bo';
...
var customer = await Customer.livelinkQuery().filter(`no=1`).executeSelect();
if(customer && customer.length>0)
console.log(customer[0].no);

async add(): Promise<void>

Decription
Parameters
Returns
Example
Decription

The method persists the entity by inserting it as new record into the local on-device database. The method will also generate synchronization packet to notify the backend for a change. The method works in offline mode and the sync packet will be sent to Dynamics Mobile Portal and/or ERP asynchronously once a network connection is detected.

Parameters

the method has no parameters

Returns

void

Example

import { Customer } from '@dms-bo';
...
var customer = new Customer();
customer.no = 1;
customer.name ='customer1'
await customer.add();

async update(): Promise<void>

Description
Parameters
Returns
Example
Description

The method persists the entity by updating it into the local on-device database by using the primary key (DMS_ROWID). The method will also generate synchronization packet to notify the backend for a change. The method works in offline mode and the sync packet will be sent to Dynamics Mobile Portal and/or ERP asynchronously once a network connection is detected.

Parameters

The method has no parameters

Returns

void

Example

import { Customer } from '@dms-bo';
...
//fetch existing customer from the local on-device database
var customer = await Customer.query().filter(`DMS_ROWID='xyz-abv-cde-qwe-dfa-ioq'`).executeSelect();
if(customer.lenght>0) {
//modify the name property in-memory
customer.name ='customer1-newname'
//persist the change locally and remote
await customer.update();
}

async delete(): Promise<void>

Description
Parameters
Returns
Example
Description

The method deletes the entity from the local on-device database by using the primary key (DMS_ROWID). The method will also generate synchronization packet to notify the backend for a change. The method works in offline mode and the sync packet will be sent to Dynamics Mobile Portal and/or ERP asynchronously once a network connection is detected.

Parameters

The method has no parameters

Returns

void

Example

import { Customer } from '@dms-bo';
...
//fetch existing customer from the local on-device database
var customer = await Customer.query().filter(`DMS_ROWID='xyz-abv-cde-qwe-dfa-ioq'`).executeSelect();
if(customer.lenght>0) {
//delete the entity locally and remote
await customer.delete();
}

How to use

import { View, DocumentSeriesService, UserInterfaceService } from '@dms';
import { Invoice } from '@dms-bo';
// it is supposed that there is defined business object Customer in the Business Object folder
@View()
export class MyView {
//declare observable variable - supposed to be bi-directionally databound to the corresponding view ( html)
public customer: ko.Observable<Customer>;
constructor(
private DocumentSeriesService: DocumentSeriesService,
private UserInterfaceService: UserInterfaceService){
//create 'empty' instance of the customer and provide it to the observable to make sure it is "connected" to the UI
this.customer = ko.observable<Customer>(new Customer());
}
//called to validate the UI form
public async validate(){
//valaidate the user input.
//it is supposed that the customer properties are automatically propagated via data-bind
if(!this.customer().no || !this.customer().name) {
return false;
}
else{
return true;
}
}
//called to persist the UI form
public async commit(){
//create the customer in the database
await this.customer().add();
}