+ Start a Discussion
brozinickrbrozinickr 

Apex Trigger Not Firing

Hello,

 

I'm having issues with one of my apex classes not correctly working so my apex trigger will not fire.

 

I have four objects that use this class, Contract_Item__C (which is basically a custom object that list line items of a Contract and the apex trigger fires off of), Account, AM_Performance__c (a custom goal holder object), and AM_Actual__c (custom actual object for the Goal).  The purpose of this apex class is to either find the correct goal record, create a goal record if one hasn't been created, then create and/or delete the actual with with the associated goal record that month.

 

I had found an error that these actuals were writing to the first goal record that it found in query instead of the goal record based on month it should be listed for.  That made sense, because there were no date listed in the class for it to look for it.  So I went in and made the changes so that it had to look off a field called Approval_Date__c (found on the Contract_Item__c object)'s year month in order to determine which goal record to find and associate it to.

 

I tried testing it manually to make sure that it worked before I went ahead and modified my test coverage, now it doesn't even write the actual to the am_performance__c record anymore.  I can't figure out why it won't fire.  Would someone be able to look over my code and tell me what I am missing?

 

 

class

 

public with sharing class AMAttrionActuals {

 public Account accountNew
 {
    get;set;
 }
 
 public Contract_Item__c contractitemNew
 {
    get;set;
 }
 
AM_Attrition_Actual__c l_oActual;
AM_Performance__c l_oTarget;
  
public ID CreateTarget(Id OwnerID)
 {
    System.debug('****Goal Method Call*****');
    ID GoalID;
    if((accountNew!=null)&&(contractitemNew!=null))
    {       
        
           System.debug('Select id ,Name,Goal_Date__c,Target_Owner__c From AM_Performance__c where Owner=:'+OwnerID);
           l_oTarget=null;
           for(AM_Performance__c l_oLoopTarget : [Select id,Name,Goal_Date__c,Target_Owner__c From AM_Performance__c 
                                                  Where 
                                                  Year_Month__c = :contractitemNew.Approval_Date__c.Year()+'-'+contractitemNew.Approval_Date__c.Month()
                                                  And 
                                                  Target_Owner__c= :OwnerID]
                            )
                            {
                                l_oTarget=l_oLoopTarget;
                                break;
                            }
                        
                                            
            if(l_oTarget==null)
            {
                 //Date l_oNewTargetDate = Date.newInstance(accountNew.CloseDate.Year(), accountNew.CloseDate.Month(), 1);
                 //Date l_oNewTargetDate = Date.today();
                 Date l_oNewTargetDate = Date.newInstance(contractitemNew.Approval_Date__c.Year(), contractitemNew.Approval_Date__c.Month(), 1);
                 l_oTarget= new AM_Performance__c();
                 l_oTarget.Goal_Date__c=l_oNewTargetDate;
                 l_oTarget.Target_Owner__c=OwnerID;
                 l_oTarget.CV_Value__c = 0;
                 l_oTarget.Booked_Value__c = 0;
                 l_oTarget.Renewal_Goal__c = 0;
                 l_oTarget.Attrition_Goal__c = 0;
                 l_oTarget.Eligible_Amount__c = 0;
                 insert l_oTarget;
            }
           GoalID=l_oTarget.Id;
                
    //}   
    return GoalID;  
    }
    
    return null;
 }
 
  
 public void CreateAMAttrtionActualNegative(Id OwnerID,Id contractID, ID GoalID,Decimal booked_Value,Decimal CV_value,String attr_Status)
 {
          
    if(GoalID!= null)
    {
            l_oActual = new AM_Attrition_Actual__c(Booked_Value__c = 0,CV__c=0);
            l_oActual.Account__c=accountNew.Id;
            l_oActual.Booked_Value__c = -booked_Value;
            l_oActual.CV__c =-CV_value;
            l_oActual.AttritionStatus__c = attr_Status;
            l_oActual.AM_Renewal_Target__c = GoalID;
            l_oActual.Contract_Item__c = contractID;
            l_oActual.Attrition_Actual_Owner__c = OwnerID;
            insert l_oActual;
    }
 }
 
 
 public void CreateAMAttrtionActualPositive(Id OwnerID,Id contractID, ID GoalID,Decimal booked_Value,Decimal CV_value,String attr_Status)
 {
          
    if(GoalID!= null)
    {
            l_oActual = new AM_Attrition_Actual__c(Booked_Value__c = 0,CV__c=0);
            l_oActual.Account__c=accountNew.Id;
            l_oActual.Booked_Value__c = booked_Value;
            l_oActual.CV__c =CV_value;
            l_oActual.AttritionStatus__c = attr_Status;
            l_oActual.AM_Renewal_Target__c = GoalID;
            l_oActual.Contract_Item__c = contractID;
            l_oActual.Attrition_Actual_Owner__c = OwnerID;
            insert l_oActual;
    }
 }
 public void DeleteActual(ID contractItemId)
 {
    if(accountNew!=null)
    {
        List<AM_Attrition_Actual__c> l_olstActual = [Select id From AM_Attrition_Actual__c Where Contract_Item__c =:contractItemId];
        delete l_olstActual;            
    }
 }


}

 


trigger

 

trigger CreateAMAttrtionActual on Contract_Item__c (after insert, after update) {
if(Trigger.isUpdate || Trigger.isInsert)   
    {
      Map<ID,Contract_Item__c> contractItemDetails = new Map<ID,Contract_Item__c> ([Select id,Status__c,SPID__c, Approval_Date__c, Attrition_Reason__c,Booked_Value__c,CV__c From Contract_Item__c o where id in:trigger.newMap.keySet()]);
      Set<ID> contractAcntIds = new Set<ID>();
      AMAttrionActuals l_oGooalAndActuals = new AMAttrionActuals();
      //start : to overcome salesforce SOQL limit exception
      for(Contract_Item__c contractItem:contractItemDetails.values())
      {
        contractAcntIds.add(contractItem.SPID__c);
      } 
      
      Map<ID,Account> AccountDetails = new Map<ID,Account> ([Select id,o.Owner.ManagerId,o.OwnerId,o.Type From Account o where Id in:contractAcntIds]);
      //end: to overcome salesforce SOQL limit exception     
      for(Contract_Item__c contractItem:contractItemDetails.values())
      {
        Account acnt = AccountDetails.get(contractItem.SPID__c);
        if((acnt!= null) && (acnt.Type=='Advertiser'))
        {
        System.debug('********************Trigger Start***********************');
            
    if(((contractItem.Status__c=='Pull')|| (contractItem.Status__c=='Cancel')) && (contractitem.Attrition_Reason__c != null) && (contractitem.Booked_Value__c != null))
            {
              ID SalesRepGoalID,ManagerGoalID;
              //modified
              l_oGooalAndActuals = new AMAttrionActuals();
              
               
               System.debug('SPID' + contractItem.SPID__c );
              
              
                  l_oGooalAndActuals.accountNew=acnt;
                  //Goal Creation
                  SalesRepGoalID  =l_oGooalAndActuals.CreateTarget(acnt.OwnerId);//SalesRep Goal Creation
                      
                  if(acnt.Owner.ManagerId!= null)
                  {
                    ManagerGoalID = l_oGooalAndActuals.CreateTarget(acnt.Owner.ManagerId);//Manager Goal Creation
                  }
              
              
                  //Actual Deletion
                  if(Trigger.isUpdate)
                  {
                    l_oGooalAndActuals.DeleteActual(contractItem.Id);
                    
                  }
              
                  //Actual Creation
                  l_oGooalAndActuals.CreateAMAttrtionActualNegative(acnt.OwnerId,contractItem.Id,SalesRepGoalID,contractItem.Booked_Value__c,contractItem.CV__c,contractItem.Status__c );//SalesRep Actual Creation
                  if(acnt.Owner.ManagerId!= null)
                  {
                    l_oGooalAndActuals.CreateAMAttrtionActualNegative(acnt.Owner.ManagerId,contractItem.Id,ManagerGoalID,contractItem.Booked_Value__c,contractItem.CV__c,contractItem.Status__c);//Manager Actual Creation
                  }
                  
                }
                else if(contractItem.Status__c=='Reinstate')
                {
                    ID SalesRepGoalID,ManagerGoalID;
              //modified
              l_oGooalAndActuals = new AMAttrionActuals();
              
               AM_Renewal_Goal__c ManagerGoal;  
               System.debug('SPID' + contractItem.SPID__c );
              
              
                  l_oGooalAndActuals.accountNew=acnt;
                  //Goal Creation
                  SalesRepGoalID  =l_oGooalAndActuals.CreateTarget(acnt.OwnerId);//SalesRep Goal Creation
                      
                  if(acnt.Owner.ManagerId!= null)
                  {
                    ManagerGoalID = l_oGooalAndActuals.CreateTarget(acnt.Owner.ManagerId);//Manager Goal Creation
                  }
              
              
                  //Actual Deletion
                 /* if(Trigger.isUpdate)
                  {
                    l_oGooalAndActuals.DeleteActual(contractItem.Id);
                    
                  }*/
              
                  //Actual Creation
                  l_oGooalAndActuals.CreateAMAttrtionActualPositive(acnt.OwnerId,contractItem.Id,SalesRepGoalID,contractItem.Booked_Value__c,contractItem.CV__c,contractItem.Status__c );//SalesRep Actual Creation
                  if(acnt.Owner.ManagerId!= null)
                  {
                    l_oGooalAndActuals.CreateAMAttrtionActualPositive(acnt.Owner.ManagerId,contractItem.Id,ManagerGoalID,contractItem.Booked_Value__c,contractItem.CV__c,contractItem.Status__c);//Manager Actual Creation
                  }
                }
        
       
        
        /*
        Start: Big Deal Project 
        *   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(contractItem.Id).Status__c=='Pulled') ||(Trigger.oldMap.get(contractItem.Id).Status__c=='Cancelled')||(Trigger.oldMap.get(contractItem.Id).Status__c=='Reinstated')) && 
            ((contractItem.Status__c != 'Pulled')||(contractItem.Status__c != 'Cancelled')||(contractItem.Status__c != 'Reinstated')))
            {
                l_oGooalAndActuals = new AMAttrionActuals ();
                l_oGooalAndActuals.accountNew=acnt;
                l_oGooalAndActuals.DeleteActual(contractItem.Id);
            }
        }*/
        /*End: Big Deal Project */ 
        
        }//if acnt!=null    
        
      }
    }
}