Skip to main content
Commonmark migration
Source Link

You are going to have to do something like this first: (from your link)

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];

Then use that id as the first parameter in the method:

diff(bh.id, startdate, Datetime.now())<48)

You were getting an error message because you were using the name of the type of parameter that you needed to put into the function instead of using a value of that type.


I got this to run:

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
Datetime startdate = Datetime.newInstance(2013, 5, 28, 1, 6, 8);
long l = BusinessHours.diff(bh.id,startdate,system.now());
system.debug(l);  

I this also worked for me:

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
Case c = [SELECT ClosedDate FROM Case WHERE ClosedDate != NULL LIMIT 1];

DateTime startdate = c.ClosedDate;
DateTime systemnow = system.now();

long startl = startdate.getTime();
long nowl   = systemnow.getTime();
long diffl  = BusinessHours.diff(bh.id,startdate,systemnow);

system.debug('startl: ' + startl);
system.debug('nowl: '   + nowl);
system.debug('nowl - startl: ' + (nowl - startl));
system.debug('diffl: '  + diffl);

system.assertEquals(nowl-startl,diffl);

This also works:

Trigger Test_Business_hours_Trigger on EmailMessage (after insert) {
    BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
    list<Id> RelatedCaseIds = new list<Id>();
    for(EmailMessage em : Trigger.New){
        RelatedCaseIds.add(em.ParentId);
    }
    list<Case> CasesToUpdate = new list<Case>();
    for(Case c : [SELECT Id, ClosedDate FROM Case WHERE Id IN :RelatedCaseIds AND ClosedDate != NULL]){
        if(BusinessHours.diff(bh.Id,c.ClosedDate,system.now()) < 172800000){
            c.Status = 'Working';
            CasesToUpdate.add(c);
        }
    }
    update CasesToUpdate;
}

You are going to have to do something like this first: (from your link)

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];

Then use that id as the first parameter in the method:

diff(bh.id, startdate, Datetime.now())<48)

You were getting an error message because you were using the name of the type of parameter that you needed to put into the function instead of using a value of that type.


I got this to run:

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
Datetime startdate = Datetime.newInstance(2013, 5, 28, 1, 6, 8);
long l = BusinessHours.diff(bh.id,startdate,system.now());
system.debug(l);  

I this also worked for me:

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
Case c = [SELECT ClosedDate FROM Case WHERE ClosedDate != NULL LIMIT 1];

DateTime startdate = c.ClosedDate;
DateTime systemnow = system.now();

long startl = startdate.getTime();
long nowl   = systemnow.getTime();
long diffl  = BusinessHours.diff(bh.id,startdate,systemnow);

system.debug('startl: ' + startl);
system.debug('nowl: '   + nowl);
system.debug('nowl - startl: ' + (nowl - startl));
system.debug('diffl: '  + diffl);

system.assertEquals(nowl-startl,diffl);

This also works:

Trigger Test_Business_hours_Trigger on EmailMessage (after insert) {
    BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
    list<Id> RelatedCaseIds = new list<Id>();
    for(EmailMessage em : Trigger.New){
        RelatedCaseIds.add(em.ParentId);
    }
    list<Case> CasesToUpdate = new list<Case>();
    for(Case c : [SELECT Id, ClosedDate FROM Case WHERE Id IN :RelatedCaseIds AND ClosedDate != NULL]){
        if(BusinessHours.diff(bh.Id,c.ClosedDate,system.now()) < 172800000){
            c.Status = 'Working';
            CasesToUpdate.add(c);
        }
    }
    update CasesToUpdate;
}

You are going to have to do something like this first: (from your link)

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];

Then use that id as the first parameter in the method:

diff(bh.id, startdate, Datetime.now())<48)

You were getting an error message because you were using the name of the type of parameter that you needed to put into the function instead of using a value of that type.


I got this to run:

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
Datetime startdate = Datetime.newInstance(2013, 5, 28, 1, 6, 8);
long l = BusinessHours.diff(bh.id,startdate,system.now());
system.debug(l);  

I this also worked for me:

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
Case c = [SELECT ClosedDate FROM Case WHERE ClosedDate != NULL LIMIT 1];

DateTime startdate = c.ClosedDate;
DateTime systemnow = system.now();

long startl = startdate.getTime();
long nowl   = systemnow.getTime();
long diffl  = BusinessHours.diff(bh.id,startdate,systemnow);

system.debug('startl: ' + startl);
system.debug('nowl: '   + nowl);
system.debug('nowl - startl: ' + (nowl - startl));
system.debug('diffl: '  + diffl);

system.assertEquals(nowl-startl,diffl);

This also works:

Trigger Test_Business_hours_Trigger on EmailMessage (after insert) {
    BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
    list<Id> RelatedCaseIds = new list<Id>();
    for(EmailMessage em : Trigger.New){
        RelatedCaseIds.add(em.ParentId);
    }
    list<Case> CasesToUpdate = new list<Case>();
    for(Case c : [SELECT Id, ClosedDate FROM Case WHERE Id IN :RelatedCaseIds AND ClosedDate != NULL]){
        if(BusinessHours.diff(bh.Id,c.ClosedDate,system.now()) < 172800000){
            c.Status = 'Working';
            CasesToUpdate.add(c);
        }
    }
    update CasesToUpdate;
}
Another example
Source Link
martin
  • 12.7k
  • 10
  • 50
  • 75

You are going to have to do something like this first: (from your link)

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];

Then use that id as the first parameter in the method:

diff(bh.id, startdate, Datetime.now())<48)

You were getting an error message because you were using the name of the type of parameter that you needed to put into the function instead of using a value of that type.


I got this to run:

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
Datetime startdate = Datetime.newInstance(2013, 5, 28, 1, 6, 8);
long l = BusinessHours.diff(bh.id,startdate,system.now());
system.debug(l);  

I this also worked for me:

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
Case c = [SELECT ClosedDate FROM Case WHERE ClosedDate != NULL LIMIT 1];

DateTime startdate = c.ClosedDate;
DateTime systemnow = system.now();

long startl = startdate.getTime();
long nowl   = systemnow.getTime();
long diffl  = BusinessHours.diff(bh.id,startdate,systemnow);

system.debug('startl: ' + startl);
system.debug('nowl: '   + nowl);
system.debug('nowl - startl: ' + (nowl - startl));
system.debug('diffl: '  + diffl);

system.assertEquals(nowl-startl,diffl);

This also works:

Trigger Test_Business_hours_Trigger on EmailMessage (after insert) {
    BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
    list<Id> RelatedCaseIds = new list<Id>();
    for(EmailMessage em : Trigger.New){
        RelatedCaseIds.add(em.ParentId);
    }
    list<Case> CasesToUpdate=CasesToUpdate = new list<Case>();
    for(Case c : [SELECT Id, ClosedDate FROM Case WHERE Id IN RelatedCaseIds]:RelatedCaseIds AND ClosedDate != NULL]){
        if(BusinessHours.diff(bh.Id,c.ClosedDate,system.now()) > < 172800000){
            c.Status = 'Working';
            CasesToUpdate.add(c);
        }
    }
    update CasesToUpdate;
}

You are going to have to do something like this first: (from your link)

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];

Then use that id as the first parameter in the method:

diff(bh.id, startdate, Datetime.now())<48)

You were getting an error message because you were using the name of the type of parameter that you needed to put into the function instead of using a value of that type.


I got this to run:

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
Datetime startdate = Datetime.newInstance(2013, 5, 28, 1, 6, 8);
long l = BusinessHours.diff(bh.id,startdate,system.now());
system.debug(l);  

I this also worked for me:

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
Case c = [SELECT ClosedDate FROM Case WHERE ClosedDate != NULL LIMIT 1];

DateTime startdate = c.ClosedDate;
DateTime systemnow = system.now();

long startl = startdate.getTime();
long nowl   = systemnow.getTime();
long diffl  = BusinessHours.diff(bh.id,startdate,systemnow);

system.debug('startl: ' + startl);
system.debug('nowl: '   + nowl);
system.debug('nowl - startl: ' + (nowl - startl));
system.debug('diffl: '  + diffl);

system.assertEquals(nowl-startl,diffl);

Trigger Test_Business_hours_Trigger on EmailMessage (after insert) {
    BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
    list<Id> RelatedCaseIds = new list<Id>();
    for(EmailMessage em : Trigger.New){
        RelatedCaseIds.add(em.ParentId);
    }
    list<Case> CasesToUpdate= new list<Case>();
    for(Case c : [SELECT Id, ClosedDate FROM Case WHERE Id IN RelatedCaseIds]){
        if(BusinessHours.diff(bh.Id,c.ClosedDate,system.now()) >  172800000){
            c.Status = 'Working';
            CasesToUpdate.add(c);
        }
    }
    update CasesToUpdate;
}

You are going to have to do something like this first: (from your link)

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];

Then use that id as the first parameter in the method:

diff(bh.id, startdate, Datetime.now())<48)

You were getting an error message because you were using the name of the type of parameter that you needed to put into the function instead of using a value of that type.


I got this to run:

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
Datetime startdate = Datetime.newInstance(2013, 5, 28, 1, 6, 8);
long l = BusinessHours.diff(bh.id,startdate,system.now());
system.debug(l);  

I this also worked for me:

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
Case c = [SELECT ClosedDate FROM Case WHERE ClosedDate != NULL LIMIT 1];

DateTime startdate = c.ClosedDate;
DateTime systemnow = system.now();

long startl = startdate.getTime();
long nowl   = systemnow.getTime();
long diffl  = BusinessHours.diff(bh.id,startdate,systemnow);

system.debug('startl: ' + startl);
system.debug('nowl: '   + nowl);
system.debug('nowl - startl: ' + (nowl - startl));
system.debug('diffl: '  + diffl);

system.assertEquals(nowl-startl,diffl);

This also works:

Trigger Test_Business_hours_Trigger on EmailMessage (after insert) {
    BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
    list<Id> RelatedCaseIds = new list<Id>();
    for(EmailMessage em : Trigger.New){
        RelatedCaseIds.add(em.ParentId);
    }
    list<Case> CasesToUpdate = new list<Case>();
    for(Case c : [SELECT Id, ClosedDate FROM Case WHERE Id IN :RelatedCaseIds AND ClosedDate != NULL]){
        if(BusinessHours.diff(bh.Id,c.ClosedDate,system.now()) < 172800000){
            c.Status = 'Working';
            CasesToUpdate.add(c);
        }
    }
    update CasesToUpdate;
}
deleted 20 characters in body
Source Link
martin
  • 12.7k
  • 10
  • 50
  • 75

You are going to have to do something like this first: (from your link)

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];

Then use that id as the first parameter in the method:

diff(bh.id, startdate, Datetime.now())<48)

You were getting an error message because you were using the name of the type of parameter that you needed to put into the function instead of using a value of that type.


I got this to run:

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
Datetime startdate = Datetime.newInstance(2013, 5, 28, 1, 6, 8);
long l = BusinessHours.diff(bh.id,startdate,system.now());
system.debug(l);  

I this also worked for me:

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
Case c = [SELECT ClosedDate FROM Case WHERE ClosedDate != NULL LIMIT 1];

DateTime startdate = c.ClosedDate;
DateTime systemnow = system.now();

long startl = startdate.getTime();
long nowl   = systemnow.getTime();
long diffl  = BusinessHours.diff(bh.id,startdate,systemnow);

system.debug('startl: ' + startl);
system.debug('nowl: '   + nowl);
system.debug('nowl - startl: ' + (nowl - startl));
system.debug('diffl: '  + diffl);

system.assertEquals(nowl-startl,diffl);

Trigger Test_Business_hours_Trigger on EmailMessage (after insert) {
    BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
    list<Id> RelatedCaseIds = new list<Id>();
    for(EmailMessage em : Trigger.New){
        RelatedCaseIds.add(em.ParentId);
    }
    list<Case> CasesToUpdate= new list<Case>();
    for(Case c : [SELECT Id, ClosedDate FROM Case WHERE Id IN RelatedCaseIds]){
        if(BusinessHours.diff(bh.Id,c.ClosedDate.getTime(),system.now().getTime()) >  172800000){
            c.Status = 'Working';
            CasesToUpdate.add(c);
        }
    }
    update CasesToUpdate;
}

You are going to have to do something like this first: (from your link)

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];

Then use that id as the first parameter in the method:

diff(bh.id, startdate, Datetime.now())<48)

You were getting an error message because you were using the name of the type of parameter that you needed to put into the function instead of using a value of that type.


I got this to run:

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
Datetime startdate = Datetime.newInstance(2013, 5, 28, 1, 6, 8);
long l = BusinessHours.diff(bh.id,startdate,system.now());
system.debug(l);  

I this also worked for me:

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
Case c = [SELECT ClosedDate FROM Case WHERE ClosedDate != NULL LIMIT 1];

DateTime startdate = c.ClosedDate;
DateTime systemnow = system.now();

long startl = startdate.getTime();
long nowl   = systemnow.getTime();
long diffl  = BusinessHours.diff(bh.id,startdate,systemnow);

system.debug('startl: ' + startl);
system.debug('nowl: '   + nowl);
system.debug('nowl - startl: ' + (nowl - startl));
system.debug('diffl: '  + diffl);

system.assertEquals(nowl-startl,diffl);

Trigger Test_Business_hours_Trigger on EmailMessage (after insert) {
    BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
    list<Id> RelatedCaseIds = new list<Id>();
    for(EmailMessage em : Trigger.New){
        RelatedCaseIds.add(em.ParentId);
    }
    list<Case> CasesToUpdate= new list<Case>();
    for(Case c : [SELECT Id, ClosedDate FROM Case WHERE Id IN RelatedCaseIds]){
        if(BusinessHours.diff(bh.Id,c.ClosedDate.getTime(),system.now().getTime()) >  172800000){
            c.Status = 'Working';
            CasesToUpdate.add(c);
        }
    }
    update CasesToUpdate;
}

You are going to have to do something like this first: (from your link)

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];

Then use that id as the first parameter in the method:

diff(bh.id, startdate, Datetime.now())<48)

You were getting an error message because you were using the name of the type of parameter that you needed to put into the function instead of using a value of that type.


I got this to run:

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
Datetime startdate = Datetime.newInstance(2013, 5, 28, 1, 6, 8);
long l = BusinessHours.diff(bh.id,startdate,system.now());
system.debug(l);  

I this also worked for me:

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
Case c = [SELECT ClosedDate FROM Case WHERE ClosedDate != NULL LIMIT 1];

DateTime startdate = c.ClosedDate;
DateTime systemnow = system.now();

long startl = startdate.getTime();
long nowl   = systemnow.getTime();
long diffl  = BusinessHours.diff(bh.id,startdate,systemnow);

system.debug('startl: ' + startl);
system.debug('nowl: '   + nowl);
system.debug('nowl - startl: ' + (nowl - startl));
system.debug('diffl: '  + diffl);

system.assertEquals(nowl-startl,diffl);

Trigger Test_Business_hours_Trigger on EmailMessage (after insert) {
    BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
    list<Id> RelatedCaseIds = new list<Id>();
    for(EmailMessage em : Trigger.New){
        RelatedCaseIds.add(em.ParentId);
    }
    list<Case> CasesToUpdate= new list<Case>();
    for(Case c : [SELECT Id, ClosedDate FROM Case WHERE Id IN RelatedCaseIds]){
        if(BusinessHours.diff(bh.Id,c.ClosedDate,system.now()) >  172800000){
            c.Status = 'Working';
            CasesToUpdate.add(c);
        }
    }
    update CasesToUpdate;
}
added 106 characters in body
Source Link
martin
  • 12.7k
  • 10
  • 50
  • 75
Loading
added another example
Source Link
martin
  • 12.7k
  • 10
  • 50
  • 75
Loading
closed date instead of created date
Source Link
martin
  • 12.7k
  • 10
  • 50
  • 75
Loading
Another example
Source Link
martin
  • 12.7k
  • 10
  • 50
  • 75
Loading
Added a working example
Source Link
martin
  • 12.7k
  • 10
  • 50
  • 75
Loading
Source Link
martin
  • 12.7k
  • 10
  • 50
  • 75
Loading