Skip to content

Commit 64ed5ab

Browse files
committed
Add LWC Button
1 parent 1dbdc1f commit 64ed5ab

File tree

6 files changed

+97
-44
lines changed

6 files changed

+97
-44
lines changed
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
<aura:component implements="force:lightningQuickActionWithoutHeader,force:hasRecordId,force:hasSObjectName">
2-
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
3-
<c:jsButton aura:id="jsbutton" recordId="{!v.recordId}" cmdtName="{!v.sObjectName}"></c:jsButton>
2+
<c:jsButtonLwc
3+
aura:id="jsbutton"
4+
recordId="{!v.recordId}"
5+
cmdtName="{!v.sObjectName}"
6+
oninitcomplete="{!c.doInit}"
7+
></c:jsButtonLwc>
48
</aura:component>

force-app/main/default/aura/jsButtonQuickAction/jsButtonQuickActionController.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
({
2-
doInit: function (component, event, helper) {
2+
doInit: function (component) {
33
component
44
.find("jsbutton")
55
.invoke()
66
.then(
7-
$A.getCallback(() => {
7+
$A.getCallback((resp) => {
8+
console.log('>> resp '+JSON.stringify(resp));
89
$A.get("e.force:closeQuickAction").fire();
910
})
1011
)
1112
.catch(
1213
$A.getCallback((err) => {
13-
alert("An error occurred " + err);
1414
$A.get("e.force:closeQuickAction").fire();
1515
})
1616
);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": ["@salesforce/eslint-config-lwc/recommended"]
3+
}
Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,99 @@
1-
import { LightningElement, api, wire } from "lwc";
2-
import fetchJSFromCmdt from '@salesforce/apex/DynamicSOQLDMLController.getJSFromCmdt';
3-
import executeSoql from '@salesforce/apex/DynamicSOQLDMLController.executeSoqlQuery';
4-
import runDml from '@salesforce/apex/DynamicSOQLDMLController.executeDml';
5-
import getSObjectType from '@salesforce/apex/DynamicSOQLDMLController.getSObjectTypeFromId';
1+
import { LightningElement, api } from "lwc";
2+
import fetchJSFromCmdt from "@salesforce/apex/DynamicSOQLDMLController.getJSFromCmdt";
3+
import executeSoql from "@salesforce/apex/DynamicSOQLDMLController.executeSoqlQuery";
4+
import executeDml from "@salesforce/apex/DynamicSOQLDMLController.executeDml";
5+
import getSObjectType from "@salesforce/apex/DynamicSOQLDMLController.getSObjectTypeFromId";
6+
import { ShowToastEvent } from "lightning/platformShowToastEvent";
67

78
const REGEX_SOQL = "\\|\\|\\s?(select\\s+[^|]+)\\s?\\|\\|";
89
const REGEX_UPDATE = "\\|\\|\\s?update\\s([^|;]+);?\\s*\\|\\|";
910
const REGEX_INSERT_UPSERT =
1011
"\\|\\|\\s?(insert|upsert)\\s([\\w\\d_]+)\\s?\\(\\s?(\\w+).*\\|\\|";
11-
const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor;
12+
1213
export default class JsButtonLwc extends LightningElement {
1314
@api js;
1415
@api cmdtName;
1516
@api recordId;
17+
_notifiedParent = false;
18+
19+
renderedCallback() {
20+
if (!this._notifiedParent)
21+
this.dispatchEvent(new CustomEvent("initcomplete"));
22+
}
1623

1724
@api
1825
async invoke() {
1926
if (!this.js && this.cmdtName) {
20-
this.js = await fetchJSFromCmdt({ cmdtName });
21-
return await this.runJS();
22-
} else if (this.js) {
23-
return await this.runJS();
27+
let js = await fetchJSFromCmdt({ cmdtName: this.cmdtName });
28+
await this.runJS(js);
29+
}else if(this.js){
30+
await this.runJS(this.js)
2431
}
25-
throw Error("No script found to execute");
2632
}
2733

28-
executeSoql(){
29-
//TODO: implement
34+
_showError(message) {
35+
this.dispatchEvent(new ShowToastEvent({ message, variant: "error" }));
3036
}
3137

32-
executeDml(){
33-
//TODO: implement
38+
async executeSoql(query) {
39+
try {
40+
let results = await executeSoql({ query });
41+
return results;
42+
} catch (err) {
43+
this._showError(err);
44+
}
45+
return null;
3446
}
3547

48+
async executeDml(dmlType, records, sObjectType) {
49+
try {
50+
if(records && !Array.isArray(records)){
51+
records = [records];
52+
}
53+
if (!sObjectType)
54+
sObjectType = await getSObjectType({ recordId: records[0].Id });
55+
records = records.map((rec) => ({
56+
...rec,
57+
attributes: { type: sObjectType }
58+
}));
59+
let results = executeDml({
60+
operation: dmlType,
61+
strData: sObjectType
62+
? JSON.stringify(records, (k, v) => {
63+
return typeof v === "number" ? "" + v : v;
64+
})
65+
: null,
66+
sObjectType
67+
});
68+
return results;
69+
} catch (err) {
70+
this._showError(err);
71+
}
72+
return null;
73+
}
3674

37-
runJS() {
75+
async runJS(js) {
3876
//replace consecutive spaces
39-
this.js = this.js.replace(/\s+/g, " ");
77+
js = js.replace(/\s+/g, " ");
4078

4179
//parse soql
42-
this.js = this.js.replace(
43-
new RegExp(helper.REGEX_SOQL, "gi"),
44-
"await this.executeSoql(cmp,`$1`);"
80+
js = js.replace(
81+
new RegExp(REGEX_SOQL, "gi"),
82+
"await this.executeSoql(`$1`);"
4583
);
4684

4785
//parse updates
48-
this.js = this.js.replace(
49-
new RegExp(helper.REGEX_UPDATE, "gi"),
50-
"await this.executeDml(cmp,'update',$1);"
86+
js = js.replace(
87+
new RegExp(REGEX_UPDATE, "gi"),
88+
"await this.executeDml('update',$1);"
5189
);
5290

5391
//parse inserts
54-
this.js = this.js.replace(
55-
new RegExp(helper.REGEX_INSERT_UPSERT, "gi"),
56-
"await this.executeDml(cmp,'$1',$3,'$2');"
92+
js = js.replace(
93+
new RegExp(REGEX_INSERT_UPSERT, "gi"),
94+
"await this.executeDml('$1',$3,'$2');"
5795
);
58-
59-
return await AsyncFunction("recordId", `return ${js}`).bind(this)(recordId);
60-
96+
let op = await (Function("recordId", `return (async ()=>{${js}})()`).bind(this))(this.recordId);
97+
return op;
6198
}
6299
}

package-lock.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,14 @@
2121
"eslint": "^6.8.0",
2222
"prettier": "^2.0.5",
2323
"prettier-plugin-apex": "^1.4.0"
24-
}
24+
},
25+
"main": ".eslintrc.js",
26+
"dependencies": {},
27+
"repository": {
28+
"type": "git",
29+
"url": "git@suraj.github.com:surajp/lightning-js-button.git"
30+
},
31+
"keywords": [],
32+
"author": "",
33+
"license": "ISC"
2534
}

0 commit comments

Comments
 (0)