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 

How can we add date automatically in the records?

Hi,
I want functionality where according to month and year date will automatically added into record. How can we achive it?



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  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;
       newdtp.add(dt);
       System.debug('****************dtp added********************'+tid);
       //insert newdtp;
     
   
    }
    insert newdtp;
    return null;
    }
   
    }


Regards,
Satakshi
Virendra ChouhanVirendra Chouhan
Hi Satakshi,

As much as i can understand with this code i think based on your Month__c and Year__c of MTP__c Object you want to genreat a date so for that you can use below code:
date myDate = date.newInstance(1998, 10, 21);
Integer day = myDate.dayOfYear();
system.assertEquals(294, day);
Replace year and month with your variable name:
i.e. 
date myDate = date.newInstance(obj.year__c, obj.Month__c, 1);
Integer day = myDate.dayOfYear();
system.assertEquals(294, day);
For more methods look on below link:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_date.htm



 
satakshisatakshi
Hello Virendra, 
    I am facing error that variable does not exist

Regards & thanks,
Satakshi
Virendra ChouhanVirendra Chouhan
In which line are you getting this error please make sure that your veriable is accessible at that line.
Create myDate variable on the top.
date myDate;

and then access it :
myDate = date.newInstance(obj.year__c, obj.Month__c, 1);