You need to sign in to do that
Don't have an account?
Date type casting problem
The following code compiles but causes a System.type exception - invalid date/time during runtime. I The first and second lines appear to fail. I am trying to get a dat from the db( aPgCase.Effective_End_Date__c) and add 1 year. Same for the second line but add one day. Can someone tell me what I am doing wrong?
Thanks
DateTime newEffectiveEndDate = DateTime.valueOf(aPgCase.Effective_End_Date__c).addYears(1);
DateTime newEffectiveStartDate = DateTime.valueOf(aPgCase.Effective_Start_Date__c).addDays(1);
Date newEffectiveEndDay = Date.valueOf(newEffectiveEndDate);
Date newEffectiveStartDay = Date.valueOf(newEffectiveStartDate);
You should use the newinstance method, and then pass the date fields required.
DateTime newEffectiveEndDate = DateTime.newinstance(aPgCase.Effective_End_Date__c.year(),aPgCase.Effective_End_Date__c.month(),aPgCase.Effective_End_Date__c.day()).addYears(1); DateTime newEffectiveStartDate = DateTime.newinstance(aPgCase.Effective_Start_Date__c.year(),aPgCase.Effective_Start_Date__c.month(),aPgCase.Effective_Start_Date__c.day()).addDays(1); Date newEffectiveEndDay = Date.newinstance(newEffectiveEndDate.year(),newEffectiveEndDate.month(),newEffectiveEndDate.day()); Date newEffectiveStartDay = Date.newinstance(newEffectiveStartDate.year(),newEffectiveStartDate.month(),newEffectiveStartDate.day());
All Answers
Datetime.valueof takes a string as the parameter, you appear to be passing a date or datetime field.
If you Effective_End_Date__c is a datetime, you could use the Adddays and addyear directly,
if it is a date, you would need to generate a newinstance of the correct type using the newinstance method.
So the "dates" are coming to me as a Date type. I needed to use the DateTime so I could use the add years and add days methods.
I am trying to cast it back to a Date.
DateTime.instanceOf does not appear to have a signature to pass in a stand alone date. I am sure I am missing something here.
You should use the newinstance method, and then pass the date fields required.
DateTime newEffectiveEndDate = DateTime.newinstance(aPgCase.Effective_End_Date__c.year(),aPgCase.Effective_End_Date__c.month(),aPgCase.Effective_End_Date__c.day()).addYears(1); DateTime newEffectiveStartDate = DateTime.newinstance(aPgCase.Effective_Start_Date__c.year(),aPgCase.Effective_Start_Date__c.month(),aPgCase.Effective_Start_Date__c.day()).addDays(1); Date newEffectiveEndDay = Date.newinstance(newEffectiveEndDate.year(),newEffectiveEndDate.month(),newEffectiveEndDate.day()); Date newEffectiveStartDay = Date.newinstance(newEffectiveStartDate.year(),newEffectiveStartDate.month(),newEffectiveStartDate.day());