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
punuru kirankumarpunuru kirankumar 

how to add first day of month in date field from apex

Hi ,

I have a field called date in that field while creating records from after trigger i need to add first date of the month in that field.any ideas?

thanks
punuru kirankumarpunuru kirankumar
Hi Piyush,

Actually i am creating 3 records at a same time using after insert trigger out of those 3 records in the first record date field i need to put 
System.Date.today().toStartOfMonth(),for the second record i need put next month first day of month ,for third record i need to add 3 rd month 1 st date.

Here is my code:
trigger createProcedure on Opportunity(after insert) {
 //createProcedures.createForeCastProcedures(trigger.new);
 List<Procedure__c> lstToInsert  = new List<Procedure__c>();
 for(Opportunity opp:trigger.new){
     for(integer i=1;i<=3;i++){
          Procedure__c proc = new Procedure__c ();
          proc.Account__c = opp.AccountId;
          proc.Status__c = 'New';
           if(i ==1)
                proc.Start_Date__c = System.Date.today().toStartOfMonth();
           if(i ==2)
             proc.Start_Date__c = date.today()+30;
             if(i ==3)
             proc.Start_Date__c = date.today()+60;
          lstToInsert.add(proc);
                
     }
    }
if(lstToInsert.size()>0)
   insert lstToInsert; 
}

can you tell me how to set for second and third record.

thanks
sfdcMonkey.comsfdcMonkey.com
hi use brlow code :
trigger createProcedure on Opportunity(after insert) {
 //createProcedures.createForeCastProcedures(trigger.new);
 List<Procedure__c> lstToInsert  = new List<Procedure__c>();
 for(Opportunity opp:trigger.new){
     for(integer i=1;i<=3;i++){
          Procedure__c proc = new Procedure__c ();
          proc.Account__c = opp.AccountId;
          proc.Status__c = 'New';
           if(i ==1)
                proc.Start_Date__c = System.Date.today().toStartOfMonth();
           if(i ==2)
             proc.Start_Date__c = System.Date.today().toStartOfMonth().AddMonths(1)
             if(i ==3)
             proc.Start_Date__c = System.Date.today().toStartOfMonth().AddMonths(2)
          lstToInsert.add(proc);
                
     }
    }
if(lstToInsert.size()>0)
   insert lstToInsert; 
}

Thanks let me know if it helps you 
 
sfdcMonkey.comsfdcMonkey.com
hi punuru kirankumar, if you got your answer please close this question by choosing best answer so it will make proper solution for others in future
thanks