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
SFDC_2706SFDC_2706 

Trigger to update subject on checkbox update

Hi Experts
I need quick help on one of the trigger i have written. I have a trigger on event object and what it does is updating a subject field with "Cencelled" word in front of the subject when one checkbox is checked.
Subject field : Subject__c
Checkbox : eventcancelled__c
trigger : public static void updateSubjectOnActivity(List<Event> newItems , Map<Id,Event> oldItems) {        for(Event currentEvent : newItems){
            if( (oldItems != null && currentEvent.GAM_MeetingCancelled__c != oldItems.get(currentEvent.Id).GAM_MeetingCancelled__c && currentEvent.GAM_MeetingCancelled__c == true )){
                currentEvent.Subject = 'CANCELLED' + ' - ' + currentEvent.Subject;
            }
            if( (oldItems != null && currentEvent.GAM_MeetingCancelled__c != oldItems.get(currentEvent.Id).GAM_MeetingCancelled__c && currentEvent.GAM_MeetingCancelled__c == false )){
                currentEvent.Subject = currentEvent.Subject ;
            }
        }
    }

This is working fine for me. Now i want to remove the "Cancelled" word from subject when again checkbox is unchecked. Not sure how should i take the first oldest value again in my map. 

Please help and thanks in advance..!!

Thank you

Best Answer chosen by SFDC_2706
AbhinavAbhinav (Salesforce Developers) 
Hi Ronu,

Updated code:
 
trigger : public static void updateSubjectOnActivity(List<Event> newItems , Map<Id,Event> oldItems) {        for(Event currentEvent : newItems){
            if( (oldItems != null && currentEvent.GAM_MeetingCancelled__c != oldItems.get(currentEvent.Id).GAM_MeetingCancelled__c && currentEvent.GAM_MeetingCancelled__c == true )){
                currentEvent.Subject = 'CANCELLED' + ' - ' + currentEvent.Subject;
            }
            

if( (oldItems != null && oldItems.get(currentEvent.Id).GAM_MeetingCancelled__c ==true && currentEvent.GAM_MeetingCancelled__c == false )){ if (currentEvent.Subject.contains('CANCELLED -'))
    {
    currentEvent.Subject = currentEvent.Subject.substringAfter('CANCELLED -') ; 
} }
        }
    }


If it helps mark it as best answer.

Thanks!

All Answers

AbhinavAbhinav (Salesforce Developers) 
Hi Ronu,

Updated code:
 
trigger : public static void updateSubjectOnActivity(List<Event> newItems , Map<Id,Event> oldItems) {        for(Event currentEvent : newItems){
            if( (oldItems != null && currentEvent.GAM_MeetingCancelled__c != oldItems.get(currentEvent.Id).GAM_MeetingCancelled__c && currentEvent.GAM_MeetingCancelled__c == true )){
                currentEvent.Subject = 'CANCELLED' + ' - ' + currentEvent.Subject;
            }
            

if( (oldItems != null && oldItems.get(currentEvent.Id).GAM_MeetingCancelled__c ==true && currentEvent.GAM_MeetingCancelled__c == false )){ if (currentEvent.Subject.contains('CANCELLED -'))
    {
    currentEvent.Subject = currentEvent.Subject.substringAfter('CANCELLED -') ; 
} }
        }
    }


If it helps mark it as best answer.

Thanks!
This was selected as the best answer
SFDC_2706SFDC_2706
Hi Abhinav
Thank you for your revert. 
I changed my second IF condition which is :
if( (oldItems != null && currentEvent.GAM_MeetingCancelled__c != oldItems.get(currentEvent.Id).GAM_MeetingCancelled__c && currentEvent.GAM_MeetingCancelled__c == false )){ currentEvent.Subject = currentEvent.Subject ; }

to yours condition :
if( (oldItems != null && oldItems.get(currentEvent.Id).GAM_MeetingCancelled__c ==true && currentEvent.GAM_MeetingCancelled__c == false )){ currentEvent.Subject = currentEvent.Subject ; } }

Unfortunately nothing happened. Cancelled word is still there when I uncheck the checkbox.

Thanks..!!
SFDC_2706SFDC_2706
public static void updateSubjectOnActivity(List<Event> newItems , Map<Id,Event> oldItems) {  
            for(Event currentEvent : newItems){
            if( (oldItems != null && currentEvent.GAM_MeetingCancelled__c != oldItems.get(currentEvent.Id).GAM_MeetingCancelled__c && currentEvent.GAM_MeetingCancelled__c == true )){
                currentEvent.Subject = 'CANCELLED' + ' - ' + currentEvent.Subject;
            }

            if( (oldItems != null && currentEvent.GAM_MeetingCancelled__c != oldItems.get(currentEvent.Id).GAM_MeetingCancelled__c && currentEvent.GAM_MeetingCancelled__c == false )){
                currentEvent.Subject = currentEvent.Subject ;
            }
        }
    }

In the above code, BOLD code is for adding the CANCELLED which is working fine. And rest of the code is where I am facing problem to remove the CANCELLED word from the subject. Hope this will help you..!!
AbhinavAbhinav (Salesforce Developers) 
Have updated earlier answer.Check that
SFDC_2706SFDC_2706
Thank you Abhinav. It worked for me. :)