function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
B TulasiB Tulasi 

count time from different records within the custom Object.

Hi All,

I have one custome field i.e. Time__C. I need to count time from different records within the object. what we counted time will be store in another custom field i.e Toatal_Time__C.
For Example :
Record1 : Time__C=2 hours.
Record2 : Time__C=3 hours.
Record3 : Time__C=2 hours.

So, Total_Time=7 Hours.

Trigger :

trigger TimeCount on Work_Log_Sheet__c (before insert, before update) {
    decimal count;
    Set<Id> sid = new Set<Id>();
    List<Work_Log_Sheet__c> lwls=New List<Work_Log_Sheet__c>();
    for (Work_Log_Sheet__c wls : lwls) {
        sid.add(wls.Id);       
    
        if(wls.Work_Update_Date__c==System.today()){
            wls.Total_Hours__c=wls.Total_Hours__c+wls.Total_Time__c;            
            lwls.add(wls);
        }
    }
   insert lwls;
      
}

Note : there is no error . but i am not getting results.  How can i write this code.

Thanks in advance
Thulasi.
sandeep sankhlasandeep sankhla
Hi B Tulasi,

so you have Work_Log_Sheet__c object where you have Total_Time__c and Total_Hours__c field right ? can you share your req in detail so I can help you out ...

Above code will not do anything because lwls list is empty as you only initialized and instead of this list you should use Trigger.new in for loop but it would be helpful if you will share the req properly on what condition what you need....

Try the below code this will help you ...
trigger TimeCount on Work_Log_Sheet__c (before insert, before update) {
    
    
   
    for (Work_Log_Sheet__c wls : trigger.new) {
           
        if(wls.Work_Update_Date__c==System.today()){
            wls.Total_Hours__c=wls.Total_Hours__c+wls.Total_Time__c;            
            
        }
    }
  
      
}


As on same object you want to update the data so DML is not require before insert will do the job...check with above code and let me know if that helps you...

 
sandeep sankhlasandeep sankhla
Hi ,

share your req so I can help you and then you can ablove code to handle update case well, as that was only for insert...
B TulasiB Tulasi
Hi Sandeep,

First time, I tried with trigger.new, that is not working. That's why, I changed the above code
sandeep sankhlasandeep sankhla
Hi,

The above code will work for sure, may be you are missing something...let me know if you are facing any error....

just take this example:
 
trigger AccountAddressTrigger on Account (Before insert, After update) 
{
    
   
    
    set<Id> setContactids = new set<Id>();
    
    for(Account obj : trigger.new)
    {
        
        obj.Current_Provider__c = obj.Current_Provider__c +   obj.Year__c;
    }
This is trigger on Account, it will simply check whatever field value is there
Current_Provider__c it will add Year__c and show the results...so while inserting if Current Provider is 5 and year is 12 then it will show the 17 after inserting...please check your code and modify accordingly...

thanks