1

Here is my code. This will create 2 ops instead of just 1. It appears to create one before update and one after. I cannot figure out WHY!

trigger partnerOpClone on Opportunity (before update) {
Opportunity[] oppsToClone = new Opportunity[]{};
Opportunity[] oppsToSave = new Opportunity[]{};
for (Opportunity record:trigger.new)
{
    if (record.StageName == '09-Win' && trigger.oldMap.get(record.Id).StageName !=  '09-Win'
    && record.Type == 'Reseller / Partner Registration')
    oppsToClone.add(record);
}
if (oppsToClone.size() > 0)
{
    for (Opportunity record:oppsToClone)
    {
        Opportunity theClone = record.clone(false,false);
        theClone.Amount = null;
        theClone.Name = theClone.Name + ' Partner'; 
        theClone.StageName = 'Training';
        oppsToSave.add(theClone);
    }

    insert oppsToSave;
}

}

2 Answers 2

1

Do you have any other triggers on opportunity? Or perhaps a workflow rule that utilizes a field update on an Opportunity field?

2
  • +1 @Merry you need to see other triggers or workflows. I would strongly suggest using any of trigger template to manage multiple triggers on same objects with ease, here is my related post on trigger template for reference: tgerm.com/2012/01/salesforce-apex-trigger-template.html May 25, 2013 at 4:20
  • Thanks, I will take a look at it. I thought the same thing - that maybe something else was firing. I did not find anything, but I will take a look again. May 25, 2013 at 6:19
0

If there are other triggers, workflow field updates, etc... that are causing the cloning trigger to fire multiple times with a single request you can use a static boolean to track if the cloning has already occured and skip running the trigger again.

There are more details in Controlling Recursive Triggers.

public class CloneOpportunityHelper {
    private static boolean alreadyCloned = false;

    public static boolean hasAlreadyBeenCloned() {
        return alreadyCloned;
    }

    public static void setAlreadyCloned() {
        alreadyCloned = true;
    }
}

trigger partnerOpClone on Opportunity (before update) {
    if (CloneOpportunityHelper.hasAlreadyBeenCloned) {
        System.debug(LoggingLevel.Info, 'partnerOpClone before update trigger bypassed');
        return;
    }

    Opportunity[] oppsToClone = new Opportunity[]{};
    Opportunity[] oppsToSave = new Opportunity[]{};
    for (Opportunity record:trigger.new) {
        if (record.StageName == '09-Win' && 
          trigger.oldMap.get(record.Id).StageName !=  '09-Win' &&
          record.Type == 'Reseller / Partner Registration') {
            oppsToClone.add(record);
        }
    }

    if (oppsToClone.size() > 0) {
        for (Opportunity record:oppsToClone) {
            Opportunity theClone = record.clone(false,false);
            theClone.Amount = null;
            theClone.Name = theClone.Name + ' Partner'; 
            theClone.StageName = 'Training';
            oppsToSave.add(theClone);
        }

        CloneOpportunityHelper.setAlreadyCloned();
        insert oppsToSave;
    }
}
3
  • I tried this, I read the article, with this code it does not clone anything. I removed the trigger all together and nothing cloned. I put the original trigger back and it clones 2x. I am really at a loss here. I feel like I have an understanding of what my trigger is trying to do.. and I understood the help you gave me - I just do not understand this issue. May 28, 2013 at 0:07
  • That it doesn't clone after adding the additional check seems very odd, as it should only bypass the trigger if a cloned record has been inserted. At this stage I'd suggest carefully going through the apex debug log and tracing what is occurring. Maybe add some additional logging of your own as well. May 28, 2013 at 0:32
  • The transition in the Opportunity.StageName also looks suspect as a possible reason whey the trigger isn't firing. There might be another trigger or worflow field update changing the value. Another good area to add some explicit logging. May 28, 2013 at 0:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .