You need to sign in to do that
Don't have an account?
Nevin 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?
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?
You can try something like below :
In your trigger, call this method and pass the list. Let me know if you have any questions.
Thanks,
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();
}
2. While calling class method, you have to pass the list trigger.new
Class:
Trigger:
Thanks,
Dhanya