0

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"');
                        }
                    }
                }
            }
        }
    }

1 Answer 1

0

try the String.isBlank() method instead of String.isEmpty() it performs more checks if the string is for sure without a value

4
  • it's still not working.
    – sirps
    Commented Feb 6, 2023 at 11:45
  • Well. Then try to create a test class for your AccountHandler class, create a record that will reproduce the error and see when the coverage stops, there will be a line that crashes your code with the eception that you mentioned in your question. Or try to debug it and check where is the null value. This will help you trailhead.salesforce.com/content/learn/projects/… Commented Feb 6, 2023 at 11:55
  • 1
    Thanks, sir; it's working now. I have added the "try catch" that checks the exception for System.NullPointerException and then throws the newAccounts[0].addError('Please fill your "DBD ID".');
    – sirps
    Commented Feb 6, 2023 at 14:54
  • You're welcome @sirps Commented Feb 6, 2023 at 14:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .