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
Nevin O'Regan 3Nevin O'Regan 3 

Trigger That Creates Related Records Based On A Picklist Field

I have a custom object called Year__c which is related to the Opportunity object.
On the Opp object I have a custom picklist field Period__c where has values 1 to 10. 
What I am trying to do is to automatically create Year__c records based on the Period__c picklist field. So if a user selects the value "2" in the Period__c field then I would like 2 Year__c records to be created.
Along with this I have a Start Date and End Date on the Opportunity and on the Year__c object. I need the Start Date in Year 1 record to = the Start Date on the Opportunity, the end Date should be 12 months after the Start Date on Year 1. On Year 2 the Start Date should be the day of the End Date of Year 1 and the End Date of Year 2 should = the End Date of the Opportunity. 

Would anyone be able to help me with this?
Dhanya NDhanya N
Hi Nevin,

You can try something like below :
public void createYear(List<Opportunity> opportunityList) {
        
        Map<String, Year__c> yearMap = new Map<String, Year__c>();
        List<Year__c> yearListToInsert = new List<Year__c>();
        for(Opportunity opp : opportunityList) {
            if(opp.Period__c != null && opp.Start_Date__c != null && opp.End_Date__c != null) {
                
                for(Integer i=1; i<=Integer.valueOf(opp.Period__c); i++ ) {
                	
                    Year__c year = new Year__c();
                    year.Name = 'Year' + i;
                    year.Opportunity__c = opp.Id;
                    year.Start_Date__c = i==1 ? opp.Start_Date__c : yearMap.get('Year'+(i-1)).End_Date__c;
                    year.End_Date__c = i<Integer.valueOf(opp.Period__c) ? year.Start_Date__c.addMonths(12) : opp.End_Date__c;
                    yearMap.put('Year' + i, year);
                    yearListToInsert.add(year);
                }
            }           
        }
        if(!yearListToInsert.isEmpty()) {
            insert yearListToInsert;
        }
    }

In your trigger, call this method and pass the list. Let me know if you have any questions.

Thanks,
Dhanya 
Nevin O'Regan 3Nevin O'Regan 3
Hi Dhanya,

Sorry for only getting back to you on this now. I'm trying to call the method from a trigger and I'm facing a lot of problems with this part. I've outlined below the Class and the Trigger. The error I keep getting is "Method does not exist or incorrect signature: void MyTestMethod() from the type CreateYearClass".

CLASS

public class CreateYearClass {
public void MyTestMethod(List<Opportunity> opportunityList) {
        
        Map<String, Year__c> yearMap = new Map<String, Year__c>();
        List<Year__c> yearListToInsert = new List<Year__c>();
        for(Opportunity opp : opportunityList) {
            if(opp.Lease_Period_Years__c  != null && opp.Lease_Start_Date__c  != null && opp.Lease_End_Date__c  != null) {
                
                for(Integer i=1; i<=Integer.valueOf(opp.Lease_Period_Years__c ); i++ ) {
                    
                    Year__c year = new Year__c();
                    year.Lease__c  = opp.Id;
                    year.Start_Date__c  = i==1 ? opp.Lease_Start_Date__c  : yearMap.get('Year'+(i-1)).End_Date__c ;
                    year.End_Date__c = i<Integer.valueOf(opp.Lease_Period_Years__c) ? year.Start_Date__c.addMonths(12) : opp.Lease_End_Date__c ;
                    yearMap.put('Year' + i, year);
                    yearListToInsert.add(year);
                }
            }           
        }
        if(!yearListToInsert.isEmpty()) {
            insert yearListToInsert;
        }
    }
}


TRIGGER

trigger CreateYearTrigger on Opportunity (before insert) {
    CreateYearClass yearHandler = new CreateYearClass();
    yearHandler.MyTestMethod();

}
 
Dhanya NDhanya N
1. The trigger should be on after insert, because you are dealing with unrelated object.
2. While calling class method, you have to pass the list trigger.new

Class:
public with sharing class CreateYearHandler {
public void createYear(List<Opportunity> opportunityList) {
        
        Map<String, Year__c> yearMap = new Map<String, Year__c>();
        List<Year__c> yearListToInsert = new List<Year__c>();
        for(Opportunity opp : opportunityList) {
            if(opp.Period__c != null && opp.Start_Date__c != null && opp.End_Date__c != null) {
                
                for(Integer i=1; i<=Integer.valueOf(opp.Period__c); i++ ) {
                	
                    Year__c year = new Year__c();
                    year.Name = 'Year' + i;
                    year.Opportunity__c = opp.Id;
                    year.Start_Date__c = i==1 ? opp.Start_Date__c : yearMap.get('Year'+(i-1)).End_Date__c;
                    year.End_Date__c = i<Integer.valueOf(opp.Period__c) ? year.Start_Date__c.addMonths(12) : opp.End_Date__c;
                    yearMap.put('Year' + i, year);
                    yearListToInsert.add(year);
                }
            }           
        }
        if(!yearListToInsert.isEmpty()) {
            insert yearListToInsert;
        }
    }
}

Trigger:
trigger CreateYearTrigger on Opportunity (after insert) {
    CreateYearHandler yearHandler = new CreateYearHandler();
    yearHandler.createYear(trigger.new);

}

Thanks,
Dhanya