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 to delete record from Custom Object : Help Needed

I need some help and I am only asking this because I am new to APEX Triggers:

On the opportunity line item, we have a Picklist.  When this picklist value is changed to "Won"

My organization has a process which does the following:

1) On the opportunity line item the value is changed to "Won"

2) A Work flow kicks off that sends an outbound message to our local server

3) This process creates a design win on a custom object called Design_Win__c based off of the opportunity line item that had the value changed.  It creates a 1 to 1 relationship with the Opportunity Line Item to Design Win


One process that we allow (currently disabled, but cannot be) is the changing of a part number on that same opportunity line item (this is done by a custom s-control).  When the part number is changed, it deletes and creates a new opportunity line item.  This assigns a new Id to the line item.

We store the original line item Id on the Design Win that was created, but when the original line item deleted, it breaks the 1 to 1 relationship

What I need to accomplish and need help with, is to write a trigger (if this is possible):

1) when the part is changed, and deletes, on the opportunity line item

2) it would query the design win object using the opp line item Id that we have stored on the Design Win.

3) if there is a record found that matches the query, then delete the record

Can someone please help me here, I have no idea how to write a trigger to do this

Thanks is advance

~Mike

SiriusBlackOpSiriusBlackOp
Code:
trigger CleanDesignWin on OpportunityLineItem (before delete) {
    for (Integer i = 0; i < Trigger.old.size(); i++) {
        try {
            //replace OppLineId__c with actual refference name.
            Design_Win__c[] oDWs = [select id from Design_Win__c where OppLineId__c = :trigger.old[i].id];
            delete oDWs;
        } catch (System.QueryException ex) {
            //Do Nothing - There must not have been any to delete.
        }
    }
}

 
Something like that should work.  Make sure you replace the "OppLineId__c" with the actual name of the refference to the Opportunity Line Items.
msimondsmsimonds
Perfect Sir!!  Works like a charm! I REALLY appreciate it

~Mike
SiriusBlackOpSiriusBlackOp
You are welcome!  Glad it worked for you.  I'm glad there weren't any type-Os in there because I just typed it straight into this site's code text area. LOL!  It's always fun when stuff just works. :smileywink:
 
Later.
Sebastien ColladonSebastien Colladon
Hello,

You should not do SOQL in a loop especially in trigger context as it can have up to 200 records to treat in the same time and the SOQL limit is 100.

You should do that instead :
trigger CleanDesignWin on OpportunityLineItem (before delete) {
  List<Design_Win__c> aDWl = [select id from Design_Win__c where OppLineId__c = :trigger.oldmap.keyset()];
  if(!aDWl.isEmpty()){
    delete aDWl;
  }
}

Best wishes,