function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
msimondsmsimonds 

Trigger were working fine, but now receiving errors - please help

I have two triggers:

  

 trigger CleanDRLineItem on Opportunity_Tracker_Products__c (before delete) {

    for (Integer i = 0; i < Trigger.old.size(); i++) {

        try {

            //replace OppLineId__c with actual refference name.

            List<Design_Registration__c> oDRs = [select id, DP_reg_id__c, OwnerID, Inactive__c from Design_Registration__c where Opp_tracker_line_ID__c = :trigger.old[i].id];

            for (Design_registration__c dr: oDRs)

            {

             dr.OwnerId = '00540000000oJjiAAE';

             dr.Inactive__c = true;

             string old_dr_number = dr.DP_reg_id__c;

             dr.DP_reg_id__c = 'IN-' + old_dr_number;

             update dr;

            }

            

            

        } catch (System.QueryException ex) {

            //Do Nothing - There must not have been any to delete.

        }

    }

}

 

 and 

 

 trigger CleanOppTrackerLineItem on Opportunity_Trackers__c (before delete) {

    for (Integer i = 0; i < Trigger.old.size(); i++) {

        

            //replace OppLineId__c with actual refference name.

            Opportunity_Tracker_Products__c[] oTRs = [select id from Opportunity_Tracker_Products__c where Opportunity_Tracker__c = :trigger.old[i].id];

            //System.Debug('Removing the following: '+oTRs.id);

            delete oTRs;

        

    }

}

  Now I am receiving errors and i do not understand why they are getting them, Here is the text of one of the emails I am receiving:

 

 

Apex script unhandled trigger exception by user/organization: 00540000000yFVO/00D300000006AVjCleanOppTrackerLineItem: execution of BeforeDeletecaused by: System.DmlException: Delete failed. First exception on row 0 with id a0B4000000282XlEAI; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CleanDRLineItem: execution of BeforeDeletecaused by: System.Exception: Too many SOQL queries: 21Trigger.CleanDRLineItem: line 5, column 49: []Trigger.CleanOppTrackerLineItem: line 7, column 13

 

 

 

 I would appreciate any feedback and help, 

 

Thanks,

Mike Simonds 

 

 

 

Message Edited by msimonds on 01-06-2010 07:40 PM
Best Answer chosen by Admin (Salesforce Developers) 
InsertWittyNameInsertWittyName

'dr' is the individual record, you want to update 'oDRs' outside of the loop.

 

As you have it now the trigger is at risk of hitting the governer limits.

 

 

All Answers

bob_buzzardbob_buzzard

This will be because you have SOQL queries inside the loop that is iterating the items in the trigger.  As soon as there are more than 20 items in the trigger, you will break the governer limit.

 

You will need to move your queries outside of the loop so that there is just one query across all items.

bob_buzzardbob_buzzard
Also, the limit of 20 SOQL calls is for the entire transaction, not just the triggers, so you could hit this with just a few items being deleted.
msimondsmsimonds

Thanks bob for the fast reply, I appreciate it.

 

 

I am kind of new to triggers and APEX, so I have another question:

 

  

with this trigger:

 

 

 trigger CleanOppTrackerLineItem on Opportunity_Trackers__c (before delete) 

{

    for (Integer i = 0; i < Trigger.old.size(); i++) 

    {

      Opportunity_Tracker_Products__c[] oTRs = [select id from Opportunity_Tracker_Products__c where Opportunity_Tracker__c = :trigger.old[i].id];

      delete oTRs;

        

    }

}

 

 

 

Let's say that there are 5 line items in the opportunity_tracker_products__c and I need to all of then in an array such as oTR's.  How can I do that if I do not use a loop?

 

Can you help me out here a newbie would really appreciate it

 

~Mike 

 

 

 

 

InsertWittyNameInsertWittyName

Hi Mike.

 

It can be quite confusing at first, but you'll get used to it pretty quickly!

 

Something like this (untested) should serve as a good base:

 

 

trigger CleanOppTrackerLineItem on Opportunity_Trackers__c (before delete) { // Create a list to hold all the ids List <Id> otIds = new List<Id>(); // Copy each record's id to the list for(Opportunity_Trackers__c ot in Trigger.old) { otIds.add(ot.id); } // Single query List <Opportunity_Tracker_Products__c> oTRs = [select id from Opportunity_Tracker_Products__c where Opportunity_Tracker__c in :otIds]; // Check we have results, then delete them all in a single query if(oTRs.size() > 0) { delete oTRs; } }

 

 

 

msimondsmsimonds

I tried to alter the trigger as follows:

 

 

trigger CleanOppTrackerLineItem2 on Opportunity_Trackers__c (before delete) 

{

List<Opportunity_Tracker_Products__c> opp_line_items = 

[Select id 

from Opportunity_Tracker_Products__c 

where Opportunity_Tracker__c IN :Trigger.new];

 

    delete opp_line_items;  

}

 

 

 

BUT that doesn't work either

 

Man I feel stupid here

 

 

~Mike 

msimondsmsimonds

Thank you very much for that, but I am getting an error on that, on line 7

 

unexpected token: ot

 

Thanks for the fast reply InsertWittyName, I appreciate any help or guidance

 

~Mike 

InsertWittyNameInsertWittyName

Apologies, small syntax error.

 

 

trigger CleanOppTrackerLineItem on Opportunity_Trackers__c (before delete) { // Create a list to hold all the ids List <Id> otIds = new List<Id>(); // Copy each record's id to the list for(Opportunity_Trackers__c ot : Trigger.old) { otIds.add(ot.id); } // Single query List <Opportunity_Tracker_Products__c> oTRs = [select id from Opportunity_Tracker_Products__c where Opportunity_Tracker__c in :otIds]; // Check we have results, then delete in a single query if(oTRs.size() > 0) { delete oTRs; } }

 

 

 

msimondsmsimonds

I figured that out sir and much appreciate it

 

It works, thanks so much

 

I have one more


This is a trigger to inactivate another custom object record which is associated with the opportunity_tracker_products__c called design_registration__c

 

 

This trigger works:

 

 

 

 

 

trigger CleanDRLineItem on Opportunity_Tracker_Products__c (before delete) { for (Integer i = 0; i < Trigger.old.size(); i++) { try { //replace OppLineId__c with actual refference name. List<Design_Registration__c> oDRs = [select id, DP_reg_id__c, OwnerID, Inactive__c from Design_Registration__c where Opp_tracker_line_ID__c = :trigger.old[i].id]; for (Design_registration__c dr: oDRs) { dr.OwnerId = '00540000000oJjiAAE'; dr.Inactive__c = true; string old_dr_number = dr.DP_reg_id__c; dr.DP_reg_id__c = 'IN-' + old_dr_number; update dr; } } catch (System.QueryException ex) { //Do Nothing - There must not have been any to delete. } }}

 but it will have issues on the governor hitting the limit

 

Do you think that I can do the same with this trigger as the one you posted above?  As you can see, I am setting a checkbox to true and adjusting some other fields, but it is inside a for loop

 

 

~mike

 

PS > Thanks so much for all the guidance 

 

 

InsertWittyNameInsertWittyName

Definitely. Populate a list of the record id's as we did above and query the database to pull all the records back in one go.

 

You can then use a for loop to update the values in each record returned, then outside of the loop perform the update.

 

Hope that makes sense.

Message Edited by InsertWittyName on 01-07-2010 12:46 PM
msimondsmsimonds
IT does and works!!  Your great!!!  
msimondsmsimonds

I did this and it works, but you said I should update the DR's outside the loop:

 

 

 

trigger CleanDRLineItem2 on Opportunity_Tracker_Products__c (before delete) { List <Id> otIds = new List<Id>(); // Copy each record's id to the list for(Opportunity_Tracker_Products__c ot: Trigger.old) { otIds.add(ot.id); } // Single query List <Design_Registration__c> oDRs = [select id, DP_reg_id__c, OwnerID, Inactive__c from Design_Registration__c where Opp_tracker_line_ID__c in :otIds]; for (Design_Registration__c dr: oDRs) { dr.OwnerId = '00540000000oJjiAAE'; dr.Inactive__c = true; string old_dr_number = dr.DP_reg_id__c; dr.DP_reg_id__c = 'IN-' + old_dr_number; update dr; } }

 

 

 I did it within the for loop and it works, when I place the update dr outside the loop I get an error on variable does not exist dr

 

I will leave it as is above since it works

 

 

 

Message Edited by msimonds on 01-07-2010 03:02 PM
InsertWittyNameInsertWittyName

'dr' is the individual record, you want to update 'oDRs' outside of the loop.

 

As you have it now the trigger is at risk of hitting the governer limits.

 

 

This was selected as the best answer
msimondsmsimonds

YUPPPPPP that did it

 

Thanks a ton, you are the hero of the day!!!

 

~Mike