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
LloydSilverLloydSilver 

Trigger to compare old field to new field and pass list to class

I'd like this trigger to check a contact's old (before update) value of a field to the new value, and if it has changed, AND if the new field is of a certain value, to then pass the list of contacts to a class.

 

Below is my first draft of the code and it's not working. I've also included the class just in case there's an issue there, but that did deploy successfully.

 

The error I'm getting is:

 

Method does not exist or incorrect signature: UpdateOpportunityStageOnContact.agentstatus(LIST<Id>)

 

So I don't think my call to the class is formatted correctly. Or I'm not using the list correctly.

 

Thanks!

 

trigger AgentStatusChange on Contact (after update) {
	
	List<Id> contactsToUpdate = new List<Id>{};
	
	for (Contact agents: Trigger.new){
		Contact oldContact = Trigger.oldMap.get(agents.ID);
		if (agents.Agent_Status__c != oldContact.Agent_Status__c){
			if (agents.Agent_Status__c == 'SureLC Packet Complete' || agents.Agent_Status__c == 'Carrier Contracting Complete' || agents.Agent_Status__c == 'First Case Submitted' || agents.Agent_Status__c == 'Client'){
				
				contactsToUpdate.add(agents.ID);
			}
		}
	}
	
	if(!contactsToUpdate.isEmpty())
	UpdateOpportunityStageOnContact.agentstatus(contactsToUpdate);
	
}

 

public class UpdateOpportunityStageOnContact {
	
		public static void agentstatus(Contact[] contactsToUpdate) {
			
			List<Id> oppIdSureLC = new List<Id>{};
			List<Opportunity> oppsToUpdateSureLC = new List<Opportunity>{};
			List<Id> oppIdContracting = new List<Id>{};
			List<Opportunity> oppsToUpdateContracting = new List<Opportunity>{};
			List<Id> oppIdFirst = new List<Id>{};
			List<Opportunity> oppsToUpdateFirst = new List<Opportunity>{};
			List<Id> oppIdClient = new List<Id>{};
			List<Opportunity> oppsToUpdateClient = new List<Opportunity>{};
			
			for (Contact con :contactsToUpdate){
				
				if (con.Agent_Status__c == 'SureLC Packet Complete'){
					
					for(OpportunityContactRole ocr: [select OpportunityID from OpportunityContactRole where ContactID = :con.Id]){
						oppIdSureLC.add(ocr.OpportunityID);
					}
					
					for(Opportunity opp: [select ID, StageName from Opportunity where ID = :oppIdSureLC]){
						opp.StageName = 'SureLC Packet Complete';
						oppsToUpdateSureLC.add(opp);
					}
					
				}
				
				
				if (con.Agent_Status__c == 'Carrier Contracting Complete'){
					
					for(OpportunityContactRole ocr: [select OpportunityID from OpportunityContactRole where ContactID = :con.Id]){
						oppIdContracting.add(ocr.OpportunityID);
					}
					
					for(Opportunity opp: [select ID, StageName from Opportunity where ID = :oppIdContracting]){
						opp.StageName = 'Carrier Contracting Complete';
						oppsToUpdateContracting.add(opp);
					}
					
				}
				
				if (con.Agent_Status__c == 'First Case Submitted'){
					
					for(OpportunityContactRole ocr: [select OpportunityID from OpportunityContactRole where ContactID = :con.Id]){
						oppIdFirst.add(ocr.OpportunityID);
					}
					
					for(Opportunity opp: [select ID, StageName from Opportunity where ID = :oppIdFirst]){
						opp.StageName = 'First Case Submitted';
						oppsToUpdateFirst.add(opp);
					}
					
				}
				
				if (con.Agent_Status__c == 'Client'){
					
					for(OpportunityContactRole ocr: [select OpportunityID from OpportunityContactRole where ContactID = :con.Id]){
						oppIdClient.add(ocr.OpportunityID);
					}
					
					for(Opportunity opp: [select ID, StageName from Opportunity where ID = :oppIdClient]){
						opp.StageName = 'Closed Won';
						oppsToUpdateClient.add(opp);
					}
					
				}
				
			}
			
			if(!oppsToUpdateSureLC.isEmpty())
			update oppsToUpdateSureLC;
			
			if(!oppsToUpdateContracting.isEmpty())
			update oppsToUpdateContracting;
			
			if(!oppsToUpdateFirst.isEmpty())
			update oppsToUpdateFirst;
			
			if(!oppsToUpdateClient.isEmpty())
			update oppsToUpdateClient;
			
		}

}

 

 

 

k_bentsenk_bentsen

Your method definition in your class takes a list of Contact object as a parameter, but in your trigger you're passing a list of Ids. You'll want to update your code accordingly.

LloydSilverLloydSilver

I re-wrote the trigger to use a Contact List versus ID List. But I guess that wasn't enough. Would you mind elaborating on how to do what you suggest. The updated code I tried (unsuccessfully) is below.

 

trigger AgentStatusChange on Contact (after update) {
	
	List<Contact> contactsToUpdate = new List<Contact>{};
	
	for (Contact agents: Trigger.new){
		Contact oldContact = Trigger.oldMap.get(agents.ID);
		if (agents.Agent_Status__c != oldContact.Agent_Status__c){
			if (agents.Agent_Status__c == 'SureLC Packet Complete' || agents.Agent_Status__c == 'Carrier Contracting Complete' || agents.Agent_Status__c == 'First Case Submitted' || agents.Agent_Status__c == 'Client'){
				
				contactsToUpdate.add(agents.ID);
			}
		}
	}
	
	if(!contactsToUpdate.isEmpty())
	UpdateOpportunityStageOnContact.agentstatus(contactsToUpdate);
	
}

 

LloydSilverLloydSilver

Ok I think I figured it out. Code below. I had to change the List from ID to Contact, and also simply add the contact (using just the variable 'agents' versus adding the ID using 'agents.Id'.

 

trigger AgentStatusChange on Contact (after update) {
	
	List<Contact> contactsToUpdate = new List<Contact>{};
	
	for (Contact agents: Trigger.new){
		Contact oldContact = Trigger.oldMap.get(agents.ID);
		if (agents.Agent_Status__c != oldContact.Agent_Status__c){
			if (agents.Agent_Status__c == 'SureLC Packet Complete' || agents.Agent_Status__c == 'Carrier Contracting Complete' || agents.Agent_Status__c == 'First Case Submitted' || agents.Agent_Status__c == 'Client'){
				
				contactsToUpdate.add(agents);
			}
		}
	}
	
	if(!contactsToUpdate.isEmpty())
	UpdateOpportunityStageOnContact.agentstatus(contactsToUpdate);
	
}