+ Start a Discussion

Change Trigger from Delete to update > need help

I have a custom object called opportunity_tracker_products__c that is associated to another custom object called design_registration__c.


Currently there is a trigger in place that will delete any DR's that have an opp_tracker_product.id on them.


Here is the trigger:


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. Design_Registration__c[] oDRs = [select id from Design_Registration__c where Opp_tracker_line_ID__c = :trigger.old[i].id]; delete oDRs; } catch (System.QueryException ex) { //Do Nothing - There must not have been any to delete. } }}

 What I need to do is to not delete the records but update a boolean field and set the OwnerId to a specific user on the design_registration__c 


this is what i have so far:



trigger CleanDRLineItem on Opportunity_Tracker_Products__c (before delete) { List<Design_registration__c> updateDRs = [select dr.id, dr.Inactive__c, OwnerId from Design_Registration__c dr where Opp_tracker_line_ID__c IN :Trigger.new FOR UPDATE]; for(Design_Registration__c dr: updateDRs) { dr.Inactive__c = true; dr.OwnerId = '00540000000oJjiAAE'; } update updateDRs;}




 but I am getting an error 



Save error: Invalid bind expression type of SOBJECT:Opportunity_Tracker_Products__c for column of type String


 I am new to triggers and APEX (as I have stated in prior messages) and need help please.  I know that I am visiably doing something wrong, I would appreciate any feedback or ideas 









You have Trigger.new in your code but that's not available in a Before Delete trigger.   You need to use Trigger.old


Also, I noticed in your old code that there is a select statement inside a for-loop.  That's not best practice and you can hit governor limits that way.   See documentation or other postings on how to write "bulk triggers".