2

I'm trying to replace hard-coded emails with custom labels in the Aura component and am getting the below error message.

failed to save psa_selectfromadd.cmp: cannot mix expression and literal string in attribute value, try rewriting like {!'foo' + v.bar}: source

SelectFromAdd.cmp

<aura:component implements="force:hasRecordId,flexipage:availableForRecordHome,force:lightningQuickAction" description="My Lightning Component">
    <lightning:quickActionAPI aura:id="quickActionAPI" />
    
    <aura:attribute type="string" name="email1" default="{!$Label.c.pscinfo_psc}" />
    <aura:attribute type="string" name="email2" default="{!$Label.c.ggsinfo_psc}" />

    <aura:attribute type="String[]" name="values" default="[]" access="private" />
    <aura:attribute name="options" type="List" default="[
        {'label': '[email protected]', 'value': '{!v.email1}'},
        {'label': '[email protected]', 'value': '{!v.email2}'}]"/>

    <aura:attribute name="value" type="String" />

    <div>
        <lightning:radioGroup name="radioGroup"
                              label="Select From Email Address:"
                              options="{!v.options}"
                              value="{!v.values}"
                              type="radio"
                              onchange="{!c.handleChange}"
                              required="true"/>
    </div>
</aura: component>

SelectFromAddController.js

({
    handleChange: function(cmp, event) {
        var changeValue = event.getParam("value");
        var actionAPI = cmp.find("quickActionAPI");
        var fields = {
            FromAddress: {
                value: changeValue
            }
        };
        var args = {
            actionName: "Case.PSA_Send_Email",
            entityName: "Case",
            targetFields: fields,
            submitOnSuccess: false
        };
        actionAPI.setActionFieldValues(args)
            .then(function() {
                // actionAPI.invokeAction(args);
            })
            .catch(function(e) {
                console.error(e.errors);
            });
    },
    doInit: function(component, event, helper) {
        var options = [
            $A.get("$Label.c.pscinfo_psc"),
            $A.get("$Label.c.ggsinfo_psc")
        ];
        component.set('v.options', options);
    }
})

1 Answer 1

1

You could use the datasource design attribute for your aura attribute. It allows an Apex class to be used as the source of the options.

In the apex class you can return the custom labels, whether dynamically fetched emails, for example from custom metadata or custom settings.

Example with the custom metadata, it allows not to hardcode everything and not create custom labels.

Apex class

public class CustomMetadataEmailPicklist implements VisualEditor.DynamicPickList {
    
    public class PicklistOption {
        public String label;
        public String value;
        
        public PicklistOption(String label, String value) {
            this.label = label;
            this.value = value;
        }
    }
    
    public List<VisualEditor.PicklistRow> getValues() {
        List<VisualEditor.PicklistRow> picklistRows = new List<VisualEditor.PicklistRow>();
        
        // Retrieve custom metadata records containing email addresses
        List<Custom_Metadata__mdt> emailRecords = Custom_Metadata__mdt.getAll().values();
        
        for (Custom_Metadata__mdt record : emailRecords) {
            String email = record.Email__c;
            picklistRows.add(new PicklistOption(email, email));
        }
        
        return picklistRows;
    }
}

Aura component

<design:component>
        <design:attribute name="options" datasource="apex://CustomMetadataEmailPicklist "/>
</design:component>

You must log in to answer this question.

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