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
Nathan WylderNathan Wylder 

How can I call entire apex class (all methods) from trigger?

I have a process whereby whenever a specific campaign member object is created or a specific case is closed, I'd like the class to run and update 2 fields on the related contact object. But I'm stumped. How do I write the trigger to call this class? It seems like all examples I can can find online are very simple (only involve 1 object), but I have 3.
A. How can I make sure this code runs appropriately?
B. Do I need a trigger on the Campaign Member object, Case Member object or the Contact? Or all 3? 

Here's the class:
 
public class MobilizationCredit {
	//Start when Qualifying Case is edited, closed,        deleted OR
    //Start when Qualifying Campaign Member record is created, edited, deleted
      
    private date MaxDate(Contact c){
    	//Get max Date Begins value related to the Volunteer__c.contactId__r record
        Date casemaxdate2;
        Date campmaxdate2;
        string VolunteerId = c.Id;
        
 	  	List<AggregateResult> CaseMaxDate = [SELECT MAX(Date_Begins__c) FROM Case WHERE Volunteer__c = :VolunteerId AND Qualifies_for_Mobilization__c = TRUE];
		for (AggregateResult ar: CaseMaxDate) {
			system.debug('Max case date found was: '+ ar.get('expr0') + '.');
        	casemaxdate2 = Date.valueOf(ar.get('expr0'));
        }
    
     	//Get max Start Date value of all qualifying campaign member records related to the Contact.Id record
	  	List<AggregateResult> CampMemMaxDate = [SELECT MAX(Start_Date__c) FROM CampaignMember WHERE ContactId = :VolunteerId AND Campaign_Type__c = 'Weekly Attendance'];
		for (AggregateResult ar : CampMemMaxDate) {
 			system.debug('Max campaign member max date found was: '+ ar.get('expr0') + '.');
 			campmaxdate2 = Date.valueOf(ar.get('expr0'));
		}
 		system.debug('Campmaxdate: ' + campmaxdate2);
 		system.debug('Casemaxdate: ' + casemaxdate2);

		if (campmaxdate2 > casemaxdate2) {
    		system.debug('camp is bigger');
        	return campmaxdate2;
		}
		else {
    		system.debug('camp is not bigger');
			return casemaxdate2;
    	}
	}
        
    private string MobilizationStatus (Contact c){
             
         if((c.Last_Served_Date__c<>null) && (Date.TODAY().daysBetween(c.Last_Served_Date__c) < 180) && (c.Volunteer_Status_Current__c =='Activated')) {
            system.debug('Let\'s fix this. The VSC is now Mobilized.');
            Return 'Mobilized'; //if LSD is not NULL and the LSD is less then 180 days old, and the VSC is 'Activated', bumps VSC up to 'Mobilized'
        } else if((c.Last_Served_Date__c<>null) && (Date.TODAY().daysBetween(c.Last_Served_Date__c) > 180) && (c.Volunteer_Status_Current__c=='Mobilized')){
            system.debug('Let\'s fix this. The VSC is now Activated.');
            Return 'Activated'; //if LSD is not NULL and the LSD is older than 180 days, returns VSC to 'Activated'
        } else if ((c.Last_Served_Date__c==null) && (Date.TODAY().daysBetween(MaxDate(c))<180) && (c.Volunteer_Status_Current__c=='Activated')){
            system.debug('The LSD was blank, but because the MaxDate was less than 180 days old and the volunteer was already Activated, VSC changed to: Mobilized.'); //bumps VSC up to Mobilized if LSD is within 180 days
            Return 'Mobilized'; //if LSD is  NULL and the MaxDate is less than 180 days old, bumps VSC up to 'Mobilized'
        } else if ((c.Last_Served_date__c==null) && (Date.TODAY().daysBetween(MaxDate(c))>180) && (c.Volunteer_Status_Current__c=='Mobilized')) {
			system.debug('Mobilization status was changed to Activated because MaxDate was more than 180 days old.');
            Return 'Activated'; //if LSD is NULL and the MaxDate is more than 180 days old and VSC is mobilized, returns VSC to 'Activated'
        } else {
            system.debug('No Change was made.');
            Return c.Volunteer_Status_Current__c; //if it is one of the other 8 possibilities, this returns the current VSC value and makes no changes           
        }         
    }    
    public void UpdateContactRecord (string VolunteerId){
        Contact c=[SELECT ID,Last_Served_Date__c,Volunteer_Status_Current__c from Contact Where ID=:VolunteerId];

        date foundDate = MaxDate(c);
        string foundMobStatus = MobilizationStatus(c);
        c.Volunteer_Status_Current__c = foundMobStatus;
        c.Last_Served_Date__c = foundDate;
        update c;
    }
}

 
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi Nathan,
A) You have to write unit tests using apex test classes to make sure your code is running properly or not
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_unit_tests.htm
B)Apex triggers enable you to perform custom actions before or after changes to specifc object records, such as insertions, updates, or deletions. So we can only write trigger on one object at a time.
As per your requirement you have to create 2 triggers, one on Campaign Member object, Case object beacuse your class methods should  be called whenever Campaign Member or case record is changed.
https://developer.salesforce.com/forums/?id=906F00000009EfuIAE

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards

 
Nathan WylderNathan Wylder
Devi, Thank you 100 times for your response! 
I'm writing the test class now. As for part B in your response, can I just call the class or do I need to call each method within the class? 
trigger CampaignMemberTrigger on CampaignMember (after insert, update, undelete) {
    for campaignMember cm : Trigger.new) {
        MobilizationCredit(cm.ContactId);
    }
}

OR
trigger CampaignMemberTrigger on CampaignMember (after insert, update, undelete) {
    for campaignMember cm : Trigger.new) {
        MobilizationCredit.MaxDate(c);
        MobilizationCredit.MobilizationStatus(c);
        MobilizationCredit.UpdateContactRecord(VolunteerId);
    }
}

DISCLAIMER: I'm somewhat new to this

 
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hey Nathan,
Greetings to you

You have to call each method in the class
trigger CampaignMemberTrigger on CampaignMember (after insert, after update, after undelete) {
    for campaignMember cm : Trigger.new) {
        MobilizationCredit mc = new MobilizationCredit();
        mc.MaxDate(c);
        mc.MobilizationStatus(c);
        mc.UpdateContactRecord(VolunteerId);
    }
}

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards
 
rich man 2rich man 2
You explained in a detailed way. Thanks for sharing this to us. Get more about best apps here. https://cracko.org/microsoft-toolkit-download/