You need to sign in to do that
Don't have an account?
Andrew 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;
}
}
}
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;
}
}
}
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:
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?