1

Been trying to solve this for almost two weeks. I'm importing a date string from a CSV, something like 12/16/16. I want to put this date into the Tasks object Activity Date field, which is Date/Time. However, for the life of me, I can not get any conversion methods I've researched here to work. I always end up with the error of:

Illegal Assignment from Datetime to Date

Here's the portion of code where I'm trying to address the issue. I'm not a developer by trade, just trying to help some users do some things.

tskstoupload = new List<Task>();
for (Integer i=1;i<filelines.size();i++)
{
    String[] inputvalues = new String[]{};
    inputvalues = filelines[i].split(',');

    Date ad = Date.valueOf(inputvalues[1]);
    String dt = DateTime.newInstance(ad.year(), ad.month(), ad.day()).format('YYYY-MM-dd');
    Datetime acd = DateTime.valueOf(dt);                     

    Task t = new Task();
    t.WhoId = inputvalues[0];
    t.ActivityDate = acd;
    t.Subject = inputvalues[2];
    t.Priority = inputvalues[3];
    //t.RecordType = inputvalues[4];
    t.Status = inputvalues[4];
    t.Type = inputvalues[5];
    t.Description = inputvalues[6];

    tskstoupload.add(t);
}

1 Answer 1

1

You're missing the timestamp portion of the string. Also, no need to convert from String to a Datetime, then back again to a String, then back again to a Datetime. You can do it all in one go.

If you're trying to convert a date-only string into a Datetime value, you'll want to use Datetime.newInstanceGmt to get the midnight value of that date.

Datetime.newInstanceGmt(parsedDate.year(), parsedDate.month(), parsedDate.day()));

You can also use a Time instance if you prefer that syntax or want to specify a different start time.

Datetime.newInstanceGmt(parsedDate, Time.newInstance(12, 0, 0, 0)); //noon

The latter strategy is slightly more convenient if you want to one-line the conversion. That in turn would allow you construct the Task record using name=value pairs without ever needing to cache its ActivityDate value. Any time you can construct an sObject record with name=value pairs, it is significantly faster.

Task record = new Task(
    WhoId = inputValues[0],
    ActivityDate = Datetime.newInstanceGmt(
        Date.valueOf(inputValues[1]), Time.newInstance(0,0,0,0)
    ),
    Subject = inputvalues[2]//, etc.
)

Note that calling Date.valueOf('12/16/16') should cause an error:

System.TypeException: Invalid date: 12/16/16

An input of 2016-12-16 would work, but not 12/16/16. The documentation on the valueOf method states:

Usage
The specified string should use the standard date format “yyyy-MM-dd HH:mm:ss” in the local time zone.

Instead, if you expect a format of 12/16/16, use Date parsedDate = Date.parse(inputValues[0]);.

2
  • Mind=Blown on that constructing records using name=value pairs. I'm not a developer at all, never even thought about doing that! I got it to work, thank you so much!!! Dec 23, 2016 at 19:36
  • Welcome to SFSE! Glad I could help. :)
    – Adrian Larson
    Dec 23, 2016 at 19:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .