trigger AccountChangeTrigger on Account (after insert, after update) { System.debug('AccountChangeTrigger ENTERED'); System.debug(Trigger.New); System.debug(Trigger.Old); Account NewAccountValues = Trigger.New[0]; //TODO Determine if update made by API using last modified by. If so, do not publish event to avoid infinite loop System.debug('last modified by:' + NewAccountValues.LastModifiedById); //DETERMINE IF STATUS SWITCHED FROM INACTIVE TO ACTIVE String NewActive = NewAccountValues.Active__c; String OutputActive = 'inactive'; //DEFAULT IS INACTIVE, MEANING MESSAGE IS NOT SENT //IF IT'S AN UPDATE THEN COMPARE OLD AND NEW VALUES if (Trigger.isUpdate) { Account OldAccountValues = Trigger.Old[0]; String OldActive = OldAccountValues.Active__c; if((OldActive == 'no' || String.isBlank(OldActive)) & NewActive == 'yes'){ OutputActive = 'activated'; }else if(OldActive == 'yes' & NewActive == 'yes'){ OutputActive = 'activate'; }else if(OldActive == 'yes' & (NewActive == 'no' || String.isBlank(NewActive))){ OutputActive = 'deactivated'; } }else{ //OTHERWISE IT'S A NEW RECORD if(NewActive == 'yes'){ OutputActive = 'activated'; } } if(OutputActive != 'inactive'){ System.debug('Record is not inactive. Publishing event. Value of OutputActive is: ' + OutputActive); //MAP FROM THE RECORDS TO THE PLATFORM EVENT AccountChanged__e AccountChanged = new AccountChanged__e(); AccountChanged.Active__c = OutputActive; AccountChanged.BillingCity__c = NewAccountValues.BillingCity; AccountChanged.BillingPostalCode__c = NewAccountValues.BillingPostalCode; AccountChanged.BillingState__c = NewAccountValues.BillingState; AccountChanged.BillingStreet__c = NewAccountValues.BillingStreet; AccountChanged.Name__c= NewAccountValues.Name; AccountChanged.Phone__c=NewAccountValues.Phone; AccountChanged.recordId__c=NewAccountValues.Id; EventBus.publish(AccountChanged); }else{ System.debug('Record is inactive. NOT Publishing event. Value of OutputActive is: ' + OutputActive); } System.debug('AccountChangeTrigger EXITED'); }