-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDML&Database Methods
More file actions
84 lines (61 loc) · 3.25 KB
/
Copy pathDML&Database Methods
File metadata and controls
84 lines (61 loc) · 3.25 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
*****************执行操作过程中的错误信息的收集以及操作******************************************
// Create a list of contacts
List<Contact> conList = new List<Contact> {
new Contact(FirstName='Joe',LastName='Smith',Department='Finance'),
new Contact(FirstName='Kathy',LastName='Smith',Department='Technology'),
new Contact(FirstName='Caroline',LastName='Roth',Department='Finance'),
new Contact()};
// Bulk insert all contacts with one DML call
Database.SaveResult[] srList = Database.insert(conList, false);
//SaveResult这个数组中存放的是每一个插入操作之后系统返回的信息
// Iterate through each returned result
for (Database.SaveResult sr : srList) {
if (sr.isSuccess()) {
// Operation was successful, so get the ID of the record that was processed
System.debug('Successfully inserted contact. Contact ID: ' + sr.getId());
} else {
// Operation failed, so get all errors
for(Database.Error err : sr.getErrors()) {
System.debug('The following error has occurred.');
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Contact fields that affected this error: ' + err.getFields());
}
}
}
*****************************父对象中选择子对象*****************************************************************
SELECT Id,name,(select Phone from contacts) FROM Account WHERE Name='Test Trigger2'
*****************************子对象中选择父对象***************************************************************
SELECT Id,Account.Id,Account.Industry from Contact where Phone='111222333'
*****************************利用 SobjectType 来确定要查询的对象*************************************************
public static RecordType rd = [select Id,Name from RecordType where DeveloperName='BG' and SobjectType='Project_Publicity__c'];
*************************** Bad Way 不要在for 循环中使用DML语句,很容易达到governor limits****************************
public with sharing class AccountHandler {
public static void CreateNewOpportunity(List<Account> accts) {
for (Account a : accts) {
Opportunity opp = new Opportunity();
opp.Name = a.Name + ' Opportunity';
opp.AccountId = a.Id;
opp.StageName = 'Prospecting';
opp.CloseDate = System.Today().addMonths(1);
insert opp;
}
}
}
**************************** Good Way ******************************************************************************
***************************因为系统可以一次性处理200条数据,所以要充分利用这个特性***************************************
public with sharing class AccountHandler {
public static void CreateNewOpportunity(List<Account> accts) {
List<Opportunity> opps = new List<Opportunity>();
for (Account a : accts) {
Opportunity opp = new Opportunity();
opp.Name = a.Name + ' Opportunity';
opp.AccountId = a.Id;
opp.StageName = 'Prospecting';
opp.CloseDate = System.Today().addMonths(1);
opps.add(opp);
}
if (opps.size() > 0) {
insert opps;
}
}
}