Skip to content

Commit 53c362a

Browse files
committed
Add error handler
1 parent 8c6d4a5 commit 53c362a

File tree

3 files changed

+80
-54
lines changed

3 files changed

+80
-54
lines changed
Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
<template>
2-
<div style="height: 300px;">
3-
<lightning-card variant="Narrow" title={title} icon-name={iconName}>
4-
<lightning-button label="New" title="Non-primary action" onclick={newRecord} class="slds-m-left_x-small"
5-
slot="actions"></lightning-button>
6-
<template if:true={data}>
7-
<lightning-datatable key-field="id" data={data} columns={columns} onrowaction={handleRowAction} hide-checkbox-column >
8-
</lightning-datatable>
9-
</template>
10-
<div slot="footer" style="text-align: right;">
11-
<lightning-button variant="brand" disabled={isDisablePrev} label="First" title="Primary action" onclick={firstPage}
12-
class="slds-m-left_xx-small"></lightning-button>
13-
<lightning-button variant="brand" disabled={isDisablePrev} label="Previous" title="Primary action" onclick={previous}
14-
class="slds-m-left_xx-small"></lightning-button>
15-
<lightning-button variant="brand" disabled={isDisableNext} label="Next" title="Primary action" onclick={next}
16-
class="slds-m-left_xx-small"></lightning-button>
17-
<lightning-button variant="brand" disabled={isDisableNext} label="Last" title="Primary action" onclick={lastPage}
18-
class="slds-m-left_xx-small slds-m-right_xx-small"></lightning-button>
2+
<!-- Main content -->
3+
<lightning-card variant="Narrow" title={title} icon-name={iconName}>
4+
<lightning-button label="New" title="Non-primary action" onclick={newRecord} class="slds-m-left_x-small"
5+
slot="actions"></lightning-button>
6+
<!-- Error block -->
7+
<template if:true={error}>
8+
<div class="slds-notify slds-notify_alert slds-theme_alert-texture slds-theme_error" role="alert">
9+
<span class="slds-assistive-text">error</span>
10+
<p>{error}</p>
1911
</div>
20-
</lightning-card>
21-
</div>
12+
</template>
13+
<!-- Data table -->
14+
<template if:true={data}>
15+
<lightning-datatable key-field="id" data={data} columns={columns} onrowaction={handleRowAction}
16+
hide-checkbox-column>
17+
</lightning-datatable>
18+
</template>
19+
<!-- Pagination -->
20+
<div slot="footer" style="text-align: right;">
21+
<lightning-button variant="brand" disabled={isDisablePrev} label="First" title="Primary action"
22+
onclick={firstPage} class="slds-m-left_xx-small"></lightning-button>
23+
<lightning-button variant="brand" disabled={isDisablePrev} label="Previous" title="Primary action"
24+
onclick={previous} class="slds-m-left_xx-small"></lightning-button>
25+
<lightning-button variant="brand" disabled={isDisableNext} label="Next" title="Primary action" onclick={next}
26+
class="slds-m-left_xx-small"></lightning-button>
27+
<lightning-button variant="brand" disabled={isDisableNext} label="Last" title="Primary action"
28+
onclick={lastPage} class="slds-m-left_xx-small slds-m-right_xx-small"></lightning-button>
29+
</div>
30+
</lightning-card>
2231
</template>

main/default/lwc/lwcRelatedList/lwcRelatedList.js

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2-
* Author : Sarvesh
3-
* Description : The component to show records of custom/standard of Object as a table.
4-
* Production Ready : Yes
5-
*/
2+
* Author : Sarvesh
3+
* Description : The component to show records of custom/standard of Object as a table.
4+
* Production Ready : Yes
5+
*/
66
import { LightningElement, wire, track, api } from "lwc";
77
import { NavigationMixin } from "lightning/navigation";
88
import { deleteRecord } from "lightning/uiRecordApi";
@@ -14,7 +14,7 @@ let cols;
1414
const actions = [
1515
{ label: "Show details", name: "show_details" },
1616
{ label: "Edit", name: "edit" },
17-
{ label: "Delete", name: "delete" }
17+
{ label: "Delete", name: "delete" },
1818
];
1919
export default class LightningDatatable extends NavigationMixin(
2020
LightningElement
@@ -33,47 +33,61 @@ export default class LightningDatatable extends NavigationMixin(
3333
@track soql;
3434
@track offSet = 0;
3535
@track totalRows = 0;
36+
@track error;
3637

3738
// Do init funtion
3839
connectedCallback() {
39-
cols = JSON.parse(this.columns);
40+
if (this.columns != null && this.columns != undefined) {
41+
cols = JSON.parse(this.columns);
42+
}
4043
cols.push({
4144
type: "action",
42-
typeAttributes: { rowActions: actions }
45+
typeAttributes: { rowActions: actions },
4346
});
4447
this.columns = cols;
4548
this.buildSOQL();
46-
countRecords({ objectName: this.objectName }).then(result => {
49+
countRecords({ objectName: this.objectName }).then((result) => {
4750
this.totalRows = result;
4851
});
4952
this.fetchRecords();
5053
}
5154

5255
fetchRecords() {
53-
getRecords({ soql: this.soql }).then(data => {
54-
if (data) {
55-
data.map(e => {
56-
for (let key in e) {
57-
if (typeof e[key] === "object") {
58-
for (let onLevel in e[key]) {
59-
e[key + "." + onLevel] = e[key][onLevel];
56+
getRecords({ soql: this.soql })
57+
.then((data) => {
58+
if (data) {
59+
data.map((e) => {
60+
for (let key in e) {
61+
if (typeof e[key] === "object") {
62+
for (let onLevel in e[key]) {
63+
e[key + "." + onLevel] = e[key][onLevel];
64+
}
6065
}
6166
}
67+
});
68+
this.data = data;
69+
}
70+
})
71+
.catch((error) => {
72+
if (error) {
73+
this.error = "Unknown error";
74+
if (Array.isArray(error.body)) {
75+
this.error = error.body.map((e) => e.message).join(", ");
76+
} else if (typeof error.body.message === "string") {
77+
this.error = error.body.message;
6278
}
63-
});
64-
this.data = data;
65-
} else if (error) {
66-
}
67-
});
79+
console.log("error", this.error);
80+
}
81+
});
6882
}
6983

7084
newRecord() {
7185
this[NavigationMixin.Navigate]({
7286
type: "standard__objectPage",
7387
attributes: {
7488
objectApiName: this.objectName,
75-
actionName: "new"
76-
}
89+
actionName: "new",
90+
},
7791
});
7892
}
7993

@@ -125,7 +139,11 @@ export default class LightningDatatable extends NavigationMixin(
125139
}
126140

127141
get isDisableNext() {
128-
return this.offSet + this.limit >= this.totalRows ? true : this.totalRows <= this.limit ? false : false;
142+
return this.offSet + this.limit >= this.totalRows
143+
? true
144+
: this.totalRows <= this.limit
145+
? false
146+
: false;
129147
}
130148

131149
/*********************************************************************
@@ -143,7 +161,7 @@ export default class LightningDatatable extends NavigationMixin(
143161
.concat(this.data.slice(index + 1));
144162
this.showToast("Success", "Record deleted", "success");
145163
})
146-
.catch(error => {
164+
.catch((error) => {
147165
this.showToast("Error deleting record", error.body.message, "error");
148166
});
149167
}
@@ -167,8 +185,8 @@ export default class LightningDatatable extends NavigationMixin(
167185
attributes: {
168186
recordId: row["Id"],
169187
objectApiName: this.objectName,
170-
actionName: "view"
171-
}
188+
actionName: "view",
189+
},
172190
});
173191
}
174192

@@ -178,8 +196,8 @@ export default class LightningDatatable extends NavigationMixin(
178196
attributes: {
179197
recordId: row["Id"],
180198
objectApiName: this.objectName,
181-
actionName: "edit"
182-
}
199+
actionName: "edit",
200+
},
183201
});
184202
}
185203

@@ -189,15 +207,14 @@ export default class LightningDatatable extends NavigationMixin(
189207
soql += this.appendWhere();
190208
soql += this.appendLimit();
191209
soql += this.appendOffset();
192-
console.log("soql:::", soql);
193210
this.soql = soql;
194211
}
195212

196213
appendField() {
197214
let soql = "SELECT Id,",
198215
col = [];
199216
if (cols) {
200-
cols.map(val => {
217+
cols.map((val) => {
201218
if (val.hasOwnProperty("fieldName")) {
202219
col.push(val["fieldName"]);
203220
}
@@ -230,7 +247,7 @@ export default class LightningDatatable extends NavigationMixin(
230247
new ShowToastEvent({
231248
title: title,
232249
message: message,
233-
variant: variant
250+
variant: variant,
234251
})
235252
);
236253
}

main/default/lwc/lwcRelatedList/lwcRelatedList.js-meta.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
<!-- Configuring the design attributes -->
99
<targetConfigs>
1010
<targetConfig targets="lightning__RecordPage">
11-
<property name="iconName" type="String" label="Enter Icon Name" default="standard:account"/>
12-
<property name="title" type="String" label="Enter Title" default="LWC Table"/>
13-
<property name="objectName" type="String" label="Enter Object API Name"/>
14-
<property name="columns" type="String" label="Enter Columns JSON"/>
11+
<property name="iconName" type="String" required="true" label="Enter Icon Name" default="standard:account"/>
12+
<property name="title" type="String" required="true" label="Enter Title" default="LWC Table"/>
13+
<property name="objectName" type="String" required="true" label="Enter Object API Name"/>
14+
<property name="columns" type="String" required="true" label="Enter Columns JSON"/>
1515
<property name="relatedFieldAPI" type="String" default="" label="Enter Related field API Name"/>
1616
<property name="whereClause" type="String" default="" label="Enter WHERE clause"/>
1717
</targetConfig>

0 commit comments

Comments
 (0)