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
satakshisatakshi 

Variable does not exist: date

Hello, 

I am writing a code where when i will create record date should be automatically create into visualforce. i am getting error in this line        date myDate = date.newInstance(obj.Year__c, obj.Month__c, 1);
Can anyone please help me to solve this?

public with sharing class Calender {
    public List<DTP__c> mydtpList{get;set;}
    public  MTP__c obj{get;set;}
    public Calender(ApexPages.StandardController controller)
     {
          tid = ApexPages.currentPage().getParameters().get('id');
         System.debug('***************dtp added*******************'+tid); 

     }

public Decimal dy;
public date myDate;

public  id tid{get;set;}

    public PageReference createdtp() {
     
        System.debug('***************tid tid*******************'+tid);
        obj = [Select  Id, Month__c, Year__c,  February__c, NumberOfDays__c from MTP__c where id=:tid ];
        dy= obj.NumberOfDays__c;
        if(obj.February__c !=null)      
        {
         String str=obj.February__c;
         dy=Decimal.ValueOf(str);
        }  
           else 
           {
            Decimal str= obj.NumberOfDays__c;
         dy=str;
         }
        
       
        
/*
    for(MTP__c mt:obj)
    {
    if(mt.Month__c=='February')
    
    {
    String str=mt.February__c;
    dy=Decimal.ValueOf(s
      System.debug('***************in IF dy size******************'+dy);
    }
    else
    {
     Decimal str= mt.NumberOfDays__c;
      dy=str;
        System.debug('***************in  else dy size******************'+dy);
    }
    }*/
    
  List<DTP__c> newdtp= new List<DTP__c>();
   System.debug('***************dy size******************'+dy);
    for(Integer i = 1; i<=dy; i++)

    {
       DTP__c  dt = new DTP__c();
       dt.MTP__c=tid;
       date myDate = date.newInstance(obj.Year__c, obj.Month__c, 1);
       Integer day = myDate.dayOfYear();
       system.assertEquals(294, day);
       newdtp.add(dt);
       System.debug('****************dtp added********************'+tid);
       //insert newdtp;
     
   
    }
    insert newdtp;
    return null;
    }
   
    }


Thanks & Regards,
Satakshi
Virendra ChouhanVirendra Chouhan
Hi Satakshi,

Why are you declare "myDate" twise in your code (see line 12 and 59).

please modify your line number 59 with below code
myDate = date.newInstance(obj.Year__c, obj.Month__c, 1);
satakshisatakshi
Hi Virendra,

ooops sorry. But after removing that also i m getting same error
Virendra ChouhanVirendra Chouhan
Hi Satakshi,

Seems like your Year__c and Month__c fields are string type instead of Integer. and according to salesforce documentationdate.newInstance() accept only integer values. So first convert your text value to integer then create a date value like:
myDate = date.newInstance(integer.valueOf(obj.Year__c), integer.valueOf(obj.Month__c), 1);