|
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"; |
6 | 7 |
|
7 | 8 | const REGEX_SOQL = "\\|\\|\\s?(select\\s+[^|]+)\\s?\\|\\|"; |
8 | 9 | const REGEX_UPDATE = "\\|\\|\\s?update\\s([^|;]+);?\\s*\\|\\|"; |
9 | 10 | const REGEX_INSERT_UPSERT = |
10 | 11 | "\\|\\|\\s?(insert|upsert)\\s([\\w\\d_]+)\\s?\\(\\s?(\\w+).*\\|\\|"; |
11 | | -const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor; |
| 12 | + |
12 | 13 | export default class JsButtonLwc extends LightningElement { |
13 | 14 | @api js; |
14 | 15 | @api cmdtName; |
15 | 16 | @api recordId; |
| 17 | + _notifiedParent = false; |
| 18 | + |
| 19 | + renderedCallback() { |
| 20 | + if (!this._notifiedParent) |
| 21 | + this.dispatchEvent(new CustomEvent("initcomplete")); |
| 22 | + } |
16 | 23 |
|
17 | 24 | @api |
18 | 25 | async invoke() { |
19 | 26 | 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) |
24 | 31 | } |
25 | | - throw Error("No script found to execute"); |
26 | 32 | } |
27 | 33 |
|
28 | | - executeSoql(){ |
29 | | - //TODO: implement |
| 34 | + _showError(message) { |
| 35 | + this.dispatchEvent(new ShowToastEvent({ message, variant: "error" })); |
30 | 36 | } |
31 | 37 |
|
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; |
34 | 46 | } |
35 | 47 |
|
| 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 | + } |
36 | 74 |
|
37 | | - runJS() { |
| 75 | + async runJS(js) { |
38 | 76 | //replace consecutive spaces |
39 | | - this.js = this.js.replace(/\s+/g, " "); |
| 77 | + js = js.replace(/\s+/g, " "); |
40 | 78 |
|
41 | 79 | //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`);" |
45 | 83 | ); |
46 | 84 |
|
47 | 85 | //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);" |
51 | 89 | ); |
52 | 90 |
|
53 | 91 | //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');" |
57 | 95 | ); |
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; |
61 | 98 | } |
62 | 99 | } |
0 commit comments