Skip to content

Commit cdae809

Browse files
committed
Add sample scripts
1 parent e1979f4 commit cdae809

File tree

4 files changed

+122
-2
lines changed

4 files changed

+122
-2
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ alert(Array(5).fill(0).map((e,i)=>'Hello, '+i));
2424

2525
```javascript
2626
let accts=|| Select Name,(Select Id from Contacts) from Account order by createddate desc limit 100 ||;
27-
let contacts = accts.filter((a)=>!a.Contacts || a.Contacts.length===0).slice(0,10).map((a)=>({LastName: a.Name+'-Contact', AccountId: a.Id}));
27+
let contacts = accts.filter((a)=>!a.Contacts || a.Contacts.length===0)
28+
.slice(0,10)
29+
.map((a)=>({LastName: a.Name+'-Contact', AccountId: a.Id}));
2830
let contactIds = || insert Contact(contacts) ||; /*Note how the SObjectType has been specified. This is required for insert and upsert*/
2931
$A.get('e.force:refreshView').fire(); /* $A is supported!*/
3032
```
@@ -47,13 +49,14 @@ $A.get('e.force:refreshView').fire();
4749
* Upsert and Update statements must be qualified with the SObjectType thus `|| insert Account(accts) ||;`
4850
* SOQL statements are parsed using template literals. Any arguments should follow the appropriate syntax `${argument}`
4951
* SOQL and DML statements may not be wrapped in a function.
52+
* All statements must be strictly terminated by a semicolon.
5053

5154
### Known Limitations
5255

5356
* Support for delete has been intentionally withheld.
5457
* Single-line comments are not supported.
5558
* Haven't tested DML with date, datetime, boolean, geolocation and other compound fields. I will update this section as I do so.
56-
* Explicit use of async/await, Promises and Generators is not supported, atm.
59+
* SOQL and DML statements should be enclosed in async functions, if they are required to be contained in functions. The program automatically adds `await` to SOQL and DML statements
5760
* DML on Files, Attachments, Documents, etc. is not supported
5861

5962
### For Developers: Extending to more than one Button per SObjectType

scripts/jsButton/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## These scripts assume you've a named credential named 'salesforce' created for your instance. The detailed steps for doing so are below:
2+
3+
* Create an Auth Provider of type 'Salesforce'. You can either create your own Connected App or use the following consumer key and secret. The scopes should include `api` and `refresh_token`
4+
* Create a named credential using the Auth Provider above and name it `salesforce`. The authentication type should be `OAuth`. For better security, I suggest you used `Per User` authentication so only authorized users can use the Salesforce APIs through the button
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
try {
2+
let cmpName = prompt(
3+
"Enter the name for your aura bundle. This will be the name of the custom metadata record backing this bundle as well"
4+
);
5+
6+
if (!cmpName) return;
7+
8+
this.httpRequest.setEndpoint(
9+
"callout:salesforce/services/data/v48.0/tooling/sobjects/AuraDefinitionBundle/"
10+
);
11+
12+
this.httpRequest.setMethod("POST");
13+
14+
this.httpRequest.addHeader("Content-Type", "application/json");
15+
16+
this.httpRequest.setBody({
17+
MasterLabel: cmpName,
18+
Description: "created by js button",
19+
ApiVersion: 48.0,
20+
DeveloperName: cmpName
21+
});
22+
23+
let resp = await this.httpRequest.send();
24+
25+
let auraBundleId = JSON.parse(resp.body).id;
26+
27+
alert(auraBundleId);
28+
29+
this.httpRequest.clear();
30+
31+
this.httpRequest.setEndpoint(
32+
"callout:salesforce/services/data/v48.0/tooling/sobjects/AuraDefinition/"
33+
);
34+
35+
this.httpRequest.setMethod("POST");
36+
this.httpRequest.addHeader("Content-Type", "application/json");
37+
38+
let source = ` <aura:component implements="force:lightningQuickActionWithoutHeader,force:hasRecordId,force:hasSObjectName">
39+
<c:jsButtonLwc
40+
aura:id="jsbutton"
41+
recordId="{!v.recordId}"
42+
cmdtName="${cmpName}"
43+
oninitcomplete="{!c.doInit}"
44+
></c:jsButtonLwc>
45+
</aura:component>`;
46+
47+
this.httpRequest.setBody({
48+
AuraDefinitionBundleId: auraBundleId,
49+
DefType: "COMPONENT",
50+
Format: "XML",
51+
Source: source
52+
});
53+
54+
resp = await this.httpRequest.send();
55+
56+
alert(resp.statusCode);
57+
58+
source = ` ({
59+
doInit: function (component) {
60+
component
61+
.find("jsbutton")
62+
.invoke()
63+
.then(
64+
$A.getCallback((resp) => {
65+
$A.get("e.force:closeQuickAction").fire();
66+
})
67+
)
68+
.catch(
69+
$A.getCallback((err) => {
70+
$A.get("e.force:closeQuickAction").fire();
71+
})
72+
);
73+
}
74+
});`;
75+
76+
this.httpRequest.setBody({
77+
AuraDefinitionBundleId: auraBundleId,
78+
DefType: "CONTROLLER",
79+
Format: "JS",
80+
Source: source
81+
});
82+
83+
resp = await this.httpRequest.send();
84+
85+
alert(resp.statusCode);
86+
} catch (e) {
87+
alert(JSON.stringify(e));
88+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
try {
2+
this.httpRequest.setEndpoint(
3+
"callout:salesforce/services/data/v48.0/tooling/query/?q=Select+Id,FullName+from+Flow+where+status+!=+'Active'"
4+
);
5+
6+
let resp = await this.httpRequest.send();
7+
let respJson = JSON.parse(resp.body);
8+
9+
let results = await Promise.all(
10+
respJson.records.map(async (rec) => {
11+
this.httpRequest.clear();
12+
let flowId = rec.Id;
13+
this.httpRequest.setEndpoint(
14+
"callout:salesforce/services/data/v48.0/tooling/sobjects/Flow/" + flowId + "/"
15+
);
16+
this.httpRequest.setMethod("DELETE");
17+
resp = await this.httpRequest.send();
18+
return resp.statusCode;
19+
})
20+
);
21+
22+
alert(results);
23+
} catch (e) {
24+
alert(JSON.stringify(e));
25+
}

0 commit comments

Comments
 (0)