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
jellydogjellydog 

Apex trigger to update custom Account field with the count of related records in Activities

Hi All,

I am an Apex noob and have never written an Apex trigger. I suspect that is what is required to do this. I am using basic Salesforce Accounts and Activities. I have tried to find a similar post on this board but have not found anything that does what I am trying to do - as simple as it may seem ;).

 

I have a read-only custom field in Accounts called CallCount_c (type number). I have a picklist field in Activities (aka Tasks) called MyActivity_c. I would like the custom field in Accounts to reflect the count of Activity records for this account where the MyActivity_c = "Phone Call". The Activity (Task) has a 'Related To" picklist where I choose 'Account" and then select the account to which this Activity is to be related.

 

Does that make sense?

Thanks in advance for reading, replies...

 

jellydogjellydog

Thanks for pointing me there - I did not see this post when researching. Much appreciated, Steve456!

ItswasItswas

Hi Jellydog,

 

trigger calculateOpenItems on Task (after insert,before delete,before update)
{
Map<Id,Id> objSetTaskId = new Map<Id,Id>();
Set<Id> objSet = new Set<Id>();
for(Task ObjTsk : Trigger.New)
{
if(ObjTsk.MyActivity__c == 'Phone Call')
{
objSetTaskId.put(ObjTsk.Id,ObjTsk.WhatId);
objSet.add(ObjTsk.WhatId);
}
}
List<Task> TotalTsk = [Select id,CallType,Description from Task where whatId in : objSet];
if(Trigger.Isinsert)
{
for(Task objtskcnt : [Select id,CallType,Description from Task where whatId in : objSet])
{
if(objSetTaskId.get(objtskcnt.Id) != null)
{
Account objAcc = new Account(id= objSetTaskId.get(objtskcnt.Id));
objAcc.Site = String.ValueOf(TotalTsk.size());
update objAcc;
}
}
}
if(Trigger.IsUpdate)
{
// put your own logic
}
if(Trigger.IsDelete)
{

}
}

 

Please follow the aove code and put your own logic inside update and delete and let me know once you got the solution.

 

Regards,

Itswas.

jellydogjellydog

Thank you very much for the specific code. I changed the values of fields and data where I think it was appropriate to match my system. objAcc.Site threw me off. Should this be the read-only field I created in Accounts called 'Checkin_Call_Count_c'? (which I have defined as a 3 digit number field)

thanks again!

 

trigger calculateCheckinCalls on Task (after insert,before delete,before update)
{
Map<Id,Id> objSetTaskId = new Map<Id,Id>();
Set<Id> objSet = new Set<Id>();
for(Task ObjTsk : Trigger.New)
{
if(ObjTsk.CSM_Activity__c == 'Check-in call')
{
objSetTaskId.put(ObjTsk.Id,ObjTsk.WhatId);
objSet.add(ObjTsk.WhatId);
}
}
List<Task> TotalTsk = [Select id,CallType,Description from Task where whatId in : objSet];
if(Trigger.Isinsert)
{
for(Task objtskcnt : [Select id,CallType,Description from Task where whatId in : objSet])
{
if(objSetTaskId.get(objtskcnt.Id) != null)
{
Account objAcc = new Account(id= objSetTaskId.get(objtskcnt.Id));
objAcc.Site = String.ValueOf(TotalTsk.size());
update objAcc;
}
}
}
if(Trigger.IsUpdate)
{
// put your own logic

}
if(Trigger.IsDelete)
{

}
}