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
Leo DarkstarLeo Darkstar 

Can I call this class with a trigger ?

I'm trying to call this class which uses @Future with a trigger. I'm unsure how to reference the class in the trigger. Also, since I am attempting to trigger the code with a field change it is difficult because I can't have the callout execute after an update. So, I'm not sure what trigger context should be used as well. 

 

Here is my trigger : 

trigger Trigger_ProgramContactRole on Program_Contact_Role__c (after insert, after update, before insert, before update ,after delete) {
    //Checking for the event type
    if(Trigger.isAfter){
        
        
        if(Trigger.isInsert){
                     
                        
        }
        
        
        if(Trigger.isUpdate){
                     
            
            
        }


But I don't know how to start this class with it : 

public class UpdateContactPCRController {

    @InvocableMethod
    public static String  updateContact(String recordId){        
       
        String contactId = '';       
        
        List<Contact> contactToBeupdate = new List<Contact>();        
        
        if(String.isNotBlank(recordId)){            
            
            List<Program_Contact_Role__c> programContacList = [SELECT Id,Contact__c,Program_Name__c FROM Program_Contact_Role__c WHERE Id =:recordId AND Contact__c != null];
            contactId = programContacList[0].Contact__c;
            
            if(String.isNotBlank(contactId)){
                
                contactToBeupdate = [Select Id,Pardot_Action_Trigger__c,PCR_Register_Button_Link__c,PCR_URL_for_UI__c FROM Contact Where Id =: contactId Limit 1];                
                
                contactToBeupdate[0].Program_Contact_Role_Id__c = recordId;
            }
                        List<Program_Communication_Recipients__c> programCommunicationRecs = [Select Id,Name,
                                  Program_Communication__r.Program__r.Buyer__r.Name,Receipient__c,                
                                              From Program_Communication_Recipients__c Where 
                                                           Program_Communication__r.Program__c =: programContacList[0].Program_Name__c AND 
                                                           Receipient__c =: programContacList[0].Id];                                           
                                
                
                    contactToBeupdate[0].PCR_Welcome_Message__c = String.valueOf(programCommunicationRecs[0].Program_Communication__r.Welcome_Message__c);                                                    

 pardotCallout(contactToBeupdate[0].Id);
    {

        @future(callout=true) 
        public static void pardotCallout(String contactId) {
            String returnedResponseFromPardot = Http_Utility_Pardot.pardotCreateProspect(new Set<Id> {contactId});
            // new treatment here with the returned value included DML operations.
    }

        if(contactToBeupdate.size() > 0){
            update contactToBeupdate;  
        }
        
        return 'Updated Successfully';
    }
}

Any help you can give would be very appreciate. Thank you.
Best Answer chosen by Leo Darkstar
vishal-negandhivishal-negandhi

Hi Leo, 

Try the below code

 

public class UpdateContactPCRController {

    @InvocableMethod
    public static String  updateContact(String recordId){        
       
        String contactId = '';       
        
        List<Contact> contactToBeupdate = new List<Contact>();        
        
        if(String.isNotBlank(recordId)){            
            
            List<Program_Contact_Role__c> programContacList = [SELECT Id,Contact__c,Program_Name__c FROM Program_Contact_Role__c WHERE Id =:recordId AND Contact__c != null];
            contactId = programContacList[0].Contact__c;
            
            if(String.isNotBlank(contactId)){
                
                contactToBeupdate = [Select Id,Pardot_Action_Trigger__c,PCR_Register_Button_Link__c,PCR_URL_for_UI__c FROM Contact Where Id =: contactId Limit 1];                
                
                contactToBeupdate[0].Program_Contact_Role_Id__c = recordId;
            }
			List<Program_Communication_Recipients__c> programCommunicationRecs = [Select Id,Name,
					  Program_Communication__r.Program__r.Buyer__r.Name,Receipient__c,                
								  From Program_Communication_Recipients__c Where 
											   Program_Communication__r.Program__c =: programContacList[0].Program_Name__c AND 
											   Receipient__c =: programContacList[0].Id];                                           
					
	
			contactToBeupdate[0].PCR_Welcome_Message__c = String.valueOf(programCommunicationRecs[0].Program_Communication__r.Welcome_Message__c);                                                    
			
			if(contactToBeupdate.size() > 0){
				update contactToBeupdate;  
			}
			pardotCallout(contactToBeupdate[0].Id);
    
			return 'Updated Successfully';
		}
		else{
			return 'some message';
		}
    }
	
	@future(callout=true) 
	public static void pardotCallout(String contactId) {
		String returnedResponseFromPardot = Http_Utility_Pardot.pardotCreateProspect(new Set<Id> {contactId});
		// new treatment here with the returned value included DML operations.
		// further logic if any based on the response you get from Pardot
	}
}


pardotCallout needs to be defined as a separate method. 

 

Hope this helps. 

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Leo,

The below article mentions a way to call a future method where callout is true.

>> https://www.mstsolutions.com/technical/passing-sobject-to-future-methods-in-salesforce/#:~:text=The%20Future%20Annotation%20is%20used,only%20return%20void%20data%20type.

>> https://help.salesforce.com/articleView?id=000330691&type=1&mode=1

I hope this helps and in case if this comes in handy can you please choose this as the best answer so that it can be useful for others in the future.

Regards,
Anutej
Leo DarkstarLeo Darkstar

Anutej,

I've looked through those articles and I still don't see what is wrong with my code. I moved the IF( up to be within the same section as @future and I am still getting the error : 

pardotCallout(contactToBeupdate[0].Id);
                {        
                  
                    @future(callout=true) 
                    public static void pardotCallout(String contactId) {
                        String returnedResponseFromPardot = Http_Utility_Pardot.pardotCreateProspect(new Set<Id> {contactId});
                        // new treatment here with the returned value included DML operations.
                        if(contactToBeupdate.size() > 0){
                        update contactToBeupdate; 
                        } 
                   }     
                        
                }

 

The future method appears to be in correct syntax. 

 

vishal-negandhivishal-negandhi

Hi Leo, 

Try the below code

 

public class UpdateContactPCRController {

    @InvocableMethod
    public static String  updateContact(String recordId){        
       
        String contactId = '';       
        
        List<Contact> contactToBeupdate = new List<Contact>();        
        
        if(String.isNotBlank(recordId)){            
            
            List<Program_Contact_Role__c> programContacList = [SELECT Id,Contact__c,Program_Name__c FROM Program_Contact_Role__c WHERE Id =:recordId AND Contact__c != null];
            contactId = programContacList[0].Contact__c;
            
            if(String.isNotBlank(contactId)){
                
                contactToBeupdate = [Select Id,Pardot_Action_Trigger__c,PCR_Register_Button_Link__c,PCR_URL_for_UI__c FROM Contact Where Id =: contactId Limit 1];                
                
                contactToBeupdate[0].Program_Contact_Role_Id__c = recordId;
            }
			List<Program_Communication_Recipients__c> programCommunicationRecs = [Select Id,Name,
					  Program_Communication__r.Program__r.Buyer__r.Name,Receipient__c,                
								  From Program_Communication_Recipients__c Where 
											   Program_Communication__r.Program__c =: programContacList[0].Program_Name__c AND 
											   Receipient__c =: programContacList[0].Id];                                           
					
	
			contactToBeupdate[0].PCR_Welcome_Message__c = String.valueOf(programCommunicationRecs[0].Program_Communication__r.Welcome_Message__c);                                                    
			
			if(contactToBeupdate.size() > 0){
				update contactToBeupdate;  
			}
			pardotCallout(contactToBeupdate[0].Id);
    
			return 'Updated Successfully';
		}
		else{
			return 'some message';
		}
    }
	
	@future(callout=true) 
	public static void pardotCallout(String contactId) {
		String returnedResponseFromPardot = Http_Utility_Pardot.pardotCreateProspect(new Set<Id> {contactId});
		// new treatment here with the returned value included DML operations.
		// further logic if any based on the response you get from Pardot
	}
}


pardotCallout needs to be defined as a separate method. 

 

Hope this helps. 

This was selected as the best answer
Leo DarkstarLeo Darkstar

Thank you very much for this. Do I need to include these lines within that method ?

if(contactToBeupdate.size() > 0){
            update contactToBeupdate;  
        }
 

 
vishal-negandhivishal-negandhi
You're not making any further changes to contactToBeupdate, so I would say no. 
Leo DarkstarLeo Darkstar
Thank you so much. How can I call that whole class from within that trigger I included ? 
vishal-negandhivishal-negandhi

You cannot call the whole class technically, but since all your logic is inside methods and maybe constructor, you can call them.

Please mark the answer that helped you fix this as the best answer.

 

Leo DarkstarLeo Darkstar
I am calling the class with a Process Builder now. But I'm not sure that the method with @Future at the end of the class is running. I cannot see it in a debug log, and I cannot see anything in Apex Jobs. The rest of the code looks to be running correctly. Is there something preventing it from running ? Or is there a better way for me to see @Future code has run ?