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
falfadlifalfadli 

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);

Best Answer chosen by Admin (Salesforce Developers) 
JimRaeJimRae

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

JimRaeJimRae

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.

falfadlifalfadli

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.

JimRaeJimRae

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());

 

 

This was selected as the best answer
falfadlifalfadli
Thanks for help Jim! it appears to indeed work now with the code you provided.