You need to sign in to do that
Don't have an account?
Andrew 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
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
Hi Andre,
replace this line
with
Please mark as the best answer if this help you!
Regards,
Omar
All Answers
Please try the code below:
Please mark as the best answer if this help you!
Regards,
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
Hi Andre,
replace this line
with
Please mark as the best answer if this help you!
Regards,
Omar