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
rohan patel 30rohan patel 30 

How to increment a text field by 1 after record creation.

Hello,
I have a simple use case.
I need to update a text field "Contact id" after increment it by 1 whenever a new contact is created.
below is ss for reference. I want to update contact id as 172 for new contact creation.
Please help me how can I update a text field by 1 .

field label : contact id
api name :Contact_ids__c


User-added image 
Abdul KhatriAbdul Khatri
Hi Rohan,

I am very critical of design solutions and before providing any answer, would try to understand the requirement.

Is this field to know how many total contacts you have in the system and does this field represents the number for example you may have contact created before with the number 170, 169, 168, etc?

What if any contact got deleted, you may lose the sequence and this information may not give you the right picture?

.If you can provide a little more context.
 
rohan patel 30rohan patel 30
Hi @abdul, thanks for your prompt reply,
I just need this field for a unique contact id for each record. 170 is highest number currently and I want to increment it like 171,172 and so on.
this field does not represent number of contact so losing sequence won't be an issue.

 
Abdul KhatriAbdul Khatri
Hi Rohan,

So assuming this field exists on Contact SObject. Here is the code

Trigger
trigger ContactTrigger on Contact (before insert) {

    if(TriggerOperation.BEFORE_INSERT)
        ContactService.ContactIdIncrement(trigger.new);
}

Apex Class
public class ContactService {    
    
    public static void ContactIdIncrement(List<Contact> contactList){
        Integer contactCounter;    
        List<Contact> contPrevList = [SELECT Contact_Id__c 
                            FROM Contact 
                            WHERE Contact_Id__c != null 
                            ORDER BY CreatedDate 
                            DESC];
               
        contactCounter = (contPrevList == null || contPrevList.isEmpty() || contPrevList[0].Contact_Id__c == null) ? 0 : Integer.valueOf(contPrevList[0].Contact_Id__c);
        
        for(Contact cont : contactList)
        {
            contactCounter = contactCounter + 1;
            cont.Contact_Id__c = String.ValueOf(contactCounter);
        }        
    }
}

Test Class
@isTest
public class ContactServiceTest {
    
    @isTest static void testContactIdIncrement() {
        
        List<Contact> contactList = new List<Contact>{
        	new Contact(LastName = 'test1'),
            new Contact(LastName = 'test2')            
        };
        insert contactList;
        
        Contact cont1 = [SELECT Contact_Id__c FROM Contact WHERE LastName = 'test1' LIMIT 1];
        Contact cont2 = [SELECT Contact_Id__c FROM Contact WHERE LastName = 'test2' LIMIT 1];
        
        system.assert.isTrue(cont1.Contact_Id__c == '1');
        system.assert.isTrue(cont2.Contact_Id__c == '2');
    }
    
}

​​​​​​​​​​​​​​