-2

In my code i am getting this error:

Illegal assignment from String to Date

Kindly anyone explain how to assign a string to date field in List (APEX code).

Here is my code:

for (Integer i=1; i<filelines.size(); i++) { 
    String[] inputvalues = new String[]{}; 
    inputvalues = filelines[i].split(','); 
    Opportunity opp = new Opportunity(); 
    opp.Name = inputvalues[3]; 
    opp.OPP_DateOfBirth__c = inputvalues[4]; 
    opp.StageName = inputvalues[6]; 
    opp.CloseDate = inputvalues[5]'; 
    opptoupload.add(opp); 
} 
  • 1
    Have you read the Apex reference materials? – Mike Chale Apr 16 '13 at 10:54
  • Check this salesforce.stackexchange.com/questions/8246/… out – Sergej Utko Apr 16 '13 at 10:54
  • 1
    Please put more effort into your question - if you can share with us your string and the way you are assigning it, you will find the community more responsive. – bigassforce Apr 16 '13 at 10:56
  • Please indicate which line is causing the error and some sample data. – Mike Chale Apr 16 '13 at 11:23
  • @LavanyaSanathkumar try using this opp.CloseDate = date.valueof(inputvalues[5]);.This will change the string value into date Value – Eagerin Sf Apr 16 '13 at 11:29
10

Have you tried Date.parse()? From the documentation:

Constructs a Date from a String. The format of the String depends on the local date format. The following example works in some locales:

date mydate = date.parse('12/27/2009');

| improve this answer | |
  • Code: for (Integer i=1;i<filelines.size();i++) { String[] inputvalues = new String[]{}; inputvalues = filelines[i].split(','); Opportunity opp = new Opportunity(); opp.Name = inputvalues[3]; opp.OPP_DateOfBirth__c = inputvalues[4]; opp.StageName = inputvalues[6]; opp.CloseDate = inputvalues[5]'; opptoupload.add(opp); } – Lavanya Sanathkumar Apr 16 '13 at 11:08
  • Thanks for the reply, yes i tried date.parse but after this i am VF error. – Lavanya Sanathkumar Apr 16 '13 at 11:09
  • @LavanyaSanathkumar I've inserted your code to your question body. Please don't post it here. – Sergej Utko Apr 16 '13 at 11:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.