-
Notifications
You must be signed in to change notification settings - Fork 8
/
first error: SELF_REFERENCE_FROM_TRIGGER
73 lines (55 loc) · 2.67 KB
/
first error: SELF_REFERENCE_FROM_TRIGGER
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
/* error message: first error: SELF_REFERENCE_FROM_TRIGGER
complete error:
Error:Apex trigger AccountTrigger caused an unexpected exception, contact your administrator: AccountTrigger: execution of BeforeUpdate
caused by: System.DmlException: Update failed. First exception on row 0 with id 0015g000007naNoAAI;
first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 0015g000007naNo) is currently in trigger AccountTrigger,
therefore it cannot recursively update itself: []: Trigger.AccountTrigger: line 20, column 1
*/
Requirment: when account is updated, put latest opportunity's type
=========================code throwing error==============================
trigger AccountTrigger on Account (before update) {
list<Account> UpdatelstAcc= new list<Account>();
list<Account> lstAcc= new list<Account>();
for(Account acc:trigger.new){
if(acc!=trigger.oldmap.get(acc.Id)){
lstAcc.add(acc);
}
}
if(lstAcc.size()>0){
for(Opportunity opp:[select Id, Account.Id, Account.Description, Type from Opportunity
where Account.Id=:lstAcc ORDER BY CreatedDate Limit 1]){
system.debug('@@@@ opp: '+opp.Account.Description);
opp.Account.Description=opp.Type;
system.debug('@@@@ updated account: '+opp.Account.Description);
UpdatelstAcc.add(opp.Account);
}
}
if(UpdatelstAcc.size()>0){
update UpdatelstAcc;
}
integer DMLCount= Limits.getDmlRows();
integer DMLRows=Limits.getDmlStatements();
integer SOQLCount=Limits.getQueries();
system.debug('@@@@: '+DMLCount +''+DMLRows +''+SOQLCount);
}
=============Solution explained: https://help.salesforce.com/s/articleView?id=000324391&type=1 ===============================
========>>>>>Solution in below code:<<<<<==============================
trigger updateAccount on Account(before update){
set<string> setAcc = new set<string>();
map<string,Opportunity> mapUp= new map<string,Opportunity>();
for(Account ac:trigger.new){
if(ac!=trigger.oldmap.get(ac.Id)){
setAcc.add(ac.Id);
}
if(setAcc.size()>0){
for(Opportunity opp:[select Id, Type, Account.Id, Account.Description,CreatedDate from Opportunity Where
Account.Id=:setAcc Order By CreatedDate DESC Limit 1]){
system.debug('@@@ opp.Account.Description: '+opp.Account.Description);
system.debug('@@@ opp.Type: '+opp.Type);
mapUp.put(opp.AccountId,opp);
}
for(Account acc:trigger.new){
acc.Description=mapUp.get(acc.Id).Type;
}
}
}}