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
Semira@gmail.comSemira@gmail.com 

Primary campagin source will not update on Opportunity

Hi friends, 

Once again I'm asking for help because I can't seem to figure out where the bug is. 

I have a contact field on opportunity and when there is an active campaign assigned to that contact, I want to take that id and update the primary campaign source field on opportunity. So that Campaign Influence will populate. 

However, even setting CampaignID on opportunity does not set primary campaign source. 

This is the  AFTER trigger calling the apex class:
else if (Trigger.IsInsert)
            {
    
            	for(Opportunity opp : trigger.new)
            	{
            		OppId.add(opp.id);
            	}
            	
            	CampaignAssignmentToJob.CampaignAssignmentToJob(OppId);
            }
            // otherwise it is update
            else
            {
            	for(Opportunity opp : trigger.new)
            	{
            		OppId.add(opp.id);
            	}
            	
            	CampaignAssignmentToJob.CampaignAssignmentToJob(OppID);
            }

Here's the class: 
public with sharing class CampaignAssignmentToJob {
	
	@future
	public static void CampaignAssignmentToJob (list<ID> Opty)
	{ 
		List<Opportunity> opps = [select id from Opportunity where id IN: Opty];
		
		
		Set<ID> ContactID = new Set<ID>();

		
		for(Opportunity opp: Opps)
		{
			if(opp.Contact__c != null)
			{
				ContactID.add(opp.Contact__c);

			}
		}
		
		System.debug('This is the ContactID:::::::::::::::::::::::::::::::::::::::::::::::::::' + ContactID);
		

		list <CampaignMember> CampMember = new list<CampaignMember>();
		
		if(!ContactID.isempty())
		{
			CampMember = new list<CampaignMember>([Select Id, contactid, CampaignID From CampaignMember where contactid IN: ContactID order by CreatedDate ASC]);
			
		}
		
		map<Id, List<CampaignMember>> cmMap = new map<Id, List<CampaignMember>>();
		
		//map <String, String> CampaignMap = new map<String, String>();
		
		for(CampaignMember member: CampMember)
		{
			List<CampaignMember> workList = cmMap.containsKey(member.ContactId) ? cmMap.get(member.ContactId) : new List<CampaignMember>();
			workList.add(member);
			cmMap.put(member.contactId,workList);

		}
		
		System.debug('This is the CampMember:::::::::::::::::::::::::::::::::::::::::::::::::::' + CampMember);

		list<Opportunity> OppList = new list<Opportunity>();
		
		for(Opportunity opp: Opps)
		{
			if (cmMap.containsKey(opp.contact__c))
			{
				for (CampaignMember cm: cmMap.get(opp.contact__c))
				{
					opp.CampaignID = cm.CampaignID;
					OppList.add(opp);
				}
			}
		}
		

		
		if(!OppList.isEmpty())
		{
			update OppList;	
		}
		
	
	}

}

Is there any other way I can set the primary campaign source?  I'm not getting any error, just the field isn't up dating. 
Paul S.Paul S.
Maybe a silly question here, but could it be that your method simply hadn't yet run given the @future annotation?
Semira@gmail.comSemira@gmail.com
Never mind. I was running into Future method cannot be called from a future or batch method error. So had to add a system.isfuture checkpoint before calling my class. It works now.