You need to sign in to do that
Don't have an account?
MOHAMMED AL IMAM
Change the below code trigger to helper class and provide test class also
trigger campaigntrigger on CampaignMember (after insert) { Map<id,List<opportunity>> map1 = new Map<id,List<opportunity>>(); set<id> conSet =new set<id>(); for(CampaignMember c :trigger.new ){ conSet.add(c.contactId); } for(opportunity cm: [select id,contactId,Name,StageName from opportunity where contactid in: conSet]){ List<opportunity> oppList; if(!map1.containsKey(cm.contactId)){ oppList = new List<opportunity> (); oppList.add(cm); map1.put(cm.contactId, oppList); } else{ oppList = map1.get(cm.contactId); oppList.add(cm); map1.put(cm.contactId,oppList); } } List<Opportunity> oppsToUpdate = new List<Opportunity>(); for(CampaignMember cm : trigger.new){ if(cm.ContactId != NULL){ if(map1.get(cm.ContactId) != NULL){ for(Opportunity op : map1.get(cm.ContactId)){ op.CampaignId = cm.CampaignId; oppsToUpdate.add(op); } } } } if(oppsToUpdate.size() > 0){ update oppsToUpdate; } }
When campaign member is created its related opportunity should be counted and once that campaign member is removed that its related opportunity count should be decreased.
Change the below code trigger to helper class and provide test class also
trigger campaigntrigger on CampaignMember (after insert) { Map<id,List<opportunity>> map1 = new Map<id,List<opportunity>>(); set<id> conSet =new set<id>(); for(CampaignMember c :trigger.new ){ conSet.add(c.contactId); } for(opportunity cm: [select id,contactId,Name,StageName from opportunity where contactid in: conSet]){ List<opportunity> oppList; if(!map1.containsKey(cm.contactId)){ oppList = new List<opportunity> (); oppList.add(cm); map1.put(cm.contactId, oppList); } else{ oppList = map1.get(cm.contactId); oppList.add(cm); map1.put(cm.contactId,oppList); } } List<Opportunity> oppsToUpdate = new List<Opportunity>(); for(CampaignMember cm : trigger.new){ if(cm.ContactId != NULL){ if(map1.get(cm.ContactId) != NULL){ for(Opportunity op : map1.get(cm.ContactId)){ op.CampaignId = cm.CampaignId; oppsToUpdate.add(op); } } } } if(oppsToUpdate.size() > 0){ update oppsToUpdate; } }
Hi Mohammed,
Can you please provide the code in proper format so that it becomes easy for us to read.
regards,
Priya Ranjan
When campaign member is created its related opportunity should be counted and once that campaign member is removed that its related opportunity count should be decreased.
Yes I'm reposting the code
trigger campaigntrigger on CampaignMember (before insert) {
Map<id,List<opportunity>> map1 = new Map<id,List<opportunity>>();
set<id> conSet =new set<id>();
for(CampaignMember c :trigger.new ){
conSet.add(c.contactId);
}
for(opportunity cm: [select id,contactId,Name,StageName from opportunity where contactid in: conSet]){
List<opportunity> oppList;
if(!map1.containsKey(cm.contactId)){
oppList = new List<opportunity> ();
oppList.add(cm);
map1.put(cm.contactId, oppList);
}
else{
oppList = map1.get(cm.contactId);
oppList.add(cm);
map1.put(cm.contactId,oppList);
}
}
List<Opportunity> oppsToUpdate = new List<Opportunity>();
for(CampaignMember cm : trigger.new){
if(cm.ContactId != NULL){
if(map1.get(cm.ContactId) != NULL){
for(Opportunity op : map1.get(cm.ContactId)){
op.CampaignId = cm.CampaignId;
oppsToUpdate.add(op);
}
}
}
}
if(oppsToUpdate.size() > 0){
update oppsToUpdate;
}
}