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 */
}
}
}