• rich man 2
  • 0 Points
  • Member since 2019

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
  • 1
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;