• Lumina Software
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    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.
I'm having a difficult time finding the answer to this question.

I coded up a before insert trigger that checks both the insertion set (all leads being currently inserted) as well as existing Leads+Contacts to see if the leads trying to be inserted already exist or are duplicated in the insertion set. The check is a cross-check on their email or alt-email for the insertion set itself, Leads, and Contacts.

I prevent the insertion of duplicate leads by adding an error when a duplicate is found (either in the insertion set, or already existing Lead/Contact in Salesforce), but doing this unfortunately rolls back the insert preventing that lead from being inserted and trying to insert again without that lead (as Salesforce does). This does not work for our purposes because ultimately if 2 leads insert at the same time (in the same insertion set) from our web insert they should merge into 1, not simply stop the second insert.

Instead of removing the duplicated lead from being inserted entirely, in cases where the existing Lead/Contact can be updated, or 2 leads are being inserted in this insertion set that are duplicated and can be merged I would like to update / merge the Leads, but I can't do that if it rolls back the insertion of all leads when I error the insert of 1 lead.

In short:

How can I prevent insertion of a single Lead in my insertion set without it rolling back and restarting the trigger (removing the lead from the insertion set and running the insert again) so that in the trigger itself I can either A: merge the lead with the other lead in the insertion set which is a duplicate, or B: update the existing Lead / contact with the new information.

Right now I'm thinking that this isn't possible and instead I should allow the lead to insert but set some sort of flag on it that says "Duplicate of LEAD_ID / CONTACT_ID" and then run a trigger after insert that merges the duplicated lead with the existing/duplicated-in-set lead/contact. Would that work? Is there a cleaner way of doing this? What am I not thinking about / should I keep in mind with this insertion logic?

Any assistance here is much appreciated. Thanks!