trigger trg_OpportunityClone on Opportunity (after insert)
{
set<String> stateRecords = new set<String>();
map<String, list<Dodge_State__c>> dodgeRecordMap = new map<String, list<Dodge_State__c>>();
list<Dodge_State__c> dodgeList = new list<Dodge_State__c>();
CLS_OpportunityClone obj_CLS_OpportunityClone = new CLS_OpportunityClone();
if(triggerrecursionPrevention.getVal() == false)
{
triggerrecursionPrevention.setVal();
if(trigger.isAfter && trigger.isInsert)
{
if(trigger.new!=trigger.old)
{
RecordType recType = [Select SobjectType, Name, Id From RecordType where SobjectType = 'Opportunity' and Name = 'MHC Dodge Project' limit 1];
System.debug('>>>>>>>>'+recType);
for(Opportunity o: trigger.new)
{
stateRecords.add(o.MHC1__State__c);
}
list<Dodge_State__c> dodgeStateRecords = new list<Dodge_State__c>([Select Name, Id, IsPartner__c, Assigned_User__c From Dodge_State__c where Name in : stateRecords]);
for(String s : stateRecords)
{
String stateName = s;
for(Integer i=0;i<dodgeStateRecords.size();i++)
{
if(stateName == dodgeStateRecords.get(i).Name)
{
dodgeList.add(dodgeStateRecords.get(i));
}
}
dodgeRecordMap.put(stateName, dodgeList);
dodgeList = new list<Dodge_State__c>();
}
System.debug('TheDodge Record Map>>'+dodgeRecordMap);
for(Opportunity o : trigger.new)
{
if(o.RecordTypeId == recType.Id)
{
list<Dodge_State__c> tempDodgeRecords = dodgeRecordMap.get(o.MHC1__State__c);
System.debug('Dodge Records----->>>'+tempDodgeRecords);
obj_CLS_OpportunityClone.createOpportunity(o, tempDodgeRecords);
}
}
}
}
}
}
public class CLS_OpportunityClone
{
public void createOpportunity(Opportunity Opp, list<Dodge_State__c> dodgeStateList)
{
list<Opportunity> oppRecordsInsert = new list<Opportunity>();
list<MHC1__Project_Bidder__c> ProjBinderInsert = new list<MHC1__Project_Bidder__c>();
list<MHC1__Project_Firm__c> ProjFirmInsert = new list<MHC1__Project_Firm__c>();
Opportunity Oppty = new Opportunity(Id=Opp.Id);
for(Integer i=0;i<dodgeStateList.size();i++)
{
if(dodgeStateList.get(i).IsPartner__c == true)
{
Opportunity opportunityRecord = Opp.clone(false,true);
opportunityRecord.OwnerId = dodgeStateList.get(i).Assigned_User__c;
oppRecordsInsert.add(opportunityRecord);
}
else
{
Oppty.OwnerId = dodgeStateList.get(i).Assigned_User__c;
}
System.debug('<<<<Opportunity records>>>>>'+oppRecordsInsert);
}
if(oppRecordsInsert.size()>0)
{
insert oppRecordsInsert;
System.debug('#########Inserted Opportunity records>>>>>>'+oppRecordsInsert);
if(oppRecordsInsert.size() == 1)
{
Oppty.Partner_Opportunity_cloned_ID_1__c = oppRecordsInsert[0].Id;
}
if(oppRecordsInsert.size() == 2)
{
Oppty.Partner_Opportunity_cloned_ID_1__c = oppRecordsInsert[0].Id;
Oppty.Partner_Opportunity_cloned_ID_2__c = oppRecordsInsert[1].Id;
}
if(oppRecordsInsert.size() >= 3)
{
Oppty.Partner_Opportunity_cloned_ID_1__c = oppRecordsInsert[0].Id;
Oppty.Partner_Opportunity_cloned_ID_2__c = oppRecordsInsert[1].Id;
Oppty.Partner_Opportunity_cloned_ID_3__c = oppRecordsInsert[2].Id;
}
update Oppty;
}
list<MHC1__Project_Bidder__c> projectBinderRecords = new list<MHC1__Project_Bidder__c>([Select Name, MHC1__Type__c, MHC1__Trade__c, MHC1__Sub_Project_Code__c, MHC1__Sub_Project_2__c, MHC1__Opportunity__c, MHC1__Low_Bidder__c, MHC1__Contact__c, MHC1__Bidder__c, MHC1__Bid_Withdrawn__c, MHC1__Bid_Awarded__c, MHC1__Bid_Amount__c, CurrencyIsoCode, ConnectionSentId, ConnectionReceivedId From MHC1__Project_Bidder__c where MHC1__Opportunity__c =: Opp.Id]);
list<MHC1__Project_Firm__c> projectFirmRecords = new list<MHC1__Project_Firm__c>([Select Name, MHC1__Project_Opportunity__c, MHC1__Project_Firm__c, MHC1__Firm_Role__c, MHC1__Contact__c, MHC1__Awarded__c, CurrencyIsoCode, ConnectionSentId, ConnectionReceivedId From MHC1__Project_Firm__c where MHC1__Project_Opportunity__c =: Opp.Id]);
for(Integer i=0;i<oppRecordsInsert.size();i++)
{
if(projectBinderRecords.size()>0)
{
for(Integer j=0;j<projectBinderRecords.size();j++)
{
MHC1__Project_Bidder__c ProjBinder = projectBinderRecords.get(j).clone(false,true);
ProjBinder.MHC1__Opportunity__c = oppRecordsInsert.get(i).Id;
ProjBinderInsert.add(ProjBinder);
}
}
if(projectFirmRecords.size()>0)
{
for(Integer k=0;k<projectFirmRecords.size();k++)
{
MHC1__Project_Firm__c ProjFirm = projectFirmRecords.get(k).clone(false,true);
ProjFirm.MHC1__Project_Opportunity__c = oppRecordsInsert.get(i).Id;
ProjFirmInsert.add(ProjFirm);
}
}
}
if(ProjBinderInsert.size()>0)
{
insert ProjBinderInsert;
}
if(ProjFirmInsert.size()>0)
{
insert ProjFirmInsert;
}
}
public static testmethod void test_OpportunityTrigger()
{
RecordType recType = [Select SobjectType, Name, Id From RecordType where SobjectType = 'Opportunity' and Name = 'MHC Dodge Project'];
Account acc = [Select Id from Account limit 1];
Opportunity opp = new Opportunity(AccountId = acc.Id, MHC1__State__c = 'AL', RecordTypeId = recType.Id, name='testOpp', stageName='Open',CloseDate=Date.newInstance(2000,08,10), Type='New Customer');
insert opp;
/*MHC1__Project_Bidder__c ProBidder = new MHC1__Project_Bidder__c(Name='test', MHC1__Opportunity__c = opp.Id);
insert ProBidder;
MHC1__Project_Firm__c projFirm = new MHC1__Project_Firm__c(Name = 'test', MHC1__Project_Opportunity__c = opp.Id);
insert projFirm;*/
}
}
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:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
by: Bill-HansonPosted on 2009-10-08 at 07:18:51ID: 25525673
Without seeing the error message, it's kind of hard to pin-point, but I'm guessing you are hitting either the 20 DML/SOQL query limit or the 1,000 SOQL result limit.
Placing DML/SOQL in a loop always presents this type of problem. The solution is usually to use SOQL For-Loops (see apex documentation) or to combine multiple DML/SOQL calls into a single statement.
Sometimes, I'll loop over records and cache any data I need into a Map keyed by record Id. Then, I use this cached data to build a large DML/SOQL statement that returns all data for all records, which I then store in another Map (also keyed by record Id.) Then, I loop over both maps, joining the data as needed by the class. Sometimes I can keep my code down to a single select statement per object type doing it this way.
Please post the specific error message and I may be able to provide a better answer.