You need to sign in to do that
Don't have an account?
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
'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
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.
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
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; } }
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
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
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; } }
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
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.
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
'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.
YUPPPPPP that did it
Thanks a ton, you are the hero of the day!!!
~Mike