I'm writing code(trigger) that checks the dbd_id field in an Account. The field should not be null or empty. If you try to convert a lead with an empty dbd_id field, you will get an error message. You must fill it in the Lead first. I want to change an error message on the lead conversion page to "Please fill your "DBD ID"" using the addError() method in the accountHandler.apxc class, but it does not work. I don't know whether it's about the condition in the code or not. Please help me.
Here is the error I want to change( to "Please fill your DBD ID") on the conversion page:
There was an error converting the lead. Please resolve the following error and try again: AccountTrigger: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Class.AccountHandler.CheckDBD: line 61, column 1 Class.AccountTriggerHandler.beforeInsert: line 10, column 1 Class.TriggerDispatcher.Run: line 18, column 1 Trigger.AccountTrigger: line 13, column 1
accountHandler.apxc
public class AccountHandler {
public static void CheckDBD(List<Account> newAccounts) {
if(String.isEmpty(newAccounts[0].DBD_ID__c)) {
newAccounts[0].addError('Please fill your "DBD ID".');
}
Id RTPublicId;
Id RTPrivateId;
List<RecordType> RTs = [select id, developername from RecordType];
if(RTs.size() > 0) {
for(RecordType RT : RTs) {
if(RT.DeveloperName == 'Public_Co') {
RTPublicId = RT.Id;
}
else if(RT.DeveloperName == 'Private_Co') {
RTPrivateId = RT.Id;
}
}
}
//AccountHandler.CheckDBD([select Name, DBD_ID__c, RecordTypeId from Account]); **execution code
List<List<String>> dbdid = new List<List<String>>();
List<List<String>> dbdprivate = new List<List<String>>();
List<List<String>> dbdpublic = new List<List<String>>();
Integer count_private = 0;
Integer count_public = 0;
DBD_Info__c[] dbdinfo_private = [select Type__c, starting_number__c, End_number__c
from DBD_Info__c order by Type__c];
DBD_Info__c[] dbdinfo_public = [select Type__c, public_co_number__c
from DBD_Info__c order by Type__c];
//keep info of each account
for(Account Acct : newAccounts) {
dbdid.add(new List<String>{String.valueOf(Acct.Name),
String.valueOf(Acct.DBD_ID__c),String.valueOf(Acct.RecordTypeId)});
}
//keep public_co_number__c
for(DBD_Info__c dbdpub : dbdinfo_public){
dbdpublic.add(new List<String>{String.valueOf(dbdpub.Type__c),
String.valueOf(dbdpub.public_co_number__c)});
}
//keep private dbdid
for(DBD_Info__c dbdpri : dbdinfo_private){
dbdprivate.add(new List<String>{String.valueOf(dbdpri.Type__c),
String.valueOf(dbdpri.starting_number__c),
String.valueOf(dbdpri.End_number__c)});
}
for(List<String> x : dbdid){
//if record type is private
if(x[2]==RTPrivateId){
if(x[1].length()==5 && x[1].isNumeric()){
for(List<String> m : dbdprivate){
if(x[1]>=m[1] && x[1]<=m[2]){
system.debug('name = '+x[0]+' dbd = '+x[1]+' this account is private');
system.debug('in the range'+m[1]+' '+m[2]);
system.debug('type = '+m[0]);
count_private = 0; //reset to zero
newAccounts[0].Business_Type__c = m[0];
break;
}
else{
system.debug('not in the range, go next... ');
system.debug(count_private);
count_private += 1;
}
if(count_private == 21){
newAccounts[0].addError('your private "DBD ID" is not correct.');
}
}
}
else{
if(String.isEmpty(newAccounts[0].DBD_ID__c)){
newAccounts[0].addError('Please fill your "DBD ID".');
}
else{
newAccounts[0].addError('Please enter the correct format for Private "DBD ID"');
}
}
}
//if record type is public
else if(x[2]==RTPublicId){
if(x[1].length()==5 && x[1].substring(0,1).isAlpha()){
for(List<String> n : dbdpublic){
//compare input dbd in the dbd information
if(x[1].Contains(n[1])){
system.debug('name = '+x[0]+' dbd = '+x[1]+' this account is public');
system.debug('yes '+x+'='+n[1]);
system.debug('type = '+n[0]);
count_public = 0; //reset to zero
newAccounts[0].Business_Type__c = n[0];
break;
}
else{
system.debug('not match, go next...');
system.debug(count_public);
count_public += 1;
}
if(count_public == 21){
newAccounts[0].addError('your public "DBD ID" is not correct.');
}
}
}
else{
if(String.isEmpty(newAccounts[0].DBD_ID__c)){
newAccounts[0].addError('Please fill your "DBD ID".');
}
else{
newAccounts[0].addError('Please enter the correct format for Public "DBD ID"');
}
}
}
}
}
}