1

i have been trying to get Salesforce to create a timer which will tell the user how long the specific object has been in a certain pick list option for, for example say it has been on the option 'New' for 10 hours it will show through a field that its been in this status for 10 hours..

so far i have got it to display its been in each option for 0.00 minutes but the timer doesnt seem to work and i cant see where the problem may be heres what ive got so far in a trigger..

trigger CalculatePOBusinessHoursAges on Order__c (before insert, before update) {

    if (Trigger.isUpdate) {
        for (Order__c ProductOrder:System.Trigger.new) {
            //ProductOrder.status__c = '--None--';
            ProductOrder.Last_Status_Change__c = System.now();
            ProductOrder.Time_With_Support__c = 0;
            ProductOrder.Time_With_Customer__c = 0;
            ProductOrder.Time_Keeping_Completed__c = 0;
            ProductOrder.Time_Keeping_New__C = 0;
            ProductOrder.Time_Keeping_Progressing__C = 0;
            ProductOrder.Time_Keeping_Customer_Hold__C = 0;
            ProductOrder.Time_Keeping_Wayleave__C = 0;
            ProductOrder.Time_Keeping_Supplier_Hold__C = 0;
            ProductOrder.Time_Keeping_Sales__C = 0;
            ProductOrder.Time_Keeping_ADSL__C = 0;
            ProductOrder.Time_Keeping_invoiced__C = 0;
            ProductOrder.Time_Keeping_Cancelled__C = 0;

        }
    } else {
        //Get the stop statuses
        Set<String> stopStatusSet = new Set<String>();
        for (Stop_Status__c stopStatus:[Select Name From Stop_Status__c]) {
            stopStatusSet.add(stopStatus.Name);

        }

        BusinessHours defaultHours = [select Id from BusinessHours where IsDefault=true];

        for (Order__c ProductOrder:System.Trigger.new) {
            Order__c oldProductOrder = System.Trigger.oldMap.get(ProductOrder.Id);

            if (oldProductOrder.Status__c!=ProductOrder.Status__c && ProductOrder.Last_Status_Change__c != null) {   

                Id hoursToUse = ProductOrder.BusinessHours__r.Id!=null?ProductOrder.BusinessHours__r.Id:defaultHours.Id;

                Double timeSinceLastStatus = BusinessHours.diff(hoursToUse, ProductOrder.Last_Status_Change__c, System.now())/3600000.0;

                System.debug('business'+defaultHours);
                System.debug('Time status'+timeSinceLastStatus);
                System.debug('hoursToUse'+hoursToUse);
                System.debug('BusinessHours.diff'+System.now());

                if (oldProductOrder.Status__c =='New') {
                    ProductOrder.Time_Keeping_New__C += timeSinceLastStatus;
                }
                else if (oldProductOrder.Status__c =='Completed') {
                    ProductOrder.Time_Keeping_Completed__C += timeSinceLastStatus;
                }
                else if (oldProductOrder.Status__c =='Progressing') {
                    ProductOrder.Time_Keeping_Progressing__C += timeSinceLastStatus;
                }
                else if (oldProductOrder.Status__c =='Customer Hold') {
                    ProductOrder.Time_Keeping_Customer_Hold__C += timeSinceLastStatus;
                }
                else if (oldProductOrder.Status__c =='Wayleave') {
                    ProductOrder.Time_Keeping_Wayleave__C += timeSinceLastStatus;
                }
                else if (oldProductOrder.Status__c =='Supplier Hold') {
                    ProductOrder.Time_Keeping_Supplier_Hold__C += timeSinceLastStatus;
                }
                else if (oldProductOrder.Status__c =='Cancelled') {
                    ProductOrder.Time_Keeping_Cancelled__C += timeSinceLastStatus;
                }
                else if (oldProductOrder.Status__c =='Sales') {
                    ProductOrder.Time_Keeping_Sales__C += timeSinceLastStatus;
                }
               else if (oldProductOrder.Status__c =='ADSL') {
                    ProductOrder.Time_Keeping_ADSL__C += timeSinceLastStatus;
                }
                else if (oldProductOrder.Status__c =='Invoiced') {
                    ProductOrder.Time_Keeping_invoiced__C += timeSinceLastStatus;
                }
                else {
                    ProductOrder.Time_With_Support__c += timeSinceLastStatus;
                }

                ProductOrder.Last_Status_Change__c = System.now();
            }
        }
    }
}
  • Shouldn't the line if (Trigger.isUpdate) { be if (Trigger.isInsert) {? – Keith C Nov 10 '14 at 16:32
  • tried that but then you dont even get the 0.00 on all the fields? – ClevelandDan Nov 10 '14 at 16:45
  • Well keep that change. With this trigger you would have to start with new Order__c objects to get the zero initialisation done. For testing I would hardcode a value in timeSinceLastStatus then when the updates were working put the real timing back in. Otherwise you'll be waiting a long time when you are testing. – Keith C Nov 10 '14 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.