Am trying to create auto number through apex.
The functionality which i require there in an number field which is number/text type which is UNIQUE. Whenever an Account is created the custom number field should be incremented by 1 irrespective of the previous record are deleted the count should always keep on incrementing through a trigger. i can't use a auto number field type as there are different record types and the auto number field can't be incremented based on record type, the gap is okay and there shouldn't be no duplicates My code below.
trigger rectype on Account (before insert) {
for(Account accitem : Trigger.New){
Account acc = new Account();
System.debug('rectype'+recordTypeId);
List<Account> results =[SELECT <custom field> FROM Account where RecordTypeId='0120l000000PxdRAAS' order by CreatedDate desc LIMIT 1 ];
Decimal prtnr = results[0].custom field;
System.debug('count'+prtnr);
accitem.custom field= prtnr+1;
// i=integer.valueof(s);
System.debug('PartnerId Visible'+accitem.<custom field>);
}
}
The issue here is lets say the record is getting deleted , the incrementeation is happening from the recent record not with the last Customfield value.
From user3169951's comment:
i can't use a auto number filed type as there are different record types and the auto number field can't be incremented based on record type, the gap is okay and there shouldnt be no duplicates