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
Deepak agarwal 9Deepak agarwal 9 

Incompatible element type Datetime for collection of Lead(Error)

Hi,
Need Help!!
trigger Update_modified_date on Task (after insert,after update) {      
     list<task> t=[SELECT LastModifiedDate FROM Task];
    list<lead> l=[select TaskLastModifiedDate__c from lead where Isconverted = false];
        for(task t1:t){
            //system.debug('this is ----------------------------------');
            for(lead l1:l){
                system.debug('this is ----------------------------------');
            l1.TaskLastModifiedDate__c=t1.LastModifiedDate;
            l.add(l1.TaskLastModifiedDate__c);  
                
            }
        }
    update l;
    }


Iam trying to copy last modified date of task in lead.But iam getting the following error.Please help.

Error: Incompatible element type Datetime for collection of Lead
veda shriveda shri
Hi Deepak,

Can you please explain me the requirement.

The way you are using trigger is wrong. You are using ofr insider for which is not a best practice. 

Do you want to update task last LastModifiedDate in its parent lead?



 
Deepak agarwal 9Deepak agarwal 9
Yup.correct that's what is my requirement.
Deepak agarwal 9Deepak agarwal 9
I am fetching lastmodified date from task and copying it in lead custom field TaskLastModifiedDate__c.
veda shriveda shri
Please try below code:
trigger Update_modified_date on Task (after insert,after update) {
	List<Lead> lstLeadToUpdate = new List<Lead>();
	
	for(Task objTask : trigger.new)
	{
		if((objTask.WhoId).startsWith('00Q'))
		{
			lstLeadToUpdate.add(new Lead(Id = objTask.WhoId, TaskLastModifiedDate__c = objTask.LastModifiedDate));
		}
	}
	
	if(!lstLeadToUpdate.isEmpty())
		update lstLeadToUpdate;
}

let me know if you are facing any problem.
Mahesh K 22Mahesh K 22
may i know the TaskLastModifiedDate__c is which datatype
Deepak agarwal 9Deepak agarwal 9
it is date/time
veda shriveda shri
Deepak,

have you got solution for your problem?
 
Deepak agarwal 9Deepak agarwal 9
Hi Veda thanks for help.But iam facing problem.
Startswith method signature is incorrect.It says the method doesnot exists.
veda shriveda shri
try this
trigger Update_modified_date on Task (after insert,after update) {
	List<Lead> lstLeadToUpdate = new List<Lead>();
	
	for(Task objTask : trigger.new)
	{
	    String strWhoID = objTask.WhoId;
		if(strWhoID.startsWith('00Q'))
		{
		    lstLeadToUpdate.add(new Lead(Id = objTask.WhoId, TaskLastModifiedDate__c = objTask.LastModifiedDate));
		}
	}
	
	if(!lstLeadToUpdate.isEmpty())
		update lstLeadToUpdate;
}
Deepak agarwal 9Deepak agarwal 9
Nope.Error with startswith method
Deepak agarwal 9Deepak agarwal 9
And why there is a need to create a new lead.After creating lead we will be creating task.
Deepak agarwal 9Deepak agarwal 9
Can you tell me whats wrong in my code.Actually your code is little complex for me at this level.
veda shriveda shri
I home you are asking about below code on line number 9.
new Lead(Id = objTask.WhoId, TaskLastModifiedDate__c = objTask.LastModifiedDate)

FYI, This will not create new lead. It is a way to update lead.

Task.WhoId is having lead Id so, we are making Lead.Id = Task.WhoId

then if you see line number 14 we are just updating the lead.

If you want more information, p[lease pass me your contact information like skype/email so that I can explain you.

Thanks,
Veda
veda shriveda shri
Yaa sure I can explain you.

 Please pass me your contact information like skype/email here.
Deepak agarwal 9Deepak agarwal 9
deepakagarwal483@gmail.com

 
Deepak agarwal 9Deepak agarwal 9
also there is a error in the code.Startswith
veda shriveda shri
what error in startswiths?