apex
基础语法介绍
字段定义
Integer count = 0;
Decimal total = 18.89;
// 字符串必须使用单引号
String str = 'hello world';
// sObject
Account myAccount = new Account();
- Blob
- Boolean
- Date
- Datetime
- Decimal
- Double
- ID
- Integer
- Long
- Object
- String
- Time
List
有序列表,数组 方法介绍
List<Integer> nums = new List<Integer>();
List<String> strs = new List<String>{'a','b'};
// 嵌套
List<List<List<Integer>>> myLists = new List<List<List<Integer>>>();
// 使用基元或对象的一维列表时,可以使用数组符号来声明和引用
String[] strArr = new List<String>();
List<String> strArr = new String[1];
String[] strArr = new String[1];
// strArr.add()是弹性的,但是strArr[0]就不能数组越界
strArr.add(index, value);
strArr.add(value);
strArr[0] = 'test';
Set
无序列表,元素不可重复 方法介绍
// Set 的语法跟 List 类似
Set<Integer> uniqueNums = new Set<Integer>();
Map
key => value 字典,key为基础类型且不能重复 方法介绍
Map<Integer,String> = new Map<Integer,String>();
Map<String,Integer> = new Map<String,Integer>{'one' => 1, 'two' => 2};
Enums
public enum Season {SPRING, SUMMER, AUTUMN, WINTER}
Season currentSeason = Season.WINTER;
if (currentSeason == Season.SPRING) {
} else if (currentSeason == Season.WINTER) {
}
Constants
public class myCls {
static final Integer PRIVATE_INT_CONST = 200;
static final Integer PRIVATE_INT_CONST2;
public static Integer calculate() {
return 2 + 7;
}
static {
PRIVATE_INT_CONST2 = calculate();
}
}
判断
Integer a = 666;
if (a == 1) {
System.debug('if');
} else if (a == 2) {
System.debug('else if');
} else {
System.debug('else');
}
switch on a {
when 2 {
System.debug('case 1');
}
when 666,888 {
System.debug('case 2');
}
when else {
System.debug('default');
}
}
循环
Integer count = 0;
do {
count++;
System.debug(count);
} while(count < 10);
while(count < 10){
count++;
System.debug(count);
}
for (Integer i = 0; i < 10; i++) {
System.debug(i);
}
Integer[] myInts = new Integer[]{1,2,3,4,5,6,7};
for (Integer i : myInts) {
System.debug(i);
}
异常
public class MyException extends Exception {}
try {
throw new MyException('需要继承一下');
} catch(Exception e) {
System.debug('捕获异常' + e.getMessage());
}
日志
设置 -> 调试日志 -> 新建 -> 按需配置 -> 保存
System.debug("日志内容");
常用用户: 微信用户 来宾用户
前端加按钮触发
针对classic模式下的,列表视图复选,然后点击"批量测试"按钮请求到APEX服务
找到相关对象 -> 新按钮或链接
/*
标签: 批量测试
名称: batch_test
显示类型: 列表按钮,并勾选显示复选框
行为: 执行JavaScript
内容源: OnClick JavaScript
输入以下JavaScript代码,并点击检查语法,然后保存
*/
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
// 获取所有选中的记录ID
var selectedIds = {!GETRECORDIDS($ObjectType.test__c)};
if (selectedIds.length == 0) {
alert("请至少选择一个测试记录!");
} else {
if(confirm("您确定要同步所选的记录吗?")) {
var result = sforce.apex.execute('ApexAService', 'batchTest', { ids: selectedIds });
alert(result);
}
}
找到对应列表视图 -> 编辑 -> 可用按钮 -> 添加到后侧
Apex代码
global with sharing class ApexAService {
webservice static Boolean batchTest(List<String> ids) {
try {
List<test__c> t = [SELECT Id, Code__c FROM test__c WHERE Id IN :ids];
System.debug(t);
return true;
} catch (Exception e) {
System.debug('批量测试失败:' + e.getMessage());
return false;
}
}
}