0

I am trying to import a database that have numbers in the picklist value. (1,2,3,4,5..etc)

However, in the user-end, they should see different set of values, for example (

1.-> Partner 2.->LLC 3.->Other

So I want an apex trigger saying

IF field value is 1; then replace it to Partner IF field value is 2; then replace it to LLC and etc.

I considered using the IF statements. Some recommended using the toLabel function. Please clarify! Please help!

Thank you so much :)

1

My first though is to use a map for the value replacement like this:

trigger MyTrigger on MyObject__c (before insert) {

    Map<String, String> m = new Map<String, String>{
        '1' => 'Partner',
        '2' => 'LLC',
        '3' => 'Other'
    };

    for (MyObject__c o : Trigger.new) {
        String value = m.get(o.MyField__c);
        if (value != null) o.MyField__c = value;
    }
}
1

You should try using translation workbench to define values stored in the picklist field.

You can show values as different language or set them to a default single language.

If this picklist field is required in some apex coding, you can use toLabel() method to work with the translated values.

For example, You can map 1 as 'partner' in translation workbench and show it to users and if 'partner' value is required in apex logic then use toLabel() method to get 'partner' in the apex.

0

You could use a custom setting:

for(SObject record: Trigger.new)
    if(FieldMap__c.getInstance(record.Field__c) != null)
        record.Field__c = FieldMap__c.getInstance(record.Field__c).Value__c;

Or a custom metadata:

for(SObject record: Trigger.new)
    try {
        record.Field__c = [SELECT Value__c FROM FieldMap__mdt WHERE Name = :record.Field__c].Value__c;
    } catch(QueryException e) {
        // No match found, go to next
    }

Note: Custom metadata queries do not count against SOQL limits at all, so this is one of the very rare cases where a query inside a loop is acceptable.

Both options allow you to change the values without deploying a new version of your code every time you need to add, remove, or alter a value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.