+ Start a Discussion
Arvind JainArvind Jain 

Why Minutes of Business Hours are not Calculated in Apex Trigger?

I have design a Apex Trigger Code:


trigger BusinessHrs on Customer__c (before insert, before update)
{
Long Diff1;
Long Diff2;
for(Customer__c c1: trigger.new)
{
BusinessHours stdBusinessHours = [select id from businesshours where id = '01mi0000000aAVI'];

DateTime d1 = DateTime.newInstance(1997, 1, 31, 7, 8, 16);
DateTime d2 = DateTime.newInstance(1997, 1, 31, 7, 8, 16);

d1=c1.Startdate__c;
d2=c1.EndDate__c;

Diff1=BusinessHours.diff('01mi0000000aAVI',d1, d2) ;
c1.Business_Hour1__c=(Diff1/(1*60*60*1000));

DateTime d3=DateTime.newInstanceGmt((d1.dateGmt()), (d1.timeGmt()) );
DateTime d4=DateTime.newInstanceGmt((d2.dateGmt() ), (d2.timeGmt() ) );

Diff2=BusinessHours.diff('01mi0000000aAVI',d1, d2) ;
c1.Business_Hour2__c=(Diff2/(1*60*60*1000)); } } }

In Both Business_Hour1__c,Business_Hour2__c the number field(decimal 2 places) have no decimal value... Is something wrong in : Diff2=BusinessHours.diff('01mi0000000aAVI',d1, d2) ;
c1.Business_Hour2__c=(Diff2/(1*60*60*1000));
Swati GSwati G
Instead of long data type use decimal type for Diff1 and Diff2 and try.