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
Dilyan DimitrovDilyan Dimitrov 

Company names list configuration

Hello,

I need an advice regaring the following implementation. I need to normalize large set of user generated company names for instance 'Derby International LLC', and I need the company name without its extension. In this case the extension is 'LLC'. I use an algorithm which I wrote myself in order to remove the extensions from the name of the company which is working ok. However, in order to get the extesnsions I used a list to store each company extension. Here is the code:List<String> companyExtensionsList = new List<String>(); private static void initCompanyExtensions() { companyExtensionsList.add('ltd'); companyExtensionsList.add('inc'); companyExtensionsList.add('group'); companyExtensionsList.add('corp'); companyExtensionsList.add('llc'); companyExtensionsList.add('llp'); companyExtensionsList.add('lp'); }The company extensions are actually a list of constants.

What I need to know is whether there is a better approach to store the extensions instead of storing the them into a list strings?

Regards,

Dilyan
pconpcon
What I would do is to create a custom list setting [1] called "Company Extensions" then create each one with the name that you are trying to remove.  Then pull those in and use them in your code.  This way you can add them without having to re-deploy and you don't have to burn any DML/SOQL to get them

User-added image
User-added image
 
List<String> companyNames = new List<String>{
    'Megacorp LLC',
    'Buy N Large LLP',
    'Umbrella Corp',
    'CHOAM INC'
};
    
Set<String> extensions = CompanyExtensions__c.getAll().keySet();
Map<String, Pattern> patternMap = new Map<String, Pattern>();

for (String extension : extensions) {
    // Use a case insensitive match and only match at the end of the string
    patternMap.put(extension, Pattern.compile('(?i)' + extension + '$'));
}

for (String companyName : companyNames) {
    System.debug('Looking for: ' + companyName);
    for (String extension : extensions) {
        Matcher m = patternMap.get(extension).matcher(companyName);
        
        if (m.find()) {
            String newName = m.replaceAll('').trim();
            System.debug('Converted name: ' + newName);
            break;
        }
    }
}
 
05:51:43.042 (42031892)|USER_DEBUG|[17]|DEBUG|Looking for: Megacorp LLC
05:51:43.042 (42894399)|USER_DEBUG|[23]|DEBUG|Converted name: Megacorp
05:51:43.042 (42987880)|USER_DEBUG|[17]|DEBUG|Looking for: Buy N Large LLP
05:51:43.044 (44200647)|USER_DEBUG|[23]|DEBUG|Converted name: Buy N Large
05:51:43.044 (44290526)|USER_DEBUG|[17]|DEBUG|Looking for: Umbrella Corp
05:51:43.046 (46349503)|USER_DEBUG|[23]|DEBUG|Converted name: Umbrella
05:51:43.046 (46439424)|USER_DEBUG|[17]|DEBUG|Looking for: CHOAM INC
05:51:43.047 (47219982)|USER_DEBUG|[23]|DEBUG|Converted name: CHOAM
[1] https://help.salesforce.com/HTViewHelpDoc?id=cs_about.htm&language=en_US (https://help.salesforce.com/HTViewHelpDoc?id=cs_about.htm&language=en_US)
Dilyan DimitrovDilyan Dimitrov
Hello,

Thank you for your advice but for some reason it is not possible to read the element values from the custom list Company_Extensions__c.

Here is how I attempt to read the elements
Set<String> extensions = Company_Extensions__c.getAll().keySet(); 

List elements

System.debug('extensions ' + extensions); 

22:52:45.107 (107645930)|USER_DEBUG|[37]|DEBUG|extensions ()

Where in the debug log I found that there are no elements in the list Company_Extensions__c.

Here is screenshot of the List Company_Extensions__c and the elements of the list created in my Salesforce sandbox.

Please find the attached image file.

Please advise what am I doing wrong and how to get the elements from the List Company_Extensions__c?
Dilyan DimitrovDilyan Dimitrov
List elements
pconpcon
The problem is you added these as fields.  You only need the Name field in your custom setting.  In my screenshot that is a custom setting per extension.  You will want to delete all of the "custom fields" listed under that custom setting then click "manage" and add a new custom setting with the Name field filled out for each of your extensions.
Dilyan DimitrovDilyan Dimitrov
Hello,


I have the following method
 
public static String splitCompanyName(String company) {
        String companyNameLowerCase = company.toLowerCase();
        // get all extensions from the salesforce list and are added in the set
        List<Company_Extensions__c> extensions = Company_Extensions__c.getAll().values();
        Set<String> companyExtensions = new Set<String>();
        for(Company_Extensions__c extension : extensions) {
            companyExtensions.add(extension.Name);                
        }
        // remove all extensions from company name
		for(String companyExtension : companyExtensions){
            integer extIndex = companyNameLowerCase.indexOf(' ' + companyExtension);
            if(extIndex > -1) {
                // in order to avoid IndexOutOfBounds exception the two variables must be with equal values regardless of the case
                company = company.substring(0, extIndex);
                companyNameLowerCase = companyNameLowerCase.substring(0, extIndex);    
            }
        }        
        return company;        
    }

When I run test from a test class
static testMethod void testSplitCompanyName(){
         Test.startTest();
         String company = 'Salesforce Org';
         LeadToMerchantController.splitCompanyName(company);         	
         Test.stopTest();
    }

for some reason List<Company_Extensions__c> extensions does not contain elements because it does not enter the first for loop.

Here are the details from the log:

User-added image

Could please advise how to get the values of the emlements from the list.
Dilyan DimitrovDilyan Dimitrov
User-added image
pconpcon
The reason you are not getting into your loop in your test is that you did not create a the custom setting.  You will need to do this prior to calling your method.

Additionally, I would really recommend that you use the Pattern / Matcher construct I provided because it will be faster for large data sets.
Dilyan DimitrovDilyan Dimitrov
Hi,

I've created a custom setting with the company extensions, please take a look at my last screenshot added. What I've noticed is that the when I convert Lead to Account the code enters the first for loop but when I run the test from the test class the List<Company_Extensions__c> extensions does not contain elements because it does not enter the for loop. Could you please advise what I need to do and how to run the test so it can enter the first for loop?

Regards,

Dilyan
pconpcon
Like I said before, you'll need to create one in your test class.
 
static testMethod void testSplitCompanyName() {
    Company_Extensions__c extension = new Company_Extension(
        Name = 'Org'
    );
    insert extension;

    Test.startTest();

    String company = 'Salesforce Org';
    LeadToMerchantController.splitCompanyName(company);   
      	
    Test.stopTest();
}