You need to sign in to do that
Don't have an account?

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
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
you want to capture the Old time for Substatus__c?
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
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.
Please check following code:
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.