We're looking for a way to budget for and track costs on Campaigns in Salesforce. We are looking into a solution using a custom object and some method of performing a roll-up, but I thought I would post a question to the Success Community to see if anyone is aware of an existing add-on or best practice for handling this issue. Here is some detail about what we want:
- Track individual costs on a campaign.
- Flag the costs as expected or actual
- Roll up the costs to the parent campaign.
- Roll up campaign costs all the way up through the hierarchy.
I believe we can accomplish this with a Custom object with master-detail relationship to the Campaign. However, if we want to roll-up into the standard fields like BudgetedCost and ActualCost, we'd need to use Apex or Process builder, as those are not roll-up type fields. So, before we go down that route, does anyone out there have a similar use case? If so, how did you solve it?
Thanks,
Kevin McAuliffe
I know this question was a while ago, but I had implemented a solution and am happy to share the code:
I created a custom object Campaign_Cost_Item__c with a lookup to Campaign and added a trigger:
trigger UpdateCampaignCost on Campaign_Cost_Item__c (after insert, after update) {
double newCost = 0.0;
for(Campaign_Cost_Item__c theItem: Trigger.New)
{
Id theCampaignId = theItem.campaign__c;
List<Campaign_Cost_Item__c> lstCosts = [Select cost__c from Campaign_Cost_Item__c where campaign__c = :theCampaignId];
for (Campaign_Cost_Item__c ci:lstCosts){
newCost = newCost + ci.cost__c;
}
Campaign theCampaign = [Select Id, ActualCost from Campaign where Id = :theCampaignId];
theCampaign.ActualCost = newCost;
update theCampaign;
}