You need to sign in to do that
Don't have an account?
learn1.3947400898749973E12
Update A Contact Check Box when a Task is created with a specific picklist value
I can't understand why the trigger below is not working. All I am wanting to do is update a Contact checkbox when a Task is created with a custom picklist field has a specific value:
trigger UsefulActivity on Task (after insert, after update)
{
for(Task t : trigger.new)
{
for(Contact c : [select Id, Had_Useful_Activity__c from Contact where Id=:t.WhatID])
{
if(t.Result__c == 'Meeting')
{
c.Had_Useful_Activity__c = true;
}
}
}
}
trigger UsefulActivity on Task (after insert, after update)
{
for(Task t : trigger.new)
{
for(Contact c : [select Id, Had_Useful_Activity__c from Contact where Id=:t.WhatID])
{
if(t.Result__c == 'Meeting')
{
c.Had_Useful_Activity__c = true;
}
}
}
}
trigger UsefulActivity on Task (after insert, after update)
{
for(Task t : trigger.new)
{
for(Contact c : [select Id, Had_Useful_Activity__c from Contact where Id = :t.WhoID])
{
if(t.Result__c == 'Meeting')
{
c.Had_Useful_Activity__c = true;
update c;
}
}
}
}
You should not use the query inside the for loop . It may hit the governor limit . Use Set,Map Collections .
trigger UsefulActivity on Task (after insert, after update)
{
set<Id > setContact = new set<Id>();
for(Task t : trigger.new)
{
if(t.Result == 'Meeting')
{
setContact.add(t.WhoID);
}
}
List<Contact> lstContactToInsert = new List<Contact>();
for(Contact c : [select Id, Had_Useful_Activity__c from Contact where Id IN :setContact])
{
c.Had_Useful_Activity__c = true;
lstContactToInsert.add(c);
}
if(lstContactToInsert.size() > 0)
update lstContactToInsert;
}
Thanks ,
Ajay
Totally agree with @Ajay_SFDC , try to bulkify your triggers and your code to don't hit limits, appart from that you got great answers.
I attach here a link about Apex Patterns that might be help you too : https://github.com/financialforcedev/fflib-apex-common
:)