You need to sign in to do that
Don't have an account?
phiberoptik
Easy question for developer - Why this error?
trigger ContactRollUpCampaigns on Campaign (after delete, after insert, after update) { //Limit the size of list by using Sets which do not contain duplicate elements set ContactIds = new set(); //When adding new Campaigns or updating existing Campaigns if(trigger.isInsert || trigger.isUpdate){ for(Campaign p : trigger.new){ ContactIds.add(p.Contact__c); } } //When deleting Campaigns if(trigger.isDelete){ for(Campaign p : trigger.old){ ContactIds.add(p.Contact__c); } } //Map will contain one Contact Id to one sum value map ContactMap = new map (); //Produce a sum of Campaigns and add them to the map //use group by to have a single Contact Id with a single sum value for(AggregateResult q : [select Contact__c,sum(Amount__c) from Campaign where Contact__c IN :ContactIds group by Contact__c]){ ContactMap.put((Id)q.get('Contact__c'),(Double)q.get('expr0')); } List ContactsToUpdate = new List(); //Run the for loop on Contact using the non-duplicate set of Contact Ids //Get the sum value from the map and create a list of Contacts to update for(Contact c : [Select Id, Total_Campaigns__c from Contact where Id IN :ContactIds]){ Double Campaignsum = ContactMap.get(c.Id); c.Total_Campaigns__c = Campaignsum; ContactsToUpdate.add(c); } update ContactsToUpdate; }
This is actually code from http://www.anthonyvictorio.com/salesforce/roll-up-summary-trigger/ that I have manipulated to try and create a RUS function on the Contact object to count the Campaigns they are added to. Clearly I am not a developer or I would understand why the error is throwing for me.
Error: Compile Error: expecting a semi-colon, found 'ContactIds' at line 4 column 6
Change your code from
set ContactIds = new set();
to
set <Id>ContactIds = new set<Id>();
set<Id> ContactIds = new set();
set<Id> ContactIds = new set<Id>();
Thank you both! Now I get:
Error: Compile Error: unexpected token: 'map' at line 21 column 2
FYI - I am liberal with my Kudos... Karma right?
Change your code to :
map<Id,double> ContactMap = new map<Id,double> ();
Also,you need to change the list as :-
List<Contact> ContactsToUpdate = new List<Contact>();
Thank you again.
Now this confused me because I couldnt understand why the original creator of the code referenced Contact__c or Account__c. Can you figure out what this should be?
Error: Compile Error: Invalid field: 'Contact__c' at line 25 column 27
In the original code,
Contact__c is a lookup fieild from Contact to Campaign.You will have to replace it with your field(Use API name of the field).
Ok so please try to help me understand. The idea of this trigger is to count the number of Campaigns the Contact is related to. Why would there be a lookup field to Campaign then?
The original trigger was to count the number of related custom object records on an Opportunity. I replaced Opportunity with Contact and the custom object with Campaign. So the original trigger had:
Why would they have a custom lookup on the Master object and be summing its parent object?
Contact__c is a custom relationship field ,it could be a lookup or master detail
Hi Phiberoptik,
First confirm two field on campaign Contact__c (Lookup to Contact) and Amount__c (Number) is available or not. if not then make it.
Second Confirm Total_Campaigns__c (Number) on Contact is available or not if not then make it.
Then you can use following corrected code. I hope it helps you.
trigger ContactRollUpCampaigns on Campaign (after delete, after insert, after update) {
//Limit the size of list by using Sets which do not contain duplicate elements
set<Id> ContactIds = new set<id>();
//When adding new Campaigns or updating existing Campaigns
if(trigger.isInsert || trigger.isUpdate){
for(Campaign p : trigger.new){
ContactIds.add(p.Contact__c);
}
}
//When deleting Campaigns
if(trigger.isDelete){
for(Campaign p : trigger.old){
ContactIds.add(p.Contact__c);
}
}
//Map will contain one Contact Id to one sum value
map<id,Double> ContactMap = new map<id,Double>();
//Produce a sum of Campaigns and add them to the map
//use group by to have a single Contact Id with a single sum value
for(AggregateResult q : [select Contact__c,sum(Amount__c)
from Campaign where Contact__c IN :ContactIds group by Contact__c]){
ContactMap.put((id)q.get('Contact__c'),(Double)q.get('expr0'));
}
List<contact> ContactsToUpdate = new List<contact>();
//Run the for loop on Contact using the non-duplicate set of Contact Ids
//Get the sum value from the map and create a list of Contacts to update
for(Contact c : [Select Id, Total_Campaigns__c from Contact where Id IN :ContactIds]){
Double Campaignsum = ContactMap.get(c.Id);
c.Total_Campaigns__c = Campaignsum;
ContactsToUpdate.add(c);
}
try{
update ContactsToUpdate;
}catch(DMLException de){ System.debug('Exception is '+de); }
}
If this is your solution then mark it as a solution and others can take help for similar issues.
B K, thank you so much. If there is not Contact nor Amount fields on the Campaign object, how would this code be changed?
Hi Phiberoptik,
AggregateResult q : [select Contact__c,sum(Amount__c) from Campaign where Contact__c IN :ContactIds group by Contact__c])
The above soql query needs relationship to contact and also you sum all amount campaign which is refer to same contact.
So, without creating two fields on campaign you can not do query for campaign object.
If this is your solution then mark it as solution and it helps other for similar requirement.