You need to sign in to do that
Don't have an account?

records are compared to all set values by trigger when using data loader
Hi,
I am attempting to pull a value from an object called Est__c and place it into an Opportunity field Est_Cap__c. The value is based on what the user puts into the C__c, N__c, and P__c fields from the Opportunity. The trigger works fine when updating and adding individual Opportunities but fails when using data loader. Looking at the debug logs I see that trigger is comparing each record to every set from each record. For example, Record 1 has N__c = 2 and Record 2 has N__c = 3. When run through data loader Record 1's N__c field is compared to 2 and 3 (resulting from the list) and fails to update. Does anyone know anyway to get thoes distinct values (C__c, N__c, and P__c) so Record 1 is only being compared with N__c = 2 rather than N__c = 2, 3? I am trying to maintain best practices but keeping the list out of the loop. My code is below:
trigger
OpportunityEstCap onOpportunity (beforeinsert, beforeupdate)
{
set<String> setISO = newset<String>();
set<Decimal> setMonth = newset<Decimal>();
set<String> setType = newset<String>();
Integer j = 0;
for (Opportunity oOpp : Trigger.new)
{
setISO.add(oOpp.C__c);
setMonth.add(oOpp.N__c);
setType.add(oOpp.P__c);
}
List <Est__c> listEst = [
SELECTID, C__c, Pr__c, Months__c, Cap__c
FROMEstimated_Capital_Adder__cWHERE C__c IN:setISO
AND Months__c IN:setMonth
AND Pr__c IN:setType];
system.debug('listEst: '+listEst);
system.debug('listEst.size(): '+listEst.size());
if ((Trigger.new[j].StageName != 'Terminated' && Trigger.new[j].StageName != 'Closed Won' && Trigger.new[j].StageName != 'Closed Lost') && Trigger.new[j].Comm__c == 'Test')
{
for (Integer i = 0; i <Trigger.new.size(); i++)
{
if(listEst.size() > 0)
{
for(Est__c oEst : listEst)
{
// set new Est_Cap__cif (Trigger.new[i].C__c == oEst.C__c && Trigger.new[i].N__c == oEst.Months__c && Trigger.new[i].P__c == oEst.Pr__c)
{
Trigger.
new[i].Est_Cap__c = oEst.Cap__c;
}
//if (Trigger.new[i].C__c == oEst.C__c && Trigger.new[i].N__c == oEst.Months__c && Trigger.new[i].P__c == oEst.Pr__c)system.debug('Trigger.new[i].id: '+Trigger.new[i].id);
system.debug('Trigger.new[i].C__c: '+Trigger.new[i].C__c);
system.debug('oEst.C__c: '+oEst.C__c);
system.debug('Trigger.new[i].N__c: '+Trigger.new[i].N__c);
system.debug('oEst.Months__c: '+oEst.Months__c);
system.debug('Trigger.new[i].P__c: '+Trigger.new[i].P__c);
system.debug('oEst.Pr__c: '+oEst.Pr__c);
if (Trigger.new[i].C__c != oEst.C__c || Trigger.new[i].N__c != oEst.Months__c || Trigger.new[i].P__c != oEst.Pr__c)
{
Trigger.
new[i].Est_Cap__c.adderror('Combonation of ' + Trigger.new[i].C__c + ', '+ Trigger.new[i].N__c+', and ' + Trigger.new[i].P__c + ' is not supported. Please contact your administrator.');
}
//if (Trigger.new[i].C__c != oEst.C__c && Trigger.new[i].N__c != oEst.Months__c && Trigger.new[i].P__c != oEst.Pr__c)
}
//for (Estimated_Cap__c oEst : listEst)
}
//if (listEst.size() > 0)else
{
trigger.new[i].Est_Cap__c.adderror('Combonation of ' + Trigger.new[i].C__c + ', '+ Trigger.new[i].N__c+', and ' + Trigger.new[i].P__c + ' is not supported. Please contact your administrator.');
}
//else
}
//for (Integer i = 0; i <Trigger.new.size(); i++)
}
//if (oOpp.StageName != 'Terminated' && oOpp.StageName != 'Closed Won' && oOpp.StageName != 'Closed Lost' && oOpp.Comm__c == 'Test')
}
Try using Map in your code in place of Set.Please go through the below link to know more :-
http://blogs.developerforce.com/developer-relations/2011/04/apex-tip-using-maps-to-reduce-soql-calls.html
I am pretty new to apex and can't seem to get the maps working. Do you have anything else I could look at?
I would say you should go through the documentation for Map methods in salesforce which is below :-
http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_collections_maps.htm
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_map.htm