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
ApexNewbie77ApexNewbie77 

Adding a sequence to email subject

Hello,
I have created a custom object that we use to track different issues. Just like any custom object, I have the notes and attachements related list on there. On the object, there is a multi-select picklist field that contains a few different values. I wrote the following apex trigger to send an email notification when a new NOTE is added. The email will be sent to specific address based upon the value in the muli-select picklist. I am trying to figure out how to add a secuence to the subject line. So everytime a note is added the subject shows that it is the first, third, eighth, etc. 

Example: A new custom object record is created. The user creates a new note, which will trigger and send an email notification to a specified email. Since that was the first note added for that record, the subject line should say, "Update 1: A new note has been added to Custom Object Record. When another note is added, the subject like should say, "Update 2: A new note has been added to Custom Object Record". The number should increment for every new note added.

Any help one this would be great! Thanks!
trigger NoteAddedNotification on Note (after insert) 
{
    
    Set<ID> noteids = new Set<ID>();
    
    for (Note n:Trigger.new)
    {
        
        noteids.add(n.parentID);
    }
    
    List<Custom_Object__c> notelist = new List<Custom_Object__c>([SELECT Id, Name, MultiSelect_Picklist__c FROM Custom_Object__c WHERE ID in:noteids]);
    
     Messaging.SingleEmailMessage NoteToCS = new Messaging.SingleEmailMessage();
    
    	String[] toAddresses1 = new String[]
        {
        	'email1@test.com'
    	};
            
        String[] toAddresses2 = new String[]
        {
            'email2@test.com', 
        };    
        
        String[] toAddresses3 = new String[]{
        	'email3@test.com'    
        };
           
    for(Custom_Object__c c:notelist)
    {
    
        NoteToCS.setSubject('A note has been added to Custom Object: ' +c.Name);
        
        String stringURL  = URL.getSalesforceBaseUrl().toExternalForm()+ '/'+ c.Id;
        
        String body = 'A new note was added to ' + c.Name + '<br/>';
        body += 'Multi-Select Picklist: ' + c.MultiSelect_Picklist__c + '<br/>';
        body += 'Please click on the link to take a look:' + '<br/>';
        body += stringURL;
        
        
         if(String.isEmpty(c.MultiSelect_Picklist__c) && c.MultiSelect_Picklist__c == null)
         {
            
                c.addError('Please select a value for Multi-select Picklist before adding a 
                note.');

         }
        else
        {
               if(c.MultiSelect_Picklist__c.contains('Value1'))
               {
            		NoteToCS.setToAddresses(toAddresses1);
            		NoteToCS.setSaveAsActivity(false);
            		NoteToCS.setHtmlBody('<p style="font-family:Calibri;font-size:24px !important;">' + body + '</p>');
        	 		Messaging.sendEmail(new Messaging.Email[] {
             			NoteToCS
             		});
        		}
        		else if(c.MultiSelect_Picklist__c.contains('Value2'))
                {
            		NoteToCS.setToAddresses(toAddresses2);
            		NoteToCS.setSaveAsActivity(false);
            		NoteToCS.setHtmlBody('<p style="font-family:Calibri;font-size:24px !important;">' + body + '</p>');
        	 		Messaging.sendEmail(new Messaging.Email[] {
             			NoteToCS
             		});
        		}
        		else if(c.MultiSelect_Picklist__c.contains('Value3'))
                {
        			NoteToCS.setToAddresses(toAddresses3);
            		NoteToCS.setSaveAsActivity(false);
            		NoteToCS.setHtmlBody('<p style="font-family:Calibri;font-size:24px !important;">' + body + '</p>');
        	 		Messaging.sendEmail(new Messaging.Email[] {
             			NoteToCS
             		});	                             
        		}
        }
        
    }
update notelist ;
}
Best Answer chosen by ApexNewbie77
VamsiVamsi
Hi,

Create a new field on the custom object to maintain the count of Notes when created and then in the subject make use of this new field. So that you can specify 1st,2nd,3rd etc......

Hope this helps ...!!

Please mark as best answer if the above helps ...!!