You need to sign in to do that
Don't have an account?
Trigger unable to update old records into custom field
Hi
I have a trigger OpenActivityCount to get the count of the open tasks created on the open activity related list. The count of the tasks appear on the No. of Open Activities field.
I am getting a problem here.
The issue is all the new tasks created after I have deployed the tigger are getting populated in the field whereas the older tasks are not being populated. I want the total count of the open tasks in the field before and after I have deployed the trigger.
Can anyone please tell me how to overcome this issue.
This is the trigger
trigger OpenActivityCount on Task(after insert,after update)
{
if(trigger.new[0].AccountId!=null)
{
Account ac=[select id, No_of_Open_Activities__c from Account where id=:trigger.new[0].AccountId];
if(ac!=null)
{
if(ac.No_of_Open_Activities__c == null)
ac.No_of_Open_Activities__c =0;
if(trigger.isinsert) {
if(trigger.new[0].status!='Completed') {
ac.No_of_Open_Activities__c +=1;
}
}
if(trigger.isupdate )
{
if(trigger.new[0].status == 'Completed')
ac.No_of_Open_Activities__c -= 0.5;
if(trigger.new[0].status != 'Completed')
ac.No_of_Open_Activities__c += 0.5;
}
update ac;
}
}
}
Hi ,
Try below given code and see if it helps youi.It is on the lead object.
trigger UpdateLeadOpenTasks on Task (after delete, after insert, after undelete,
after update) {
// Declare the variables
public set<Id> LeadIDs = new Set<Id>();
public list<Lead> LeadsToUpdate = new List<Lead>();
// Build the list of Leads to update
for(Task t: Trigger.new){
if(string.valueOf(t.WhoId).startsWith('00Q'))
LeadIDs.add(t.WhoId);
System.debug('WhoId = ' + t.WhoId);
}
// Update the Leads
if(LeadIDs.size()>0){
for(Lead l: [Select l.Id, l.Open_Tasks__c,
(Select Id From Tasks where IsClosed = False)
From Lead l where Id in :LeadIDs])
LeadsToUpdate.add(new Lead(Id=l.Id, Open_Tasks__c = l.Tasks.size()));
update LeadsToUpdate;
}
}
I am not sure if this one works as I need to do an update only once.
Please correct me if wrong.
Thanks
Finney