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
Preeti Khanna 10Preeti Khanna 10 

I need to update incremental code in a custom field on accounts so that if user changes the Type from Prospect to customer then no. should be incremented by 1.

I need to update incremental code in a custom field on accounts so that if user changes the Type from Prospect to customer then no. should be incremented by 1.For e.g got accountA it will be say C1 and for account 2 it will be C2 and for account3 it will be C3 whenever user chnages the type from Prospect to Customer.It will pick the last no. of previous record and then it will increment.Code will be unique for each account as we need to use the same code for integration purpose.

Please help.
Best Answer chosen by Preeti Khanna 10
Nayana KNayana K
Now this is working :
Changes are shown in bold

public with sharing class AccountHandler
{
    public AccountHandler()
    {
        
    }
    
    public void onBeforeUpdate(Map<Id,Account> mapNewAccount, Map<Id,Account> mapOldAccount)
    {
        // replace CountField__c with proper field
        String strMaxCountNum = '';
        Integer intCount = 0;
        List<Account> lstAccount = [SELECT Id,CountField__c FROM Account ORDER BY CountField__c DESC NULLS LAST LIMIT 1];
        if(!lstAccount.isEmpty())
        {
            strMaxCountNum = lstAccount[0].CountField__c;
        }
        
        If(String.isNotBlank(strMaxCountNum))
        {
            Integer startIndex = strMaxCountNum.indexOf('C0000');
            if(startIndex != -1)
            {
                String strCount = strMaxCountNum.substring(startIndex + 5, strMaxCountNum.length());
                if(String.isNotBlank(strCount))
                {
                    intCount = Integer.valueOf(strCount);
                }
            }
            
        }
        
        for(Account objAccount : mapNewAccount.values())
        {
            
            if(objAccount.Type != mapOldAccount.get(objAccount.Id).Type && objAccount.Type == 'Customer' && mapOldAccount.get(objAccount.Id).Type == 'Prospect')
            {
                intCount++;
                objAccount.CountField__c = 'C0000' + String.valueOf(intCount);
            }
        }
    }
}

All Answers

Nayana KNayana K
Trigger :

trigger AccountTrigger on Account (before update)
{  
    AccountHandler objHandler = new AccountHandler();
    if(trigger.isBefore && trigger.isUpdate )
    {
        objHandler.onBeforeUpdate(Trigger.NewMap, Trigger.oldMap);
    }
}


Class :

public with sharing class AccountHandler
{
    public AccountHandler()
    {
        
    }
    
    public void onBeforeUpdate(Map<Id,Account> mapNewAccount, Map<Id,Account> mapOldAccount)
    {
        for(Account objAccount : mapNewAccount.values())
        {
            // replace CountField__c with proper field
            if(objAccount.Type != mapOldAccount.get(objAccount.Id).Type && objAccount.Type == 'Customer' && mapOldAccount.get(objAccount.Id).Type == 'Prospect')
                objAccount.CountField__c = objAccount.CountField__c == null ? 1 : (objAccount.CountField__c + 1);
        }
    }
}
Preeti Khanna 10Preeti Khanna 10
Hi Nayana,

Thanks alot for the same!
I want my customer code to be updated as C00001.Do I need to store the initial code value in variable.
I want it to be displayed as C00001,C00002,C00003.

As I am new to salesforce so need your help on the same.

Thanks,
Preeti
Nayana KNayana K
Suppose an account is changed 11 times from prospect to customer, then Account.CountField__c = C000010 or Account.CountField__c = C00010 ?
If "C0000" + count is the fixed format (where prefix C0000 is always fixed), the below code may work.

If not fixed (Account.CountField__c = C00010) then some other logic is needed.

    
public with sharing class AccountHandler
{
    public AccountHandler()
    {
        
    }
    
    public void onBeforeUpdate(Map<Id,Account> mapNewAccount, Map<Id,Account> mapOldAccount)
    {
      
        for(Account objAccount : mapNewAccount.values())
        {
            
            // replace CountField__c with proper field
            if(objAccount.Type != mapOldAccount.get(objAccount.Id).Type && objAccount.Type == 'Customer' && mapOldAccount.get(objAccount.Id).Type == 'Prospect')
            {
                if(objAccount.CountField__c != null)
                {
                    Integer startIndex = objAccount.CountField__c.indexOf('C0000');
                    if(startIndex != -1)
                    {
                        String strCount = objAccount.CountField__c.substring(startIndex + 4, objAccount.CountField__c.length() -1);
                        Integer intCount = Integer.valueOf(strCount) + 1;
                        objAccount.CountField__c = 'C0000' + intCount;
                    }
                }
                else
                {
                    objAccount.CountField__c = 'C00001';
                }
            }
        }
    }
}

 
Preeti Khanna 10Preeti Khanna 10

Hi Nayana,

 

Above code is not working.Please help.

Regards,
Preeti

Preeti Khanna 10Preeti Khanna 10
Hi Nayana,

Each account will have unique number and if incremented once then it will not change that I will handle through validation rule.
Like account A will have c00001,account B will have C00002,Account C will have C00003 and soon ..
 
Nayana KNayana K

public with sharing class AccountHandler
{
    public AccountHandler()
    {
        
    }
    
    public void onBeforeUpdate(Map<Id,Account> mapNewAccount, Map<Id,Account> mapOldAccount)
    {
        // replace CountField__c with proper field
        String strMaxCountNum = '';
        Integer intCount = 0;
        List<Account> lstAccount = [SELECT Id,CountField__c FROM Account ORDER BY CountField__c DESC LIMIT 1];
        if(!lstAccount.isEmpty())
        {
            strMaxCountNum = lstAccount[0].CountField__c;
        }
        
        If(String.isNotBlank(strMaxCountNum))
        {
            Integer startIndex = strMaxCountNum.indexOf('C0000');
            if(startIndex != -1)
            {
                String strCount = strMaxCountNum.substring(startIndex + 5, strMaxCountNum.length()-1);
                if(String.isNotBlank(strCount))
                {
                    intCount = Integer.valueOf(strCount);
                }
            }
            
        }
        
        for(Account objAccount : mapNewAccount.values())
        {
            
            if(objAccount.Type != mapOldAccount.get(objAccount.Id).Type && objAccount.Type == 'Customer' && mapOldAccount.get(objAccount.Id).Type == 'Prospect')
            {
                intCount++;
                objAccount.CountField__c = 'C0000' + String.valueOf(intCount);
            }
        }
    }
}
Preeti Khanna 10Preeti Khanna 10
Hi Nayana,

thanks for your help and time!
But issue is still not resolved.for each account it is showing code as C00001.While for account A it should be C00001.,Account B C00002,account c it should be C00003 and sonon.
Please help.Thanks alot for your time!
Nayana KNayana K
Now this is working :
Changes are shown in bold

public with sharing class AccountHandler
{
    public AccountHandler()
    {
        
    }
    
    public void onBeforeUpdate(Map<Id,Account> mapNewAccount, Map<Id,Account> mapOldAccount)
    {
        // replace CountField__c with proper field
        String strMaxCountNum = '';
        Integer intCount = 0;
        List<Account> lstAccount = [SELECT Id,CountField__c FROM Account ORDER BY CountField__c DESC NULLS LAST LIMIT 1];
        if(!lstAccount.isEmpty())
        {
            strMaxCountNum = lstAccount[0].CountField__c;
        }
        
        If(String.isNotBlank(strMaxCountNum))
        {
            Integer startIndex = strMaxCountNum.indexOf('C0000');
            if(startIndex != -1)
            {
                String strCount = strMaxCountNum.substring(startIndex + 5, strMaxCountNum.length());
                if(String.isNotBlank(strCount))
                {
                    intCount = Integer.valueOf(strCount);
                }
            }
            
        }
        
        for(Account objAccount : mapNewAccount.values())
        {
            
            if(objAccount.Type != mapOldAccount.get(objAccount.Id).Type && objAccount.Type == 'Customer' && mapOldAccount.get(objAccount.Id).Type == 'Prospect')
            {
                intCount++;
                objAccount.CountField__c = 'C0000' + String.valueOf(intCount);
            }
        }
    }
}
This was selected as the best answer
Preeti Khanna 10Preeti Khanna 10
Thanks alot Nayana for your help...Issue is resolved...