Renovation
Search…
.model
Model Module is responsible for handling of CRUD operations of documents in the backend.
In addition, it handles the operations associated with documents such as tagging documents & assigning users to documents.

.getDoc() β˜†

To get a single document using its type and name (identifier).

Input

GetDocParams
property
type
required
description
doctype
string
yes
The doctype of the document, e.g: 'Renovation Review'
docname
string
yes
The name (identifier) of the document, e.g: 'RE-00001'
forceFetch
boolean
no
Whether to fetch the document from the backend even if it exists in the local cache. Defaults to false

Output

RenovationDocument
The generic document RenovationDocument can be assigned to a custom doctype type, for instance, Renovation Review. This is possible because RenovationDocument contains Indexed Property.

Example

async-await
1
const documentResponse = await renovationInstance.model.getDoc({
2
doctype: "User",
3
docname: "[email protected]"
4
});
5
if (documentResponse.success) {
6
// If the document was successfully retrieved
7
console.log("Document", documentResponse.data);
8
} else {
9
// If the document was not retrieved
10
console.log("Error", documentResponse.error);
11
}
Copied!
Classic Promise
1
renovationInstance.model
2
.getDoc({
3
doctype: "User",
4
docname: "[email protected]"
5
})
6
.then(documentResponse => {
7
if (documentResponse.success) {
8
// If the document was successfully retrieved
9
console.log("Document", documentResponse.data);
10
} else {
11
// If the document was not retrieved
12
console.log("Error", documentResponse.error);
13
}
14
});
Copied!

Sample Response

1
{
2
"success": true,
3
"httpCode": 200,
4
"data": {
5
"doctype": "User",
6
"name": "[email protected]",
7
"owner": "[email protected]",
8
"creation": "2020-02-27 23:22:34.929267",
9
"modified": "2020-04-11 18:44:31.803200",
10
"modified_by": "[email protected]",
11
"parent": null,
12
"parentfield": null,
13
"parenttype": null,
14
"idx": 616,
15
"docstatus": 0,
16
"enabled": 1,
17
"email": "[email protected]",
18
"first_name": "[email protected]",
19
"middle_name": "testing_save_doc",
20
"last_name": null,
21
"full_name": "[email protected]",
22
"send_welcome_email": 1,
23
"unsubscribed": 0,
24
"username": "test_abc",
25
"language": "en",
26
"time_zone": null,
27
"override_as_global": 0,
28
"user_image": null,
29
"role_profile_name": null,
30
"gender": null,
31
"phone": null,
32
"mobile_no": null,
33
"birth_date": null,
34
"location": null,
35
"banner_image": null,
36
"interest": null,
37
"bio": null,
38
"mute_sounds": 0
39
//.........
40
}
41
};
Copied!
While the backend supplies common properties across doctypes, the above sample should not be assumed in your implementation

Possible Errors

Non-existing document

HTTP code: 404
type: NotFoundError
cause: Docname does not exist
suggestion: Make sure the queried document name is correct or create the required document

Wrong input

HTTP code: 412
type: DataFormatError
cause: The input arguments are in the wrong type/format
suggestion: Use the correct parameters types/formats referencing the functions signature

.getList() β˜†

To get a list of documents using the doctype, and other optional filters.

Input

GetListParams
property
type
required
description
doctype
string
yes
The doctype of the list, e.g: 'User'
fields
string[]
no
The list of docfield names, e.g: ["name", "email", "full_name"]. Use ["*"] if all fields are required
orderBy
string
no
Criteria of ordering ('ASC', 'DESC')
limitPageStart
number
no
Used for pagination. Select which document to start at
limitPageLength
number
no
Used for pagination . Selects the number of results in the response
filters
DBFilter
no
Can use SQL-like querying
parent
string
no
If a child table is to be queried, pass the name of the parent document
tableFields
[x: string] : string[]
no
To get the fields of the child table fields. Can specify the fields like fields
withLinkFields
string[]
no
To get the complete document if a field is of type Link (Another document)

Output

[{ [x: string]: DBBasicValues | [{}] }]
This means the data will include string or number for the fields that are not an object. Also it could contain nested array of objects, for instance, linked documents.

Example

async-await
1
const listResponse = await renovationInstance.model.getList({
2
doctype: "User"
3
});
4
if (listResponse.success) {
5
// If the list was successfully retrieved
6
console.log("Document", listResponse.data);
7
} else {
8
// If the list was not retrieved
9
console.log("Error", listResponse.error);
10
}
Copied!
Classic Promise
1
renovationInstance.model
2
.getList({
3
doctype: "User",
4
fields: ["*"]
5
})
6
.then(listResponse => {
7
if (listResponse.success) {
8
// If the list was successfully retrieved
9
console.log("Document", listResponse.data);
10
} else {
11
// If the list was not retrieved
12
console.log("Error", listResponse.error);
13
}
14
});
Copied!

Sample Response

1
{
2
"success": true,
3
"httpCode": 200,
4
"data": [
5
{
6
"doctype": "User",
7
"name": "[email protected]",
8
"owner": "[email protected]",
9
"creation": "2020-02-27 23:22:34.929267",
10
"modified": "2020-04-11 18:44:31.803200",
11
"modified_by": "[email protected]",
12
"parent": null,
13
"parentfield": null,
14
"parenttype": null,
15
"idx": 616,
16
"docstatus": 0,
17
"enabled": 1,
18
"email": "[email protected]",
19
"first_name": "[email protected]",
20
"middle_name": "testing_save_doc",
21
"last_name": null,
22
"full_name": "[email protected]",
23
"send_welcome_email": 1,
24
"unsubscribed": 0,
25
"username": "test_abc",
26
"language": "en",
27
"time_zone": null,
28
"override_as_global": 0,
29
"user_image": null,
30
"role_profile_name": null,
31
"gender": null,
32
"phone": null,
33
"mobile_no": null,
34
"birth_date": null,
35
"location": null,
36
"banner_image": null,
37
"interest": null,
38
"bio": null,
39
"mute_sounds": 0
40
//.........
41
},
42
{.....},
43
{.....}
44
]
45
};
Copied!
While the backend supplies common properties across doctypes, the above sample should not be assumed in your implementation.

.getValue()

To get a single value from DB.

Input

GetValueParams
property
type
required
description
doctype
string
yes
The doctype of the document, e.g: 'User'
docname
string
yes
The name (identifier) of the document, e.g: '[email protected]'
docfield
string
yes
The name of the field to get the value of, e.g: 'email'

Output

{ [x: string]: DBBasicValues }
This is an Indexed Property which can contain only values of type string or number.

Example

async-await
1
const fieldValue = await renovationInstance.model.getValue({
2
doctype: "User",
3
docname: "[email protected]",
4
docfield: "email"
5
});
6
​
7
if (fieldValue.success) {
8
// User Email: {"email":"[email protected]"}
9
console.log("User Email: ", fieldValue.data);
10
} else {
11
console.log("Error", fieldValue.error);
12
}
Copied!
Classic Promise
1
renovationInstance.model
2
.getValue({
3
doctype: "User",
4
docname: "[email protected]",
5
docfield: "email"
6
})
7
.then(fieldValue => {
8
if (fieldValue.success) {
9
// User Email: {"email":"[email protected]"}
10
console.log("User Email: ", fieldValue.data);
11
} else {
12
console.log("Error", fieldValue.error);
13
}
14
});
Copied!

Possible Errors

Non-existing document

HTTP code: 404
type: NotFoundError
cause: Docname does not exist
suggestion: Make sure the queried document name is correct or create the required document

Wrong input

HTTP code: 412
type: DataFormatError
cause: The input arguments are in the wrong type/format
suggestion: Use the correct parameters types/formats referencing the functions signature

.setValue()

To set a single value from DB.

Input

SetValueParams
property
type
required
description
doctype
string
yes
The doctype of the document, e.g: 'User'
docname
string
yes
The name (identifier) of the document, e.g: '[email protected]'
docfield
string
yes
The name of the field to set the value of, e.g: 'middle_name'
value
DBBasicValues
yes
The value to set the field with

Output

RenovationDocument
Returns the whole document with the field modified.

Example

async-await
1
const userDoc = await renovationInstance.model.setValue({
2
doctype: "User",
3
docname: "[email protected]",
4
docfield: "middle_name",
5
value: "My Middle Name"
6
});
7
​
8
if (userDoc.success) {
9
// User Middle Name: My Middle Name
10
console.log("User Middle Name: ", userDoc.data.middle_name);
11
} else {
12
console.log("Error", userDoc.error);
13
}
Copied!
Classic Promise
1
renovationInstance.model
2
.setValue({
3
doctype: "User",
4
docname: "[email protected]",
5
docfield: "middle_name",
6
value: "My Middle Name"
7
})
8
.then(userDoc => {
9
if (userDoc.success) {
10
// User Middle Name: My Middle Name
11
console.log("User Middle Name: ", userDoc.data.middle_name);
12
} else {
13
console.log("Error", userDoc.error);
14
}
15
});
Copied!

Possible Errors

Doctype Does Not Exist

HTTP code: 404
type: NotFoundError
cause: DocType does not exist
suggestion: Make sure the queried DocType is input correctly or create the required DocType

Docname Does Not Exist

HTTP code: 404
type: NotFoundError
cause: Docname does not exist
suggestion: Make sure the queried document name is correct or create the required document

.setLocalValue()

Sets local value on a document, triggering events for the operation.
The document must be loaded in the cache, otherwise, an error is thrown when used.

Input

SetLocalValueParams
property
type
required
description
doctype
string
yes
The doctype of the document, e.g: 'Renovation Review'
docname
string
yes
The name (identifier) of the document, e.g: 'RE-00021'
docfield
string
yes
The name of the field to set the value of
value
DBBasicValues
yes
The value to set the field with

Example

1
renovationInstance.model.setLocalValue({
2
doctype: "Renovation Review",
3
docname: "RE-00003",
4
docfield: "reviewed_by",
6
});
7
// Reviewed By: [email protected]
8
console.log(
9
"Reviewed By: ",
10
renovationInstance.model.locals["Renovation Review"]["RE-00003"].reviewed_by
11
);
Copied!

.newDoc()

To create a new document with default added properties. Also saves the created document in the local cache.
1
Default fields:
2
docstatus: 0
3
__islocal: 1
4
__unsaved: 1
Copied!

Input

NewDocParams
property
type
required
description
doctype
string
yes
The doctype of the document, e.g: 'Renovation Review'

Output

Promise<RenovationDocument>*
Returns the new document including the default values. The object can be cast to a custom DocType interface to add the properties.

Example

async-await
1
const newDoc = (await renovationInstance.model.newDoc({
2
doctype: "Renovation Review"
3
})) as RenovationReview;
4
​
5
if (newDoc.success) {
6
newDoc.name = "Test Review";
7
// Review Name: Test Review
8
console.log("Review Name: ", newDoc.data.name);
9
} else {
10
console.log("Error", newDoc.error);
11
}
Copied!
Classic Promise
1
renovationInstance.model
2
.newDoc({
3
doctype: "Review"
4
})
5
.then(newDoc => {
6
if (newDoc.success) {
7
newDoc.name = "Test Review";
8
// Review Name: Test Review
9
console.log("Review Name: ", newDoc.data.name);
10
} else {
11
console.log("Error", newDoc.error);
12
}
13
});
Copied!

.saveDoc() β˜†

To save the document in the backend.

Input

SaveDocParams
property
type
required
description
doc
RenovationDocument
yes
The document to be saved

Output

RenovationDocument

Example

async-await
1
const savedDoc = await renovationInstance.model.saveDoc({
2
doc: await renovationInstance.model.newDoc({
3
doctype: "Renovation Review",
4
name: "Test Review"
5
})
6
});
7
​
8
if (savedDoc.success) {
9
// Review Name: Test Review
10
console.log("Review Name: ", savedDoc.data.name);
11
} else {
12
console.log("Error", savedDoc.error);
13
}
Copied!
Classic Promise
1
renovationInstance.model
2
.newDoc({ doctype: "Renovation Review", name: "Test Review" })
3
.then(newDoc =>
4
renovationInstance.model
5
.saveDoc({
6
doc: newDoc
7
})
8
.then(savedDoc => {
9
if (savedDoc.success) {
10
// Review Name: Test Review
11
console.log("Review Name: ", savedDoc.data.name);
12
} else {
13
console.log("Error", savedDoc.error);
14
}
15
})
16
);
Copied!

Possible Errors

Duplicate document found

HTTP code: 409
type: DuplicateEntryError
cause: Duplicate doc found while saving
suggestion: Change the 'name' field or delete the existing document

.submitDoc()

To submit the document in the backend.

Input

SubmitDocParams
property
type
required
description
doc
RenovationDocument
yes
The document to be submitted

Output

RenovationDocument

Example

async-await
1
const submittedDoc = await renovationInstance.model.submitDoc({
2
doc: { doctype: "Renovation Review", name: "Test Review" }
3
});
4
​
5
if (submittedDoc.success) {
6
// Review Name: Test Review
7
console.log("Review Name: ", submittedDoc.data.name);
8
} else {
9
console.log("Error", submittedDoc.error);
10
}
Copied!
Classic Promise
1
renovationInstance.model
2
.submitDoc({
3
doc: { doctype: "Renovation Review", name: "Test Review" }
4
})
5
.then(submittedDoc => {
6
if (submittedDoc.success) {
7
// Review Name: Test Review
8
console.log("Review Name: ", submittedDoc.data.name);
9
} else {
10
console.log("Error", submittedDoc.error);
11
}
12
});
Copied!

Possible Errors

Doctype Does Not Exist

HTTP code: 404
type: NotFoundError
cause: DocType does not exist
suggestion: Make sure the queried DocType is input correctly or create the required DocType

Non-existing document

HTTP code: 404
type: NotFoundError
cause: Docname does not exist
suggestion: Make sure the queried document name is correct or create the required document

.saveSubmitDoc() β˜…

To save the document first, then submit, in a single db transaction.

Input

SaveSubmitDocParams
property
type
required
description
doc
RenovationDocument
yes
The document to be saved & submitted

Output

RenovationDocument

Example

async-await
1
const savedSubmittedDoc = await renovationInstance.model.saveSubmitDoc({
2
doc: { doctype: "Renovation Review", name: "Test Review" }
3
});
4
​
5
if (savedSubmittedDoc.success) {
6
// Review Name: Test Review
7
console.log("Review Name: ", savedSubmittedDoc.data.name);
8
} else {
9
console.log("Error", savedSubmittedDoc.error);
10
}
Copied!
Classic Promise
1
renovationInstance.model
2
.saveSubmitDoc({
3
doc: { doctype: "Renovation Review", name: "Test Review" }
4
})
5
.then(savedSubmittedDoc => {
6
if (savedSubmittedDoc.success) {
7
// Review Name: Test Review
8
console.log("Review Name: ", savedSubmittedDoc.data.name);
9
} else {
10
console.log("Error", savedSubmittedDoc.error);
11
}
12
});
Copied!

Possible Errors

Doctype Does Not Exist

HTTP code: 404
type: NotFoundError
cause: DocType does not exist
suggestion: Make sure the queried DocType is input correctly or create the required DocType

Non-existing document

HTTP code: 404
type: NotFoundError
cause: Docname does not exist
suggestion: Make sure the queried document name is correct or create the required document

.copyDoc()

To create a cloned document, with a new local name.
The cloned document is also added to the local cache.

Input

CopyDocParams
property
type
required
description
doc
RenovationDocument
yes
The document to be cloned

Output

Promise<RenovationDocument>*

Example

async-await
1
const clonedDoc = await renovationInstance.model.copyDoc({
2
doc: { doctype: "Renovation Review", name: "Test Review" }
3
});
4
​
5
if (clonedDoc.success) {
6
// Review Name: New Renovation Review 1
7
console.log("Review Name: ", clonedDoc.data.name);
8
} else {
9
console.log("Error", clonedDoc.error);
10
}
Copied!
Classic Promise
1
renovationInstance.model
2
.saveSubmitDoc({
3
doc: { doctype: "Renovation Review", name: "Test Review" }
4
})
5
.then(clonedDoc => {
6
if (clonedDoc.success) {
7
// Review Name: New Renovation Review 1
8
console.log("Review Name: ", clonedDoc.data.name);
9
} else {
10
console.log("Error", clonedDoc.error);
11
}
12
});
Copied!

.amendDoc()

To clone a document then set amended_from property to the original doc.

Input

RenovationDocument
property
type
required
description
doc
RenovationDocument
yes
The document to be cloned

Output

Promise<RenovationDocument>*

Example

async-await
1
const amendedDoc = await renovationInstance.model.amendDoc({
2
doc: { doctype: "Renovation Review", name: "Test Review" }
3
});
4
​
5
if (amendedDoc.success) {
6
// Review Name: New Renovation Review 1
7
console.log("Review Name: ", amendedDoc.data.amended_from);
8
} else {
9
console.log("Error", amendedDoc.error);
10
}
Copied!
Classic Promise
1
renovationInstance.model
2
.amendDoc({
3
doc: { doctype: "Renovation Review", name: "Test Review" }
4
})
5
.then(amendedDoc => {
6
if (amendedDoc.success) {
7
// Review Name: New Renovation Review 1
8
console.log("Review Name: ", amendedDoc.data.amended_from);
9
} else {
10
console.log("Error", amendedDoc.error);
11
}
12
});
Copied!

.addChildDoc()

To add a child document to a parent document. (Table field)

Input

AddChildDocParams
property
type
required
description
doc
RenovationDocument
yes
The document to be cloned
field
string | DocField
yes
The field in the parent doc to add the new child doc.

Output

Promise<RenovationDocument>*
The child document added.

Example

async-await
1
const childDoc = await renovationInstance.model.addChildDoc({
2
doc: { doctype: "Renovation Review", name: "Test Review" },
3
field: "reviews"
4
});
5
​
6
// Save the child doc or manipulate before saving...
Copied!
Classic Promise
1
renovationInstance.model
2
.addChildDoc({
3
doc: { doctype: "Renovation Review", name: "Test Review" },
4
field: "reviews"
5
})
6
.then(childDoc => {
7
// Save the child doc or manipulate before saving...
8
});
Copied!

.deleteDoc()

To delete a document from the backend.

Input

DeleteDocParams
property
type
required
description
doctype
string
yes
The doctype of the document
docname
string
yes
The document name to be deleted

Output

RenovationDocument

Example

async-await
1
const deletedDoc = await renovationInstance.model.deleteDoc({
2
doctype: "Renovation Review",
3
docname: "Test Review"
4
});
5
​
6
if (deletedDoc.success) {
7
// Review Name: Test Review
8
console.log("Review Name: ", deletedDoc.data.name);
9
} else {
10
console.log("Error", deletedDoc.error);
11
}
Copied!
Classic Promise
1
renovationInstance.model
2
.deleteDoc({
3
doctype: "Renovation Review",
4
docname: "Test Review"
5
})
6
.then(deletedDoc => {
7
if (deletedDoc.success) {
8
// Review Name: Test Review
9
console.log("Review Name: ", deletedDoc.data.name);
10
} else {
11
console.log("Error", deletedDoc.error);
12
}
13
});
Copied!

Possible Errors

Non-existing document

HTTP code: 404
type: NotFoundError
cause: Docname does not exist
suggestion: Make sure the queried document name is correct or create the required document

.cancelDoc()

To cancel a submitted document in the backend.

Input

CancelDocParams
property
type
required
description
doc
RenovationDocument
yes
The document to be cancelled

Output

RenovationDocument

Example

async-await
1
const cancelledDoc = await renovationInstance.model.cancelDoc({
2
doc: {
3
doctype: "Renovation Review",
4
docname: "Test Review"
5
}
6
});
7
​
8
if (cancelledDoc.success) {
9
// Review Name: Test Review
10
console.log("Review Name: ", cancelledDoc.data.name);
11
} else {
12
console.log("Error", cancelledDoc.error);
13
}
Copied!
Classic Promise
1
renovationInstance.model
2
.cancelDoc({
3
doc: {
4
doctype: "Renovation Review",
5
docname: "Test Review"
6
}
7
})
8
.then(cancelledDoc => {
9
if (cancelledDoc.success) {
10
// Review Name: Test Review
11
console.log("Review Name: ", cancelledDoc.data.name);
12
} else {
13
console.log("Error", cancelledDoc.error);
14
}
15
});
Copied!

Possible Errors

Doctype Does Not Exist

HTTP code: 404
type: NotFoundError
cause: DocType does not exist
suggestion: Make sure the queried DocType is input correctly or create the required DocType

Non-existing document

HTTP code: 404
type: NotFoundError
cause: Docname does not exist
suggestion: Make sure the queried document name is correct or create the required document

.assignDoc()

To assign a document to a user.

Input

AssignDocParams
property
type
required
description
doctype
string
yes
The doctype to assign to
assignTo
string
yes
The user to be assigned
docname
string
no
The specific document assigned to
docnames
string[]
no
The list of documents to be assigned to
myself
boolean
no
Whether the current logged in user is assigned
description
string
no
Description of the assignment
dueDate
string
no
The due date for the assignment
notify
boolean
no
Whether to notify the user
priority
string
no
The priority to be set for the assignment. Possible values: "Low" | "Medium" | "High"
bulkAssign
boolean
no
Whether to assign in bulk of documents. Use docnames instead while using bulkAssign

Output

any

Example

async-await
1
const assignResponse = await renovationInstance.model.assignDoc({
2
assignTo: "[email protected]",
3
doctype: "Renovation Review",
4
docname: "Renovation Review A",
5
description: "Test Assign",
6
priority: "High",
7
dueDate: "2050-12-31"
8
});
9
​
10
if (assignResponse.success) {
11
12
console.log("Owner: ", assignResponse.data.owner);
13
} else {
14
console.log("Error", assignResponse.error);
15
}
Copied!
Classic Promise
1
renovationInstance.model
2
.assignDoc({
3
assignTo: "[email protected]",
4
doctype: "Renovation Review",
5
docname: "Renovation Review A",
6
description: "Test Assign",
7
priority: "High",
8
dueDate: "2050-12-31"
9
})
10
.then(assignResponse => {
11
if (assignResponse.success) {
12
13
console.log("Owner: ", assignResponse.data.owner);
14
// Description: [email protected]
15
console.log("Description: ", assignResponse.data.description);