You need to sign in to do that
Don't have an account?
MOHAMMED AL IMAM
trigger the count the total no of opportunities associated with contact in campaign object,when campaign member is created ?
In this code:
trigger campaigntrigger on CampaignMember (after insert) {
Map<id,List<opportunity>> map1 = new Map<id,List<opportunity>>();
List<CampaignMember> newMembersList = trigger.new;
set<id> conSet =new set<id>();
for(CampaignMember c :newMembersList ){
conSet.add(c.contactId);
}
List<opportunity> oppList = [select id,contactId,Name,StageName from opportunity where contactid in: conSet];
for(opportunity cm: oppList)
{ if(!map1.containsKey(cm.contactId)){
List<opportunity> conLi = new List<opportunity> ();
conLi.add(cm);
map1.put(cm.contactId, conLi);
}
else{
List<opportunity> conL = map1.get(cm.contactId);
conL.add(cm);
map1.put(cm.contactId,conL);
}for(Id i: map1.keyset())
{
system.debug('Contact id- '+i+' list of Opportunity- '+map1.get(i));
}
}
}
trigger campaigntrigger on CampaignMember (after insert) {
Map<id,List<opportunity>> map1 = new Map<id,List<opportunity>>();
List<CampaignMember> newMembersList = trigger.new;
set<id> conSet =new set<id>();
for(CampaignMember c :newMembersList ){
conSet.add(c.contactId);
}
List<opportunity> oppList = [select id,contactId,Name,StageName from opportunity where contactid in: conSet];
for(opportunity cm: oppList)
{ if(!map1.containsKey(cm.contactId)){
List<opportunity> conLi = new List<opportunity> ();
conLi.add(cm);
map1.put(cm.contactId, conLi);
}
else{
List<opportunity> conL = map1.get(cm.contactId);
conL.add(cm);
map1.put(cm.contactId,conL);
}for(Id i: map1.keyset())
{
system.debug('Contact id- '+i+' list of Opportunity- '+map1.get(i));
}
}
}
Please follow the below code:-
Please mark it as Best Answer if it helps you.
Thanks & Regards
Suraj Tripathi
Hi can you provide the test class for below code:
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); system.debug('success' +map1); } else{ oppList = map1.get(cm.contactId); oppList.add(cm); map1.put(cm.contactId,oppList); system.debug('fail' +map1); } } 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; } }
Thanks & Regards
MOHAMMED AL IMAM
Please mark it as Best Answer if it helps you.
Thanks & Regards
Suraj Tripathi