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
Ankit Khurana24Ankit Khurana24 

how to move lost opportunities to a campaign.:urgent

what i need to do is:make a class to to a create a campaign for all those opportunities whose status is "closed lost" and create a campaign for them also all the contacts related to that opportunity should move to campaign member.e


Step  would be like this:

1.Create a camapign.

2.Identify lost opportunities

3.Identify contact related to those opportunities.

4.Put Idsof conatc into set,<map>

5.Add these conatacts to cmapaign members.


public class moveopportunity
List<Opportunity> selectopp;
public string campaignid1;
public string conactid;
List<CampaignMember> cmember;
public moveopportunity()
selectopp=[Select o.Id, o.CampaignId, (Select OpportunityId, ContactId From OpportunityContactRoles) From Opportunity o where Opportunity.StageName =:'Closed Lost'];
cmember=new List<CampaignMember>();

for(Opportunity O:selectopp)

O.CampaignId = '70190000000MjHs';("how to avoid hard code")
cmember=[Select c.ContactId, c.CampaignId From CampaignMember c where CampaignId =:'70190000000MjHs'];
for(integer i=0;i<O.OpportunityContactRoles.size();i++)
cmember[0].ContactId= O.OpportunityContactRoles[i].ContactId ;
update O;
update cmember;

public void move()




please help the above code in

1Avoid.hard code the campaign..i need to create it..

2. How to fetch contacts from opportunities because opportuniti>>>account>>contact is relationship to add conatcts obtained in sets to campaign member..