0

We have a field on the account, Market_Segment__c, that is controlled by the following piece of code:

    public static string getMarketSegment(integer employeeCount){
if(employeeCount < 201) 
        return 'SMB';
    else if(employeeCount < 1001)
        return 'MID';
    else if(employeeCount < 4501)
        return 'MAJ';
    else return 'ENT';
}

The problem is, the banding for the different segments changes every year, meaning we have to update the apex code every year, at the beginning of the new fiscal year, when we're usually (trying to) have a code freeze in effect.

I'd like to use an alternative approach that would require NOT having to update this code so often. Trying to remove this piece of code causes issues because of a complex interdependency on other classes that, so far, has been more trouble to untangle than it's worth.

I've been looking at using custom metadata. I've created a custom metadata type, Market Segment Threshold, with a custom field Threshold, and I was thinking I could replace the numerical values in the code with a reference to the metadata.

My questions is, first, is this the best/correct way of approaching this? And if so, how do I go about this? do I need SOQL queries or...? Thanks!

1
  • 1
    Is the employee count available on the Account record itself? If so, I'd think this would be better off as a formula field.
    – Derek F
    Feb 18, 2021 at 20:47

1 Answer 1

1

If this field needs to remain writeable (or specific to the year in which it was created), then you can move these bands into Custom Metadata. If neither of those conditions apply, use a Formula Field instead. With metadata mappings, your code would look something like:

List<Market_Segment__mdt> segments = [
    SELECT Label__c, Minimum__c, Maximum__c, FROM Market_Segment__mdt
];
for (Market_Segment__mdt segment : segments)
{
    if (segment.Minimum__c < employeeCount && segment.Maximum__c > employeeCount)
    {
        return segment.Label;
    }
}
return 'ENT';
0

You must log in to answer this question.

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