• Franco D
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 3
    Replies
Hello all,

I am new to Apex Code and I am trying to build a trigger to the following task.
Each time we insert new Opportunities throug a bulk insert, we have Opportunites in the system that are listed as scheduled, that are equal or same as Opportunities Won. So I want my trigger to delete the scheduled Opportunity that is equal to the Won Opportunity.

When the Opportunity is inserted it will check three fields that need to all match for Opportunity scheduled to be deleted.
StageName = "Peformed" Which is always the case when inserting new bulk Opportunites 
Contact_ID = "Contact_ID"  Which is the first field it should look for to find a duplicate Opportunity.
ClosedDate = "Date" Which is the date we enter for the Opportunity that will match the scheduled Opportunity
 
Example Giving: New Opportunity Inserted: 
StageName = Performed 
ContactID = John Doe
ClosedDate = 2/13/2017

Found Scheduled Opportunity to be deleted:
Stage Name = Scheduled
ContactID = John Doe
ClosedDate = 2/13/2017

Here is what i have so far: 

trigger myTrigger on Opportunity (before insert) {

    for (Opportunity opp : Trigger.new){
    
        if (opp.StageName = 'Performed'){
        
        Opportunity Opp = new Opportunity();
        
        Opp.StageName      = 'Surgery Performed';
        Opp.ContactID         = 'Check for same ContactID;
        Opp.ClosedDate      =  'Check for same ClosedDate;
       
        delete opp;
        }
    }
}

Sorry for my ignorance and lack of knowledge in this topic, i know this may seem very silly to ask, but I am desperate to know if this can be done by a trigger and not to be manually searched to find a duplicate to delete.

Thank you in advance.
Hello all,

I am new to Apex Code and I am trying to build a trigger to the following task.
Each time we insert new Opportunities throug a bulk insert, we have Opportunites in the system that are listed as scheduled, that are equal or same as Opportunities Won. So I want my trigger to delete the scheduled Opportunity that is equal to the Won Opportunity.

When the Opportunity is inserted it will check three fields that need to all match for Opportunity scheduled to be deleted.
StageName = "Peformed" Which is always the case when inserting new bulk Opportunites 
Contact_ID = "Contact_ID"  Which is the first field it should look for to find a duplicate Opportunity.
ClosedDate = "Date" Which is the date we enter for the Opportunity that will match the scheduled Opportunity
 
Example Giving: New Opportunity Inserted: 
StageName = Performed 
ContactID = John Doe
ClosedDate = 2/13/2017

Found Scheduled Opportunity to be deleted:
Stage Name = Scheduled
ContactID = John Doe
ClosedDate = 2/13/2017

Here is what i have so far: 

trigger myTrigger on Opportunity (before insert) {

    for (Opportunity opp : Trigger.new){
    
        if (opp.StageName = 'Performed'){
        
        Opportunity Opp = new Opportunity();
        
        Opp.StageName      = 'Surgery Performed';
        Opp.ContactID         = 'Check for same ContactID;
        Opp.ClosedDate      =  'Check for same ClosedDate;
       
        delete opp;
        }
    }
}

Sorry for my ignorance and lack of knowledge in this topic, i know this may seem very silly to ask, but I am desperate to know if this can be done by a trigger and not to be manually searched to find a duplicate to delete.

Thank you in advance.