+ Start a Discussion
Steve Connelly 5Steve Connelly 5 

I need a little help with a trigger please

I have built a trigger that should fire when an Opportunity is created or updated in a particular way.

I was so focused on the update part that I never tested the on insert part until today.

Everything else in trigger works but I can't figure out why it does not execute when the Opp is first created.

Here is the complete trigger:
trigger createOqNew on Opportunity (after insert, after update) 

//trigger
{ 
   
    // try
    try{
           
        Id recordTypeId =
        Schema.SObjectType.Opportunity.getRecordTypeInfosByDeveloperName().get('CDARS_ICS_Prospect').getRecordTypeId();
        
        List <Open_Quarter__c> createOpenQuarter = new List <Open_Quarter__c>();
        List <Open_Quarter__c> deleteOpenQuarter = new List <Open_Quarter__c>();
        
        // for loop 1
        for (Opportunity opportunityList : Trigger.new) {   
            
            // if loop 1
            if (opportunityList.RecordTypeId == recordTypeId) {
                
                // if loop 2
                if (Trigger.isInsert 
                    || (Trigger.isUpdate && opportunityList.Estimated_Start_Quarter__c != Trigger.oldMap.get(opportunityList.Id).Estimated_Start_Quarter__c)
                    || (Trigger.isUpdate && opportunityList.Estimated_Finish_Quarter__c != Trigger.oldMap.get(opportunityList.Id).Estimated_Finish_Quarter__c)){
                                          
                    Decimal year = opportunityList.Start_Year__c;
                    Decimal quarter = opportunityList.Start_Quarter__c;
                    Decimal span = opportunityList.Quarters_Spanned_op__c;
                    
                    //for loop 2
                    for (Integer counter = 0; counter < span; counter++)
                    {
                        Open_Quarter__c oq           = new Open_Quarter__C();
                        oq.Amount_Per_Quarter_oq__c  = opportunityList.Amount_per_Quarter_op__c;
                        oq.Close_Date_oq__c          = opportunityList.CloseDate;
                        oq.Name                      = year+'-'+'Q'+quarter;
                        oq.Opportunity_Name_oq__c    = opportunityList.Id;
                        
                        createOpenQuarter.add(oq);
                        quarter++;
                        
                        // if loop 3
                        if (quarter > 4) {
                            quarter = 1;
                            year++;
                        } //end if loop 3                       
                    } //end for loop 2      
                } //end if loop 2
            } //end if loop 1
        } //end for loop 1
        deleteOpenQuarter.addAll ([SELECT Id, Name FROM Open_Quarter__c WHERE Opportunity_Name_oq__c IN :trigger.newMap.keySet()]);
        
        // if loop 4
        if (deleteOpenQuarter.isEmpty()==false){
            Database.delete (deleteOpenQuarter,false);
        } // end if loop 4
        
        // if loop 5
        if(createOpenQuarter.isEmpty()==false){
            Database.insert (createOpenQuarter,false);
        } // end if loop 5
    } // end try
    
    //catch
    catch (Exception e){
        //e.getMessage()
            //e.getLineNumber()
            throw e;
    } // end catch
} // end trigger
It seems like it should work on insert but it does not. It only works on update.

Any suggestions?

Steve
 
Best Answer chosen by Steve Connelly 5
DeveloperSudDeveloperSud
Hi Steve,

Did you check if the opportunity record has value in the fields Start_Year__c,Start_Quarter__c,Quarters_Spanned_op__c during the time of insert??

All Answers

DeveloperSudDeveloperSud
Hi Steve,

Did you check if the opportunity record has value in the fields Start_Year__c,Start_Quarter__c,Quarters_Spanned_op__c during the time of insert??
This was selected as the best answer
Steve Connelly 5Steve Connelly 5
I was able to sort this out. The insert was working but I also have a delete on there that was deleting the inserted records. I had to create an ID set to limit the records that get delete3d.

Thanks though.
Sc