+ Start a Discussion
t.deepthi05t.deepthi05 

task activity date

Hi,

I am updating my task activity date in lead using a trigger when ever i am creating a new task record on the lead object

but the activity date is wrongly updated in the lead

can i know why and here is my code

if(!leadIds.isEmpty())
        {
             leadsToUpdate = new list<Lead>([Select Id, Name, Activity_Date__c, (Select Id, ActivityDate From Tasks limit 100) From Lead Where Id in : leadIds limit 1]);
        }
        if(!leadsToUpdate.isEmpty()){
            for(Lead l: leadsToUpdate)
            {
                for(Task t : l.Tasks)
                {
                    if (t.ActivityDate > Date.today())
                    {
                     system.debug('t.ActivityDate'+t.ActivityDate);
                        l.Activity_Date__c = t.ActivityDate;
                    }
                }
            }
        update leadsToUpdate;
        }
ShashForceShashForce
Hi,

Please try adding "l" to a different list and update that list, instead of the leadsToUpdate list. something like this:

list<lead> updateLeads = new list<lead>();
if(!leadIds.isEmpty())
        {
             leadsToUpdate = new list<Lead>([Select Id, Name, Activity_Date__c, (Select Id, ActivityDate From Tasks limit 100) From Lead Where Id in : leadIds limit 1]);
        }
        if(!leadsToUpdate.isEmpty()){
            for(Lead l: leadsToUpdate)
            {
                for(Task t : l.Tasks)
                {
                    if (t.ActivityDate > Date.today())
                    {
                     system.debug('t.ActivityDate'+t.ActivityDate);
                        l.Activity_Date__c = t.ActivityDate;
                        updateLeads.add(l);
                    }
                }
            }
        
        }
        if(updateLeads.size()>0) update updateLeads;

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank
magicforce9magicforce9
Hi,

This is a small trigger, see my code below.
trigger taskTrigger on Task (before insert, before update){
List<Lead> leadsToUpdate = new List<Lead>();
for(Task t : trigger.new){
    /***
    As per salesforce documentation http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_task.htm#topic-title
    if the task is related to Lead then WhatId must be empty
    ***/
    if(t.WhatId == null && t.ActivityDate > Date.today())){
    //You don't need to query for Leads because we already have lead Id and Activity date
    Lead l = new Lead(id = t.WhoId, Activity_Date__c = t.ActivityDate);
    leadsToUpdate.add(t);
    }
if(!leadsToUpdate.isEmpty()) update leadsToUpdate;
}

t.deepthi05t.deepthi05
I am able to update the lead but it is update with the wrong value.not the same as the due date 
ShashForceShashForce
Hi,

Can you explain with details the issue you are facing? Like what date you are seeing and what date should be appearing?

Thanks,
Shashank
t.deepthi05t.deepthi05
The due date is give as 5/10/2014 in task,

The udpate date in lead is 5/9/2014 5:00PM

I hope  it is  formating the date in a diffrent time zone and updating the lead
ShashForceShashForce
That is correct. The system follows UTC timezone. Please check the timezone of the logged in user and and compare times with UTC and check if they are correct.
t.deepthi05t.deepthi05
can i how i can change the time zone to the user timer zone and update the record
ShashForceShashForce
Hi,

Unfortunately, we cannot change the system timezone preference from UTC to the user's timezone. The system will always run on UTC.

Depending on the timezone of the user, the user will see the date/time as per his timezone.

Also, one more important thing to note here is that the activitydate field on tasks is a date field, and not a date/time field. It does not hold the time. So, when it is assigned to a date/time field, the time will always be 00:00 UTC. This cannot be changed.

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank
t.deepthi05t.deepthi05
now how i add the task due date  same in the lead object
t.deepthi05t.deepthi05
The Acitvity date contains a date time value even the datatype is date
ShashForceShashForce
It will ofcourse add the same date in the lead object, but since the user's timezone is different, he will see a different date. If you change the timezone of the user to UTC (GMT), you will see that the date is same.

As a workaround, if your users are always in a timezone behind UTC, you can do a "activitydate+1", so that they will see your desired date. Something like:

l.Activity_Date__c = t.ActivityDate + 1 ;
t.deepthi05t.deepthi05
+1 is adding is giving the correct date but wrong year  and time
ShashForceShashForce
Please try this:

l.Activity_Date__c = t.ActivityDate.addDays(1);

This will add a day but will not change the year.

The time will always be 00:00 GMT for activityDate field. To add your desired time to your lead activity date field, you can use the datetime instanec methods like addHours(), addMinutes(), addSeconds() etc. Please see this for more help: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_datetime.htm

something like

l.Activity_Date__c = t.ActivityDate.addDays(1);
l.Activity_Date__c = l.Activity_Date__c.addHours(7);