function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Andrew Fisher 28Andrew Fisher 28 

add month to date in trigger

Hi
Would someone be able to help me please?
i have a trigger which inserts child records when a field is populated, it works fine but have 2 issues 

1. The Month field (crd.name) is starting from 0 ideally would like it to start from 1
2. I want to add a month to each record (crd.mrr date__c) 

Idea is it would create 10 records if Opp.Contract_period_months__c) = 10

Here is my code

Trigger CreatingAutoRecords on Opportunity (After Insert, After Update)
{
    
    List<MRR__c> MRRRecordsFinalListToInsert = New List<MRR__c>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate)
    {
        For(Opportunity opp : Trigger.New)
        {
            If(Opportunity.Contract_Period_Months__c != null)
            {
                List<MRR__c> fetchingAlreadyExixtsedRecords = [Select Id FROM MRR__c WHERE Opportunity__c =:opp.Id];
                
                If(fetchingAlreadyExixtsedRecords.IsEmpty())
                {
                    // We are only creating a records when there is no MRR__c records exixts.
                    For(Integer I = 0; I < opp.Contract_Period_Months__c;I++)
                    
                        {
                        MRR__c crd = New MRR__c();
                        
                        crd.Opportunity__c = opp.Id;
                        crd.Name       = 'Month' + I;
                        crd.MRR_Amount__c = opp.MRR_Amount__c;
                        crd.MRR_Date__c = opp.Contract_Start_Date__c ;
                        
                        MRRRecordsFinalListToInsert.add(crd);
                    }
                }
                
            }
            
            try{
                If(!MRRRecordsFinalListToInsert.IsEmpty()){
                    insert MRRRecordsFinalListToInsert;
                }
            }
            Catch(Exception e){
                System.debug('The thrown exception for CreatingAutoRecords is:: ' + e.getMessage());
            }
        }
    }
    
    
}

Any help would be highly appreciated
 
Best Answer chosen by Andrew Fisher 28
Omar Rajab 94Omar Rajab 94

Hi Andre,
 

replace this line
 

crd.MRR_Date__c = opp.Contract_Start_Date__c.addMonths(1);

with
crd.MRR_Date__c = opp.Contract_Start_Date__c.addMonths(I);


Please mark as the best answer if this help you! 

Regards, 
Omar

All Answers

Omar Rajab 94Omar Rajab 94
Hi Andre, 

Please try the code below: 
Trigger CreatingAutoRecords on Opportunity (After Insert, After Update)
{
    
    List<MRR__c> MRRRecordsFinalListToInsert = New List<MRR__c>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate)
    {
        For(Opportunity opp : Trigger.New)
        {
            If(Opportunity.Contract_Period_Months__c != null)
            {
                List<MRR__c> fetchingAlreadyExixtsedRecords = [Select Id FROM MRR__c WHERE Opportunity__c =:opp.Id];
                
                If(fetchingAlreadyExixtsedRecords.IsEmpty())
                {
                    // We are only creating a records when there is no MRR__c records exixts.
                    For(Integer I = 1; I <= opp.Contract_Period_Months__c;I++)
                    
                        {
                        MRR__c crd = New MRR__c();
                        
                        crd.Opportunity__c = opp.Id;
                        crd.Name       = 'Month' + I;
                        crd.MRR_Amount__c = opp.MRR_Amount__c;
                        crd.MRR_Date__c = opp.Contract_Start_Date__c.addMonths(1);
                        
                        MRRRecordsFinalListToInsert.add(crd);
                    }
                }
                
            }
            
            try{
                If(!MRRRecordsFinalListToInsert.IsEmpty()){
                    insert MRRRecordsFinalListToInsert;
                }
            }
            Catch(Exception e){
                System.debug('The thrown exception for CreatingAutoRecords is:: ' + e.getMessage());
            }
        }
    }
    
    
}

Please mark as the best answer if this help you! 

Regards, 
Omar
Andrew Fisher 28Andrew Fisher 28
Hi Omar,

Many thanks for your help, it fixed name to start at 1, but the MRR Date is stll not right, I was hoping each record could add a month eg contract start date is May 1 , record 2 should be June 1 and so on 

Really apppreciate your help

Andrew
Omar Rajab 94Omar Rajab 94

Hi Andre,
 

replace this line
 

crd.MRR_Date__c = opp.Contract_Start_Date__c.addMonths(1);

with
crd.MRR_Date__c = opp.Contract_Start_Date__c.addMonths(I);


Please mark as the best answer if this help you! 

Regards, 
Omar
This was selected as the best answer
Andrew Fisher 28Andrew Fisher 28
Many thanks Omar, thats awesome and working perfectly. I really appreciate your help.