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
Andrew PerezAndrew Perez 

Auto increment based on Record Type

I am trying to have a field (Meeting_Type_RM__c) on Event auto increment based on the number of Events a Contact has and the Record Type (RM).  So the first Event should have a meeting type of RM1, the next RM2 and so on.  The numbering series needs to be unique for each Contact which is why I just can't use the auto number field type.

With the code that I have right now every Event is just showing RM1 as the meeting type instead of increasing by 1 each time. Also, I don't think it is unique for each Contact.  This is what I have right now:

trigger EventAutoNumber on Event (before insert) {
    list<Event> e = new list<Event>
    ([select Meeting_Type_RM__c from Event order by Meeting_Type_RM__c desc limit 1]);
    
   
    String startNumber = '0';
    String Word = 'RM';



    for(Event ev:trigger.new) {

        if(ev.RecordTypeID == '012f0000000D1fG'){
        startNumber = String.valueOf(Integer.valueOf(startNumber) + 1);
        startNumber = '0'.substring(0, 1-startNumber.length()) + startNumber;
        ev.Meeting_Type_RM__c = Word + startNumber;
        }
    }
}
Boris BachovskiBoris Bachovski
Ok, first of all you're not distinguishing between which events belong to which contact.

Issue number 2 is hardcoding the record type ID. This is a big no no.

See my code below including comments of how best to resolve this problem. Please note that it might not be working 100% as I typed it on the fly and never compiled, but you should be able to get the idea:

// Populate a map with the event record types
Map <String, Id> recordTypes = new Map <String, Id> ();

for (RecordType recordType : [SELECT Id, DeveloperName FROM RecordType WHERE sObjectType = 'Event'])
{
	if (recordType.DeveloperName == 'RM') // or whatever your record type developer name is
	{
		recordTypes.put(recordType.DeveloperName, recordType.Id);
	}
}

Map <Id, Event> contactIdsToEvents = new Map <Id, Event> ();

for (Event event : trigger.new)
{
	// it's RM record type and it has contact lookup populated
	if (event.RecordTypeId == recordTypes.get('RM') && event.WhoId.startsWith('003')) 
	{
		// map the contact ID and the actual event record
		contactIdsToEvents.put(event.WhoId, event);
	}
}

// Get all targeted contacts with their related event (last ordered by meeting type)
for (Contact contact : [SELECT Id, (SELECT Meeting_Type_RM__c FROM Events ORDER BY Meeting_Type_RM__c DESC LIMIT 1) FROM Contact WHERE Id IN :contactIdsToEvents.keySet()])
{
	// Make sure the contact has at least 1 related event - it's not null
	if (contact.Events.size() == 1)
	{
		// based on the contact ID, get the current trigger event record and set the number
		if (contactIdsToEvents.get(contact.Id) != null)
		{
			Integer lastNumber = Integer.valueOf(contact.Events[0].Meeting_Type_RM__c.substring(2)); // String off RM from the last event (if RM4, it should return 4 as integer)
			contactIdsToEvents.get(contact.Id).Meeting_Type_RM__c = 'RM' + (lastNumber + 1); // increment by 1 and add RM prefix
		}
	}
}

 
Andrew PerezAndrew Perez
Boris, thanks for the help.  

I did have to add String.valueOf to 'if (event.RecordTypeId == recordTypes.get('RM') && String.valueOf(event.WhoId).startsWith('003'))' which got the code to run without any errors but it isn't inserting anything into the field.  Does there have to be an insert/update statement on the field?