You need to sign in to do that
Don't have an account?
Trigger Update Lead from Task
Hi there,
i create the code blow by copy paste different triggers and add some conditions. The Code works fine for me, but i dont unterstand all lines .
trigger updateRelatedLead on Task (after insert,after update) {
List<Id> LeadIds = new List<Id>();
List<Lead> LeadList = new List<Lead>();
for(Task t :trigger.new)
{
if(t.whoId!=null)
{
Schema.SObjectType tType= t.whoId.getSObjectType();
if(tType == Lead.Schema.SObjectType)
{
LeadIds.add(t.WhoId);
}
}
}
//Querying the related Lead based on whoId on Task
Map<Id,Lead> LeadMap = new Map<Id,Lead>([select id,Time_Task__c from Lead where id in:LeadIds]);
for(Task t :Trigger.new)
for(Lead l : LeadMap.Values())
{
if(t.ActivityDate > l.Time_Task__c)
{
l.Time_Task__c = t.ActivityDate;
LeadList.add(l);
}
}
if(LeadList.size()>0)
{
update LeadList;
}
}
------------------------------------------------------------------------------------
Update LeadList; -- Do i need the if condition ? in my opinion , nope
------------------------------------------------------------------------------------
This part is like a black box ^^ I check if the task is related to the Lead, right ?
Schema.SObjectType tType= t.whoId.getSObjectType();
if(tType == Lead.Schema.SObjectType)
{
LeadIds.add(t.WhoId);
A short description would great. Thanks for your help guys !
i create the code blow by copy paste different triggers and add some conditions. The Code works fine for me, but i dont unterstand all lines .
trigger updateRelatedLead on Task (after insert,after update) {
List<Id> LeadIds = new List<Id>();
List<Lead> LeadList = new List<Lead>();
for(Task t :trigger.new)
{
if(t.whoId!=null)
{
Schema.SObjectType tType= t.whoId.getSObjectType();
if(tType == Lead.Schema.SObjectType)
{
LeadIds.add(t.WhoId);
}
}
}
//Querying the related Lead based on whoId on Task
Map<Id,Lead> LeadMap = new Map<Id,Lead>([select id,Time_Task__c from Lead where id in:LeadIds]);
for(Task t :Trigger.new)
for(Lead l : LeadMap.Values())
{
if(t.ActivityDate > l.Time_Task__c)
{
l.Time_Task__c = t.ActivityDate;
LeadList.add(l);
}
}
if(LeadList.size()>0)
{
update LeadList;
}
}
------------------------------------------------------------------------------------
Update LeadList; -- Do i need the if condition ? in my opinion , nope
------------------------------------------------------------------------------------
This part is like a black box ^^ I check if the task is related to the Lead, right ?
Schema.SObjectType tType= t.whoId.getSObjectType();
if(tType == Lead.Schema.SObjectType)
{
LeadIds.add(t.WhoId);
A short description would great. Thanks for your help guys !
Update LeadList; -- Do i need the if condition ? in my opinion , nope.. .........NO its not requerd but these if condtion ensure that if your LeadList size more than 0 so update it ,otherwise no need to update LeadList because there is no record in your list so why we update it? so its a good code pratice that will help you write efficient, scalable code.
and your are Right about your blackbox part
* A Schema.sObjectType object is returned from the field describe result using the getReferenceTo method, or from the sObject describe result using the getSObjectType method
its check if the task is related to the Lead.
thanks
If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer. :-)