+ Start a Discussion
sfdc007sfdc007 

Apex class help needed

1) I have a custom object called as Teamform__c and a custom picklist field called as Status__c which is a picklist type field and substatus__c which is a dependnt picklist field

2) When the Status__c value changes i want to calculate the time, The time calculation should apply only for 5 business working days (Mon - Fri)


I am thinking of having a related list which will capture the old time and when the sub status is changed it will capture the new time like the below format

Old time                  New changed time


Help me how to achieve this in apex class

Kindly help me with the code pls

Thanks in Advance

 
Dhriti MoulickDhriti Moulick
Hi,

   you want to capture the Old time for Substatus__c?
sfdc007sfdc007
No , I want to capture the new time when the sub status is changed except for sat and sun
Dhriti MoulickDhriti Moulick
What value you want to capture in Old Time?
sfdc007sfdc007
The old sub status time when it is not changed

for eg : when the sub status is set to "New"  yesterday it will capture that time

when i change the sub status to"in progress"  today it will capture the time diffrence between the old time and the present time when it is updated

 
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi sfdc007,

Here are steps to achieve this.

1. Calculate timestamp on each status change in a custom field.
2. Define business hours in your org.
3. Use Apex BusinessHours class to eliminate saturday and sunday from elapsed time in status change calculation.

Please look at BusinessHours Classs Usage below.
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_businesshours.htm

Let us know if it helps you.
 
sfdc007sfdc007
can you please help me with the sample code for reference please
Dhriti MoulickDhriti Moulick
Hi sfdc007,

Please check following code:
 
trigger AccountTrigger on Account (before insert,before update) {
List<Account> updateAccount = new List<Account>();
   if(Trigger.isbefore){
      if(Trigger.isInsert){
         for(Account acc:Trigger.new){
             Datetime dt = DateTime.newInstance(Date.today(), Time.newInstance(0, 0, 0, 0));
             String dayOfWeek=dt.format('EEEE');
             System.debug('Day : ' + dayOfWeek);
             if((dayOfWeek!='Saturday' || dayOfWeek!='Saturday') && (acc.SubStatus__c!=null || acc.SubStatus__c!=''))
               acc.Old_Time__c=System.now();
         }
      }
      if(Trigger.isUpdate){
         for(Account accs:Trigger.new){
             Datetime dt = DateTime.newInstance(Date.today(), Time.newInstance(0, 0, 0, 0));
             String dayOfWeek=dt.format('EEEE');
             System.debug('Day : ' + dayOfWeek);
             Date convertCreatedDate = date.newinstance(Trigger.oldMap.get(accs.Id).Old_Time__c.year(), Trigger.oldMap.get(accs.Id).Old_Time__c.month(), Trigger.oldMap.get(accs.Id).Old_Time__c.day());
             if((Trigger.oldMap.get(accs.Id).SubStatus__c!=Trigger.newMap.get(accs.Id).SubStatus__c) && 
               (accs.SubStatus__c!=null || accs.SubStatus__c!='') && (dayOfWeek!='Saturday' || dayOfWeek!='Saturday')){
                 Integer dateDifference = System.today().daysBetween(convertCreatedDate);
                 accs.New_Time__c=System.now();
                 accs.Days_Difference__c=dateDifference;
            }
         }
      }
   }
}

Initially while creating records,old value will take system date.

After updating status,it will find the difference old value and new value and calculate the result and display the record.
sfdc007sfdc007
can you help me with the apex class , as i am populating it through apex class not trigger please