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
JosephTJosephT 

Compile Error on Campaign Roll-Up Summary Trigger

I am receiving the error "Error: Compile Error: Variable does not exist: Campaigns at line 15 column 21" for this line of code in my trigger;

LINE 15: for (Campaign c : Campaigns.values())

HERE IS MY FULL CODE:trigger TotalCampaignServicePackagesAmountforTitleServices on Opportunity (after insert, after update) {


  List<Campaign> campIds = new List<Campaign>();
  for (Opportunity o : trigger.new){
   if (o.CampaignID != null && (trigger.isInsert || o.Service_Packages_Amount__c != trigger.oldMap.get(o.id).Service_Packages_Amount__c))
   campIds.add(o.Campaign);
  }

  if (campIds.size() > 0) {

  Map<Id,Campaign> mapCampaigns = new Map<Id,Campaign>([select id from Campaign where Id in :campIds]);


  for (Campaign c : Campaigns.values())
  c.Total_Value_Services__c = 0;


  for (Opportunity o : [SELECT id, Service_Packages_Amount__c, CampaignId FROM Opportunity WHERE CampaignId =: campIds]){
   Campaign c = campaigns.get(o.CampaignId); c.Total_Value_Services__c += o.Service_Packages_Amount__c;
  }

  update campaigns.values();
  }
}
Any help would be greatly appreciated.




Gupta.VishalGupta.Vishal
you have to use mapCampaigns.values()  inside for loop . 
that shoud resolve the problem 

Thanks
JosephTJosephT
Vishal,

Thank you for your insight and feedback.

I updated my code based on your feedback and I am getting this error; Error: Compile Error: Invalid field Service_Packages_Amount__c for SObject Campaign at line 6 column 110

For this line; if (o.CampaignID != null && (trigger.isInsert || o.Service_Packages_Amount__c != trigger.oldMap.get(o.id).Service_Packages_Amount__c))

Here is the updated code;

trigger TotalCampaignServicePackagesAmountforTitleServices on Campaign (after insert,after update,after delete) {


List campIds = new List();
for (Opportunity o : trigger.new){
if (o.CampaignID != null && (trigger.isInsert || o.Service_Packages_Amount__c != trigger.oldMap.get(o.id).Service_Packages_Amount__c))
campIds.add(o.Campaign);
}

if (campIds.size() > 0) {

Map mapCampaigns = new Map([select id from Campaign where Id in :campIds]);


for (Campaign c : mapCampaigns.values())
c.Total_Value_Services__c = 0;


for (Opportunity o : [SELECT id, Service_Packages_Amount__c, CampaignId FROM Opportunity WHERE CampaignId =: campIds]){
Campaign c = mapCampaigns.get(o.CampaignId); c.Total_Value_Services__c += o.Service_Packages_Amount__c;
}

update mapCampaigns.values();
}
}

Thoughts?
________________________________________

Gupta.VishalGupta.Vishal
for (Opportunity o : trigger.new){
if (o.CampaignID != null && (trigger.isInsert || o.Service_Packages_Amount__c != trigger.oldMap.get(o.id).Service_Packages_Amount__c))
campIds.add(o.Campaign);
}



above code is not correct as you are writing the trigger on Campaign and looping through opportunity .

and this line will give you the Campiagn object not the opportunity trigger.oldMap.get(o.id)

that is why it is saying field is not available . 

Thanks
JosephTJosephT
Vishal, thank you.  I originally had the trigger for "Campaign" but, when I udpated the mapCampaigns.values(), it told me that I needed to reference "Opportunity" and not "Campaign".  So, I updated it and then received the "Error: Compile Error: Invalid field Service_Packages_Amount__c for SObject Campaign at line 6 column 110" error message.

If I am trying to roll-up a Opportunity custom field to a Campaign level field, wouldn't I need to run the trigger against the campaign?

Joseph