• N _Rod
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies

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')

 

}

  • February 12, 2013
  • Like
  • 0

Hi,

 

The trigger below was created to update all records on the Opportunity when Field_1__c on Object__c is updated.  The trigger works fine when updates are done on the detail page but fails when updating more than 1value using data loader.  I receive the error:  List index out of bounds.  Any help would be greatly appreciated.  Code copied below.

 

Thank you,

 

trigger EstimatedUpdateOnOpp on Estimated_Capital_Adder__c (before update)
{


     list<Opportunity> listUpdate = new list<Opportunity>();
     list<Opportunity> listOppUpdate = new list<Opportunity>();
     Integer j = 0;
     Integer i = 0;

     for(Object__c obj : Trigger.new)
     {
         if (trigger.new[j].Field_1__c != trigger.old[j].Field_1__c)
         {
            set<string> setI = new set<string>();
            set<Decimal> setM = new set<Decimal>();
            set<string> setP = new set<string>();

            

                    setI.add(obj.Field_I__c);
             setM.add(obj.Field_M__c);
             setP.add(obj.Field_P__c);

         
             listOppUpdate = [SELECT id,
                                     Field_I_2__c,
                                     Field_M_2__c,
                                     Field_P_2__c,
                                     Field_E__c,
                                     StageName
                               FROM Opportunity
                              WHERE Field_I_2__c IN :setI
                                AND Field_M_2__c IN :setM
                                AND Field_P_2__c IN :setP
                                AND (StageName != 'Won' AND StageName != 'Lost' AND StageName != 'Terminated')
                                AND Field_Co__c = 'Prod'];
            
            

        
            for(Opportunity opp : listOppUpdate)
            {
      
                Opportunity Updates = new Opportunity (Id = listOppUpdate[i].Id,
                                                       Field_E__c = trigger.new[j].Field_1__c
                                                       );
                listUpdate.add(Updates);
            i++;         
            } // for(Opportunity opp : listOppUpdate)
            
           update listUpdate;    
           system.debug('listUpdate:  '+listUpdate);
           
           
                          
         }
             
    j++;
     }

}

 

  • January 28, 2013
  • Like
  • 0

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')

 

}

  • February 12, 2013
  • Like
  • 0

Hi,

 

The trigger below was created to update all records on the Opportunity when Field_1__c on Object__c is updated.  The trigger works fine when updates are done on the detail page but fails when updating more than 1value using data loader.  I receive the error:  List index out of bounds.  Any help would be greatly appreciated.  Code copied below.

 

Thank you,

 

trigger EstimatedUpdateOnOpp on Estimated_Capital_Adder__c (before update)
{


     list<Opportunity> listUpdate = new list<Opportunity>();
     list<Opportunity> listOppUpdate = new list<Opportunity>();
     Integer j = 0;
     Integer i = 0;

     for(Object__c obj : Trigger.new)
     {
         if (trigger.new[j].Field_1__c != trigger.old[j].Field_1__c)
         {
            set<string> setI = new set<string>();
            set<Decimal> setM = new set<Decimal>();
            set<string> setP = new set<string>();

            

                    setI.add(obj.Field_I__c);
             setM.add(obj.Field_M__c);
             setP.add(obj.Field_P__c);

         
             listOppUpdate = [SELECT id,
                                     Field_I_2__c,
                                     Field_M_2__c,
                                     Field_P_2__c,
                                     Field_E__c,
                                     StageName
                               FROM Opportunity
                              WHERE Field_I_2__c IN :setI
                                AND Field_M_2__c IN :setM
                                AND Field_P_2__c IN :setP
                                AND (StageName != 'Won' AND StageName != 'Lost' AND StageName != 'Terminated')
                                AND Field_Co__c = 'Prod'];
            
            

        
            for(Opportunity opp : listOppUpdate)
            {
      
                Opportunity Updates = new Opportunity (Id = listOppUpdate[i].Id,
                                                       Field_E__c = trigger.new[j].Field_1__c
                                                       );
                listUpdate.add(Updates);
            i++;         
            } // for(Opportunity opp : listOppUpdate)
            
           update listUpdate;    
           system.debug('listUpdate:  '+listUpdate);
           
           
                          
         }
             
    j++;
     }

}

 

  • January 28, 2013
  • Like
  • 0