[SalesForce] Arithmetic expressions must use numeric arguments for line-22

I have the workflow, which updates the field Case: On Hold Time when status=On Hold changes to any other value.

PRIORVALUE(On_Hold_Time__c)+ NOW()- DATETIMEVALUE(On_Hold_Start_Time__c)


1) On_Hold_Time__c   Number(12, 6)
2) On_Hold_Start_Time__c Date/Time

The same formula looking to implement using the trigger logic, but getting the below error.

Arithmetic expressions must use numeric arguments for line-22

Here is the trigger logic

Set<Id> caseIds = new Set<Id>();
for(Case c : newCases){
    if(c.Status == 'On Hold'){
    else if(c.Status != 'On Hold'){

List<Case> cases = [SELECT Id, IsStopped, Status FROM Case WHERE Id IN :caseIds];

List<Case> lstCase = new List<Case>();
for(Case ca : cases){
    Case c = new Case(Id = ca.Id);                
    if(ca.Status == 'On Hold'){
        c.IsStopped = true;
        c.On_Hold_Start_Time__c = System.now();
    else if(ca.Status != 'On Hold'){
        c.IsStopped = false;
        c.On_Hold_Time__c = (ca.On_Hold_Time__c + System.now()) - ca.On_Hold_Start_Time__c; //line-22
update lstCase; 

Best Answer

First of all, leverage Stopped Since (API name StopStartDate) attribute of Case object when you are making isStopped=true. This will return when the case is stopped calculating milestones.

Arithmetic expression works on number, so first convert datetime to seconds and then calculate the difference.

Replace the line 22 with this:

c.On_Hold_Time__c = ca.On_Hold_Time__c 
           + Integer.valueOf((System.now().getTime() - ca.StopStartDate.getTime())/1000);

On_Hold_Time__c will store values in seconds.

Be sure to first query StopStartDate in SOQL.