0

I am stuck trying to simply concatenate Checkbox Values with Label onto my Long text are field.

mycheckbox1__c (checkbox field) -- example: Yes

mycheckbox2__c (checkbox field) -- example: Yes

mycheckbox3__c (checkbox field) -- example: No

I want to update a field "Message__c" which is a Long text area field and looks like this using above three checkbox fields Labels and values:

mycheckbox1__c: Yes, mycheckbox2__c: Yes, mycheckbox3__c: No

This is what I have right now. How can i achieve this?

1 Answer 1

1

You need to use a before trigger, and there, using the field describe to get the label, concatenate it with its value, in your desired field.

something like this:

exampletrigger on account (before update) {
    Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    Schema.SObjectType accountschema= schemaMap.get('account');

    Map<String, Schema.SObjectField> lfieldMap = accountschema.getDescribe().fields.getMap();

    List<String> apiCheckboxFields = new List<String>{'examplefield1', 'examplefield2'};
    for (String fieldName : apiCheckboxFields) {
        
        String lfieldLabel = lfieldMap.get(fieldName).getDescribe().getLabel();
        for (Account acc : trigger.new) {
            acc.Message__ += lfieldLabel + ': ' + acc.get(fieldName) + ' '; 
        }

    }
}

Note*: this is just an example, triggers should never have logic.

You must log in to answer this question.

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