-1

I was wondering if someone could help me modifying this trigger. I'm having issues Opportunities are being updated after they are Closed/Won, which is overwriting the old (correct manager) with the rep's current manager. This is an issue because I have a trigger that create Actuals and it's editting Actuals and writing them for the current Manager instead of the old Manager. I'm really confused on where the best place to edit this is, the bottom after the if statement putting and ifelse statement starting if the StageName is Closed/Won in the trigger old map and StageName is equal to Closed/Won currently, then don't update the Current Actual. Here's the trigger:

trigger CreateActualsAndTargets on Opportunity (after insert, after update) 
{
if(Trigger.isUpdate || Trigger.isInsert)   
{
  Map<ID,Opportunity> l_OpportunityOwners = new Map<ID,Opportunity> ([Select id,o.Owner.ManagerId,Contract_ID__c,StageName,Amount,AccountId,CloseDate, o.OwnerId, Type From Opportunity o where id in:trigger.newMap.keySet()]);
  GooalAndActuals l_oGooalAndActuals = new GooalAndActuals ();

  for(Opportunity l_oOpportunityNew:l_OpportunityOwners.values())
  {
    System.debug('********************Trigger Start***********************');
    if(l_oOpportunityNew.StageName=='Closed/Won')
    {
      ID SalesRepGoalID,ManagerGoalID;
      //modified
      l_oGooalAndActuals = new GooalAndActuals ();
      l_oGooalAndActuals.opportunityNew=l_oOpportunityNew;


      //Goal Creation
      SalesRepGoalID=l_oGooalAndActuals.CreateGoal(l_oOpportunityNew.OwnerId);//SalesRep Goal Creation
      if(l_oOpportunityNew.Owner.ManagerId!= null)
      {
        ManagerGoalID=l_oGooalAndActuals.CreateGoal(l_oOpportunityNew.Owner.ManagerId);//Manager Goal Creation
      }


      //Actual Deletion
      if(Trigger.isUpdate)
      {
        l_oGooalAndActuals.DeleteActual(l_oOpportunityNew.Id);

      }

      //Actual Creation
      l_oGooalAndActuals.CreateActual(l_oOpportunityNew.OwnerId,SalesRepGoalID);//SalesRep Actual Creation
      if(l_oOpportunityNew.Owner.ManagerId!= null)
      {
        l_oGooalAndActuals.CreateActual(l_oOpportunityNew.Owner.ManagerId,ManagerGoalID);//Manager Actual Creation
      }



    }

    /*
    Start: Project
    *   Author          |Author-Email            |Date       |Comment
    *   ----------------|------------------------|-----------|--------------------------------------------------
    *   Sakonent Admin  |abrar.haq@sakonent.com  |02.16.2012 |Initial code 
    *   Purpose: It will remove Actual record(s) associated to Opportunity if Stage goes from 'Closed/Won' to some other stage.
    */        
    else if(Trigger.isUpdate)
    {
        if(Trigger.oldMap.get(l_oOpportunityNew.Id).StageName == 'Closed/Won' && l_oOpportunityNew.StageName != 'Closed/Won')
        {
            l_oGooalAndActuals = new GooalAndActuals ();
            l_oGooalAndActuals.opportunityNew=l_oOpportunityNew;
            l_oGooalAndActuals.DeleteActual(l_oOpportunityNew.Id);
        }
    }
    /*End: Project */        

  }
}

 }

1 Answers1

0

I don't know why this was marked down, but look at it like this:

You know the following -

The stage is closed/won The manager field is changing

Create a validation rule that states

AND ( 
  ISPICKVAL( StageName , 'Closed/Won' ),
  ISCHANGED(Manager__c)
)

Essentially this will return true if the stage is in closed/won and the manager has been changed. This will throw the validation rule preventing the record from being saved!

Hope this helps :)

If you really wanted to do this with a trigger you could do a before update, check the stage of the Opp in both the old and the new, and if the stage didn't change and the manager did you could simply replace the Trigger.new with the trigger.old value. POSSIBLY :) but there are some finicky rules around what can and cannot be modified in those Trigger.old/new values :)

Mike McMahon
  • 7,096
  • 3
  • 30
  • 42