一、同步调用的异常处理:AJAX Toolkit try catch...
<html>
<head>
<script src="/soap/ajax/44.0/connection.js" type="text/javascript"></script>
<script>
function setupPage() {
var output = document.getElementById("output");
var startTime = new Date().getTime()
try {
var queryResult = sforce.connection.query("Select Id, Name, Industry From
Account order by Industry limit 30");
layoutResults(queryResult, output, startTime);
} catch(error) {
queryFailed(error, output);
}
}
function queryFailed(error, out) {
out.innerHTML = "<font color=red>An error has occurred:</font> <p>" + error;
}
function layoutResults(queryResult, out, startTime) {
var timeTaken = new Date().getTime() - startTime;
if (queryResult.size > 0) {
var output = "";
var records = queryResult.getArray('records');
for (var i = 0; i < records.length; i++) {
var account = records[i];
output += account.Id + " " + account.Name + " [Industry - "
+ account.Industry + "]<BR>";
}
out.innerHTML = output + "<BR> query complexted in: " + timeTaken + " ms.";
} else {
out.innerHTML = "No records matched.";
}
}
</script>
</head>
<body οnlοad="setupPage()">
div id="output"></div>
</body>
</html>
二、VFPage AJAX Toolkit 中的数据迭代形式
var result = sforce.connection.query("select id, name from account limit 10");
var it = new sforce.QueryResultIterator(result);
while (it.hasNext()) {
var account = it.next();
sforce.debug.log(account.Name);
}
三、AJAX Toolkit 中字符串单引号的使用方式
Bob's B-B-Q
Apex 中使用方式:SELECT ID from ACCOUNT WHERE Name LIKE 'Bob\'s B-B-Q%'
Ajax:SELECT ID from ACCOUNT WHERE Name LIKE 'Bob\\'s B-B-Q%'
四、使用Base64二进制编码字符串:例子待补充
引用放到static中js 要用:<apex:includeScript value="{!URLFOR($Resource.majac_static, 'js/common.js')}"/>
<apex:page >
<html>
<head>
<script type="text/javascript"
src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>
<script src="/soap/ajax/44.0/connection.js"></script>
<script>
function setup() {
var document_ta = document.getElementById("document-ta");
sforce.connection.query("select name, id from document limit 1",
{onSuccess : querySuccess,
onFailure : function(error, doc_ta) {
doc_ta.value = "Oops something went wrong: " + error;
},
source: document_ta});
}
function querySuccess(result, doc_ta) {
var records = result.getArray("records");
if (records.length == 1) {
dojo.io.bind({
url: "/servlet/servlet.FileDownload?file=" + records[0].Id,
load: loadDocument});
} else {
doc_ta.value = "no records found";
}
}
function loadDocument(type, data, event) {
var document_ta = document.getElementById("document-ta");
document_ta.value = data;
}
</script>
</head>
<body οnlοad="setup()">
<textarea id="document-ta" cols="80" rows="20">
</textarea>
</body>
</html>
</apex:page >
五、异步调用的超时参数:如果一个异步调用在适当的时间内没有完成,我们可以结束这个调用。要做到这一点,请在任何异步调用的回调部分中指定超时参数。
var account = new sforce.SObject("Account");
account.Name = "my new account";
sforce.connection.create([account], {onSuccess: print, onFailure: printerr, timeout: 100});
六、Ajax 委托给Remote Site
新建一个一条 remoete site:
sforce.connection.remoteFunction({
url : "https://th-apex-http-callout.herokuapp.com",
onSuccess : function(response) {
alert("result" + response);
}
});