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
bhanu_prakashbhanu_prakash 

date need to be previous month

help me to write trigger if date is less than or equal to 10 , i need to upate previous month last date . if it is above 10 we need to update same day

ex: date : 04-Jun-17   output -- 31-may-17
ex: date  : 10-jun-17  output -- 31-may-17
ex: date   11-jun-17    output -- 11-jun-17
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Here you go,
Date d1 = System.Today();
Date d2;
if(d1.day()<=10){
Integer NumberOfDays = date.daysInMonth(d1.Year(), d1.Month()-1);
d2 = date.newInstance(d1.Year(), d1.Month()-1, NumberOfDays)
}
else if(d1.day()>10){
d2 = d1;
}

Replace d1 and d2 with your actual date fields.

Please mark as BEST ANSWER if it helps.
bhanu_prakashbhanu_prakash
selection date is a custom field your answer helps . please help me to write trigger iam new to apex
bhanu_prakashbhanu_prakash
trigger UpdateDate on Event__c(before insert, before update){
        date d1;
        date d2;
        list<Event__c > l = trigger.new;
        for(Event__c  i:l){
        if(d1.day()<=10){
        Integer NumberOfDays = date.daysInMonth(d1.Year(), d1.Month()-1);
        d2 = date.newInstance(d1.Year(), d1.Month()-1, NumberOfDays);
        }
        else if(d1.day()>10){
        d2 = d1;
}
}

i tried to write trigger like this.please help me where iam wrong
SalesFORCE_enFORCErSalesFORCE_enFORCEr
You need to tell me what dates are you using? I used d1 and d2 just to show the logic.
bhanu_prakashbhanu_prakash
Date__c (Field user enter date as 08-06-17  )
Copy__C(Output need to be 31-05-17)

I have tried with formual field also :

IF(MONTH(Date__c) < 12,
IF(DAY(Date__c) > 10, Date__c, DATE(YEAR(Date__c), MONTH(Date__c)-1,30)),
IF(DAY(Date__c) > 10, Date__c, DATE(YEAR(Date__c) + 1, 1, 30)))

but iam getting output as 01-05-17   i need to be 31-05-17