+ Start a Discussion
Cris9931Cris9931 

trigger ideas for this scenario

Hi, I want to create a trigger for the following scenario... I have 4 fields:
0. Reported Start Date(date datatype)
1. Reported Start Time(time datatype)
3. Reported End Date(Date datatype)
4. Reported End Time(time datatype)

Only the following interval of time is relevant, from 22:00(10PM) - 00:00(12 AM, next day). This interval is important because I want to display the value from formula field based on this interval of time.

Example: I have from 10:00 PM - 01:00 AM,  display show 120 minutes, because we only count  minutes in this interval of time.
Example 2: I have from 08:00 PM - 10:00 PM, display nothing(blank), because is not included in this interval of time.
Example 3: I have from 11:00 PM - 00:00 AM, display 60 minutes.

The minutes I want to be displayed in a field called Calculated_Time__c... any ideas how can I do this?
Best Answer chosen by Cris9931
Suraj Tripathi 47Suraj Tripathi 47
Hi Cristain,

You can try this code:-
trigger TimeCount on TimeCount__c (after insert) {
    List<TimeCount__c> timeList=[select End_Date__c,End_Time__c,Start_Date__c,Start_Time__c,Total_Time__c from TimeCount__c 
                                 where id IN: trigger.new and End_Date__c!=null and End_Time__c!=null and Start_Date__c!=null
                                 and Start_Time__c!=null];
    List<TimeCount__c> timeListt= new List<TimeCount__c>();
    for(TimeCount__c ti: timeList ){
        integer totalTime;
        Date startDate = ti.Start_Date__c;
        Date endDate = ti.End_Date__c;
        Integer noOfDays = startDate.daysBetween( endDate );
        system.debug('noOfDays'+noOfDays);
        system.debug('ti.Start_Time__c'+ti.Start_Time__c);
        system.debug('ti.End_Time__c'+ti.End_Time__c);
        integer hr=ti.Start_Time__c.hour();
        integer min=ti.Start_Time__c.minute();
        integer totalMin=hr*60+min;
        integer hr1=ti.End_Time__c.hour();
        integer min1=ti.End_Time__c.minute();
        integer totalMin1=hr1*60+min1;
        if(totalMin>=totalMin1){
            totalMin1=totalMin1+1440;
        }
        else{
            noOfDays++;
        }
        if(totalMin>1320 && totalMin<=1440){
            totalTime=-1;
        }
        else{
            totalTime=0;
        }
        while(totalMin<=totalMin1){
            if(totalMin>1320 && totalMin<=1440){
                system.debug('Inside if');
                totalTime++;
            }
            totalMin++;
        }
        if(noOfDays==0){
            ti.Total_Time__c=totalTime;
        }
        else{
            ti.Total_Time__c=noOfDays*totalTime;
        }
        timeListt.add(ti);
    }
    update timeListt;
}

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 
 

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi Cristain,

You can take reference given this below code:-
trigger TimeCount on TimeCount__c (after insert) {
    List<TimeCount__c> timeList=[select End_Date__c,End_Time__c,Start_Date__c,Start_Time__c,Total_Time__c from TimeCount__c 
                                 where id IN: trigger.new and End_Date__c!=null and End_Time__c!=null and Start_Date__c!=null
                                 and Start_Time__c!=null];
    List<TimeCount__c> timeListt= new List<TimeCount__c>();
    for(TimeCount__c ti: timeList ){
        integer totalTime=0;
        Date startDate = ti.Start_Date__c;
        Date endDate = ti.End_Date__c;
        Integer noOfDays = startDate.daysBetween( endDate );
        system.debug('noOfDays'+noOfDays);
        system.debug('ti.Start_Time__c'+ti.Start_Time__c);
        system.debug('ti.End_Time__c'+ti.End_Time__c);
        integer hr=ti.Start_Time__c.hour();
        integer min=ti.Start_Time__c.minute();
        integer totalMin=hr*60+min;
        integer hr1=ti.End_Time__c.hour();
        integer min1=ti.End_Time__c.minute();
        integer totalMin1=hr1*60+min1;
        if(totalMin>=totalMin1){
            totalMin1=totalMin1+1440;
        }
        else{
            noOfDays++;
        }
        while(totalMin<=totalMin1){
            if(totalMin>1320 && totalMin<=1440){
                system.debug('Inside if');
                totalTime++;
            }
            totalMin++;
        }
        if(noOfDays==0){
            ti.Total_Time__c=totalTime;
        }
        else{
            ti.Total_Time__c=noOfDays*totalTime;
        }
        timeListt.add(ti);
    }
    update timeListt;
}
In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

Thanks and Regards
Suraj Tripathi.

 
Cris9931Cris9931

Hi Suraj,

Working fine expect I have this one minute added additionally... why is that?
User-added image

Suraj Tripathi 47Suraj Tripathi 47
Hi Cristain,

You can try this code:-
trigger TimeCount on TimeCount__c (after insert) {
    List<TimeCount__c> timeList=[select End_Date__c,End_Time__c,Start_Date__c,Start_Time__c,Total_Time__c from TimeCount__c 
                                 where id IN: trigger.new and End_Date__c!=null and End_Time__c!=null and Start_Date__c!=null
                                 and Start_Time__c!=null];
    List<TimeCount__c> timeListt= new List<TimeCount__c>();
    for(TimeCount__c ti: timeList ){
        integer totalTime;
        Date startDate = ti.Start_Date__c;
        Date endDate = ti.End_Date__c;
        Integer noOfDays = startDate.daysBetween( endDate );
        system.debug('noOfDays'+noOfDays);
        system.debug('ti.Start_Time__c'+ti.Start_Time__c);
        system.debug('ti.End_Time__c'+ti.End_Time__c);
        integer hr=ti.Start_Time__c.hour();
        integer min=ti.Start_Time__c.minute();
        integer totalMin=hr*60+min;
        integer hr1=ti.End_Time__c.hour();
        integer min1=ti.End_Time__c.minute();
        integer totalMin1=hr1*60+min1;
        if(totalMin>=totalMin1){
            totalMin1=totalMin1+1440;
        }
        else{
            noOfDays++;
        }
        if(totalMin>1320 && totalMin<=1440){
            totalTime=-1;
        }
        else{
            totalTime=0;
        }
        while(totalMin<=totalMin1){
            if(totalMin>1320 && totalMin<=1440){
                system.debug('Inside if');
                totalTime++;
            }
            totalMin++;
        }
        if(noOfDays==0){
            ti.Total_Time__c=totalTime;
        }
        else{
            ti.Total_Time__c=noOfDays*totalTime;
        }
        timeListt.add(ti);
    }
    update timeListt;
}

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 
 
This was selected as the best answer
Cris9931Cris9931

Hi, I found another solution with a formula field, which is better because of less CPU consumage, also it's real time.

 

User-added image

 

IF( SVMX_PS_Customer_Start_Date__c == SVMX_PS_Customer_End_Date__c, 
    IF( OR(HOUR(SVMX_PS_Customer_End_Time__c) == 22 , HOUR(SVMX_PS_Customer_End_Time__c) == 23), 
		TEXT( ( ( ((Hour(SVMX_PS_Customer_End_Time__c) - ( IF(Hour(SVMX_PS_Customer_Start_Time__c) >21, Hour(SVMX_PS_Customer_Start_Time__c) , 22 )))*60) - (IF(Hour(SVMX_PS_Customer_Start_Time__c) > 21, Minute(SVMX_PS_Customer_Start_Time__c) , 0 )) ) + Minute(SVMX_PS_Customer_End_Time__c) ) ), 
        '0'
	), 
    IF( (HOUR(SVMX_PS_Customer_Start_Time__c) < 22 && HOUR(SVMX_PS_Customer_End_Time__c) < 22),
	   	TEXT((SVMX_PS_Customer_End_Date__c - SVMX_PS_Customer_Start_Date__c)*120),
	   	IF( (HOUR(SVMX_PS_Customer_Start_Time__c) < 22 && HOUR(SVMX_PS_Customer_End_Time__c) > 21),
		   	TEXT( (( (SVMX_PS_Customer_End_Date__c - SVMX_PS_Customer_Start_Date__c) ) *120) + ( ((Hour(SVMX_PS_Customer_End_Time__c) - 22)*60) + Minute(SVMX_PS_Customer_End_Time__c)) ),
		   	IF( (HOUR(SVMX_PS_Customer_Start_Time__c) > 21 && HOUR(SVMX_PS_Customer_End_Time__c) < 22),
			   	TEXT( (( (SVMX_PS_Customer_End_Date__c - SVMX_PS_Customer_Start_Date__c) -1) *2*60) + ( ((23-Hour(SVMX_PS_Customer_Start_Time__c))*60) + (60-Minute(SVMX_PS_Customer_Start_Time__c) ) ) ),
			   	IF( (HOUR(SVMX_PS_Customer_Start_Time__c) > 21 && HOUR(SVMX_PS_Customer_End_Time__c) > 21),
				   	TEXT( (( (SVMX_PS_Customer_End_Date__c - SVMX_PS_Customer_Start_Date__c) -1 ) *120) + ( ((23-Hour(SVMX_PS_Customer_Start_Time__c))*60) + (60-Minute(SVMX_PS_Customer_Start_Time__c)) + ((Hour(SVMX_PS_Customer_End_Time__c) - 22)*60) + Minute(SVMX_PS_Customer_End_Time__c) ) ),
				   	'0'
				)
			)
		)
	)
)