Salesforce Public Site 学习

Salesforce Public Site 如何调用 Flow?

Salesforce Public Site 如何调用 Flow?

这里注意inputVariables 是固定格式。

1
2
3
4
5
var inputVariables = [ {
name : 'RecordId',
type : 'String',
value :customerId
}

VF Page输入RecordId。在VF Page 传入参数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<apex:page sidebar="false" showHeader="false" >
<html>
<head>
<apex:includeLightning />
</head>

<body class="slds-scope">



<div id="flowContainer" />

<script>
// get url id parameter
var queryString = window.location.search;
var urlParams = new URLSearchParams(queryString);
var RecordId =urlParams.get('Id');

var inputVariables = [ {
name : 'RecordId',
type : 'String',
value :RecordId
}
];
$Lightning.use("c:InputApp", function() {
$Lightning.createComponent("lightning:flow",
{ },
"flowContainer",
function(cmp) {
console.log("Component was created");
cmp.startFlow("ScreenFlow",inputVariables);
console.log("Flow was created");
// do some stuff
}
);
});
</script>
</body>
</html>

</apex:page>

InputApp 为Aura App

1
2
3
<aura:application extends="ltng:outApp" access="global" implements="ltng:allowGuestAccess" >
<aura:dependency resource="lightning:flow"/>
</aura:application>

写一个File Upload Aura Component, 并且适用于Screen Flow

Aura传入myRecordId。

1
2
3
4
5
6
<aura:component controller="FileController" implements="lightning:availableForFlowScreens" access="global" >
<aura:attribute name="myRecordId" type="String" description="Record to which the files should be attached" />
<div>
<input type="file" class="file" aura:id="file" onchange="{!c.FileOnchange}" />
</div>
</aura:component>

Controller.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
({


FileOnchange: function(component, event, helper) {
console.log('File changed!');
helper.save(component);
},
waiting: function(component, event, helper) {
$A.util.addClass(component.find("uploading").getElement(), "uploading");
$A.util.removeClass(component.find("uploading").getElement(), "notUploading");
},

doneWaiting: function(component, event, helper) {
$A.util.removeClass(component.find("uploading").getElement(), "uploading");
$A.util.addClass(component.find("uploading").getElement(), "notUploading");
}

})

helper.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
({

save : function(component) {
var fileInput = component.find("file").getElement();
var file = fileInput.files[0];

if (file.size > 750000) {
alert('File size cannot exceed 750000 bytes.\n' +
'Selected file size: ' + file.size);
return;
}

var fr = new FileReader();
console.log('v14');
var self = this;
fr.onload = function() {
var fileContents = fr.result;
var base64Mark = 'base64,';
var dataStart = fileContents.indexOf(base64Mark) + base64Mark.length;

fileContents = fileContents.substring(dataStart);
console.log('upload');
self.upload(component, file, fileContents);
};

fr.readAsDataURL(file);
},
upload: function(component, file, fileContents) {
console.log('enter upload function ');

var action = component.get("c.saveTheFile");

action.setParams({
parentId: component.get("v.myRecordId"),
fileName: file.name,
base64Data: encodeURIComponent(fileContents),
contentType: file.type
});

action.setCallback(this, function(a) {
var attachId = a.getReturnValue();
});

$A.enqueueAction(action);
}

})

这里需要design , flow 可以传入参数。

1
2
3
4
5
6
7
<design:component>
<design:attribute
name="myRecordId"
label="Related Record ID"
required="true"/>

</design:component>

FileController class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
global class FileController {
@AuraEnabled
global static Id saveTheFile(Id parentId, String fileName, String base64Data, String contentType) {
base64Data = EncodingUtil.urlDecode(base64Data, 'UTF-8');

Attachment a = new Attachment();
a.parentId = parentId;

a.Body = EncodingUtil.base64Decode(base64Data);
a.Name = fileName;
a.ContentType = contentType;

insert a;

return a.Id;
}
}