You need to sign in to do that
Don't have an account?
Preeti 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.
Please help.
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
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);
}
}
}
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
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';
}
}
}
}
}
Hi Nayana,
Above code is not working.Please help.
Regards,
Preeti
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 ..
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);
}
}
}
}
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!
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);
}
}
}
}